We keep an array dist[] where dist[v] is our current best known distance. It starts as ∞ for everyone except dist[s]=0, and we shrink it as we discover shorter routes (relaxation).
Claim (the invariant we must prove): When we pop the vertex u with the smallest tentative distance from the frontier, dist[u] is already the true shortest distance.
dijkstra(G, s):
dist[v] = INF for all v; dist[s] = 0
PQ = min-heap with (0, s)
while PQ not empty:
(d, u) = PQ.pop_min()
if d > dist[u]: continue # stale entry, skip
for each edge (u, v, w):
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
PQ.push((dist[v], v)) # lazy: leaves old entry behind
return dist
Graph (directed):
A→B(4), A→C(1), C→B(2), C→D(5), B→D(1), source A.
Step
Pop
dist updates
dist after
init
—
A=0
A0 B∞ C∞ D∞
1
A(0)
C: 0+1=1, B: 0+4=4
A0 B4 C1 D∞
2
C(1)
B: 1+2=3<4 ✓, D: 1+5=6
A0 B3 C1 D6
3
B(3)
D: 3+1=4<6 ✓
A0 B3 C1 D4
4
D(4)
none
done
Why pop C before B? After step 1, the heap has B(4) and C(1). We pop the minimum, C. This greedy order is what guarantees correctness — and it's why C's relaxation later improves B from 4 to 3.
Recall Feynman: explain to a 12-year-old
You're at home and want the fastest time to reach every house in town. You start a timer. Roads take different minutes to walk. You keep a "best time so far" sticky note on each house. You always go to the nearest house you haven't finalized yet, because once you reach it the fast way, no slower house can give you a shortcut (all roads cost positive time — there's no magic road that gives back minutes). You stamp it "done" and update its neighbors' sticky notes. Repeat. The priority queue is just a smart way to always grab the nearest not-done house quickly.
Single-source shortest paths from a source s to all vertices in a graph with non-negative edge weights.
What is "relaxation"?
Updating dist[v]=min(dist[v],dist[u]+w(u,v)) when going through u is cheaper.
Why does Dijkstra require non-negative edges?
The correctness proof uses "the rest of the path has weight ≥ 0" so the popped min-distance vertex is already final; negative edges break this since a longer path could become cheaper.
When is a vertex's distance finalized?
When it is popped from the priority queue with a non-stale distance (not when it is pushed/relaxed).
What is the time complexity with a binary heap?
O((V+E)logV).
What complexity does a Fibonacci heap give?
O(E+VlogV) via amortized O(1) decrease-key.
What does the line if d > dist[u]: continue do?
Skips stale heap entries in the lazy-deletion approach (a vertex pushed multiple times; only its first/best pop matters).
Which algorithm to use for negative edges?
Bellman-Ford, O(VE) (or Johnson's for all-pairs).
Which algorithm for unweighted shortest path?
BFS, O(V+E) — no heap needed.
In the greedy invariant proof, what is y?
The first unfinalized vertex on the true shortest path to u; already relaxed via finalized predecessor x.
Dijkstra ka basic idea simple hai: source se paani chhodo, jo city pehle reach hoti hai uska shortest distance utna hi final ho gaya. Hum ek dist[] array rakhte hain — shuru me sab infinity, source ka 0. Phir priority queue (min-heap) se hamesha sabse closest unfinalized vertex nikalte hain, use "done" mark karte hain, aur uske neighbours ko relax karte hain: agar dist[u] + w < dist[v] ho to update kar do.
Greedy kyun chalta hai? Kyunki edges non-negative hain. Jab tum minimum distance wala vertex pop karte ho, koi bhi lamba raasta use sasta nahi bana sakta — kyunki extra edges sirf weight badhayenge, ghatayenge nahi. Yahi reason hai ki negative edge aate hi Dijkstra galat ho jata hai (tab Bellman-Ford use karo). Proof me bas ek hi jagah "≥ 0" use hota hai — wahi sab kuch hold karta hai.
Code me ek important trick hai: lazy heap. Decrease-key ki jagah hum naya (new_dist, v) push kar dete hain, purana stale entry heap me reh jata hai. Isliye if d > dist[u]: continue line lagao — stale pop ko skip karne ke liye. Vertex tab finalize hota hai jab pop hota hai, push pe nahi.
Complexity: har edge ek push de sakta hai, to heap me O(E) entries, har operation O(logV), total O((V+E)logV). Fibonacci heap se O(E+VlogV). Exam aur interview dono me yeh standard answer hai — yaad rakho aur derive bhi kar pao.