Dijkstra's algorithm
medium 22 minDijkstra's algorithm finds the shortest distance from one source to every node of a graph whose edge weights are non-negative.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
Which data structure makes Dijkstra efficient?
What is the time complexity of Dijkstra with a binary heap?
Dijkstra can give wrong answers when edges have:
What does relaxing an edge mean?
In the simple graph (A–B 4, A–C 2, B–C 1, ...), which node is settled second, and what is B's distance after it?
The heap pops (4, B) but dist[B] is already 3. What should the code do?
Directed edges A→B 2, A→C 3, C→B −2, B→D 1. What does textbook Dijkstra (with a settled set) report for D?
Final prev map: B←C, C←A, D←B, E←D, F←E. What is the shortest route from A to F?
A graph has 2,000 nodes and nearly every pair is connected. Which version is the better fit?
You only need the distance to target T. When is it safe to return?
Pick an answer to see why.
Not quite. It must repeatedly extract the unsettled node with the smallest tentative distance; a min-heap does that in O(log V).
Not quite. At most E + 1 pushes and as many pops, each O(log V). O(V²) is the array version; O(V + E) is BFS.
Not quite. The greedy proof needs every edge to add a cost of at least 0. Zero, large and fractional weights are all fine.
Not quite. Relaxing (u, v) checks whether dist[u] + w beats dist[v], and if so lowers dist[v] and sets prev[v] = u.
Not quite. After A, the heap holds (2, C) and (4, B). C is popped, and C–B gives 2 + 1 = 3 < 4.
Not quite. 4 > 3 means a better entry for B was already processed. Skipping stale entries is the lazy-deletion rule.
Not quite. It settles B at 2 and sets D = 3. C→B would give B = 1 and D = 2, but B is already final. The true answer is 2.
Not quite. Walk back F, E, D, B, C, A and reverse.
Not quite. With E close to V², the heap costs about V² log V, while scanning an array for the minimum costs V² in total.
Not quite. A pushed distance can still improve, as F did from 14 to 13. Popping T as the minimum makes it final.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What does Dijkstra's algorithm compute?
The shortest distance from one source to every node in a graph with non-negative edge weights (and, with prev, the paths themselves).
Say your answer, then reveal it.
What does it mean to relax edge (u, v)?
If dist[u] + w(u, v) < dist[v], set dist[v] = dist[u] + w(u, v) and prev[v] = u: update the distance when a shorter path is found.
Say your answer, then reveal it.
When is a node's distance final?
When it is popped from the min-heap as a non-stale entry. Not when it is first pushed: that value can still improve.
Say your answer, then reveal it.
What is a stale heap entry?
An old (distance, node) pair left behind after the node's distance improved. On pop, d > dist[node] identifies it and it is skipped.
Say your answer, then reveal it.
Time and space with a binary heap?
O((V + E) log V) time. O(V + E) space: graph, dist, prev, and up to O(E) heap entries with lazy deletion.
Say your answer, then reveal it.
Array version and Fibonacci heap version?
Array: O(V²), best for dense graphs. Fibonacci heap: O(E + V log V), mostly theoretical.
Say your answer, then reveal it.
Why do negative edges break it?
A popped node is declared final assuming later routes only add cost. A negative edge can make a later route cheaper. Use Bellman-Ford, O(V·E).
Say your answer, then reveal it.
How do you rebuild the shortest path?
Record prev[v] = u on every improvement. From the target follow prev to the source, then reverse the list.
Say your answer, then reveal it.
Dijkstra versus BFS?
Same loop; BFS uses a FIFO queue (hop order, unweighted graphs), Dijkstra uses a min-heap (distance order, weighted graphs).
Say your answer, then reveal it.
When can you stop early?
When the target is popped (not stale). Its distance is final at that moment.
Say your answer, then reveal it.
Why push (distance, node) and not (node, distance)?
heapq compares tuples from the first field, so the distance must come first for the heap to pop the closest node.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.