Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:24:10+00:00 2026-05-29T13:24:10+00:00

Dijkstra’s algorithm was taught to me was as follows while pqueue is not empty:

  • 0

Dijkstra’s algorithm was taught to me was as follows

while pqueue is not empty:
    distance, node = pqueue.delete_min()
    if node has been visited:
        continue
    else:
        mark node as visited
    if node == target:
        break
    for each neighbor of node:
         pqueue.insert(distance + distance_to_neighbor, neighbor)

But I’ve been doing some reading regarding the algorithm, and a lot of versions I see use decrease-key as opposed to insert.

Why is this, and what are the differences between the two approaches?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:24:11+00:00Added an answer on May 29, 2026 at 1:24 pm

    The reason for using decrease-key rather than reinserting nodes is to keep the number of nodes in the priority queue small, thus keeping the total number of priority queue dequeues small and the cost of each priority queue balance low.

    In an implementation of Dijkstra’s algorithm that reinserts nodes into the priority queue with their new priorities, one node is added to the priority queue for each of the m edges in the graph. This means that there are m enqueue operations and m dequeue operations on the priority queue, giving a total runtime of O(m Te + m Td), where Te is the time required to enqueue into the priority queue and Td is the time required to dequeue from the priority queue.

    In an implementation of Dijkstra’s algorithm that supports decrease-key, the priority queue holding the nodes begins with n nodes in it and on each step of the algorithm removes one node. This means that the total number of heap dequeues is n. Each node will have decrease-key called on it potentially once for each edge leading into it, so the total number of decrease-keys done is at most m. This gives a runtime of (n Te + n Td + m Tk), where Tk is the time required to call decrease-key.

    So what effect does this have on the runtime? That depends on what priority queue you use. Here’s a quick table that shows off different priority queues and the overall runtimes of the different Dijkstra’s algorithm implementations:

    Queue          |  T_e   |  T_d   |  T_k   | w/o Dec-Key |   w/Dec-Key
    ---------------+--------+--------+--------+-------------+---------------
    Binary Heap    |O(log N)|O(log N)|O(log N)| O(M log N)  |   O(M log N)
    Binomial Heap  |O(log N)|O(log N)|O(log N)| O(M log N)  |   O(M log N)
    Fibonacci Heap |  O(1)  |O(log N)|  O(1)  | O(M log N)  | O(M + N log N)
    

    As you can see, with most types of priority queues, there really isn’t a difference in the asymptotic runtime, and the decrease-key version isn’t likely to do much better. However, if you use a Fibonacci heap implementation of the priority queue, then indeed Dijkstra’s algorithm will be asymptotically more efficient when using decrease-key.

    In short, using decrease-key, plus a good priority queue, can drop the asymptotic runtime of Dijkstra’s beyond what’s possible if you keep doing enqueues and dequeues.

    Besides this point, some more advanced algorithms, such as Gabow’s Shortest Paths Algorithm, use Dijkstra’s algorithm as a subroutine and rely heavily on the decrease-key implementation. They use the fact that if you know the range of valid distances in advance, you can build a super efficient priority queue based on that fact.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In graph theory, what is the distinction between minimal distance (which Dijkstra's algorithm finds),
I am aware that Dijkstra's algorithm can find the minimum distance between two nodes
I am trying to understand why Dijkstra's algorithm will not work with negative weights.
I have been fighting all day in understanding Dijkstra's algorithm and implementing with no
I have a program that has to find the shortest path (Dijkstra's algorithm), and
Dijkstra's Shunting Yard algorithm is used to parse an infix notation and generate RPN
Dijkstra(G,w,s) { ISS(G,s); let S be an empty set let Q be a priority
I understand what Dijkstra's algorithm is, but I don't understand why it works. When
I'm writing an application that uses Dijkstra algorithm to find minimal paths in the
just a quick query about safe/unsafe states in Dijkstra's Banker's algorithm ... If one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.