1. Gyanastra
  2. Algorithms
  3. Dijkstra's Algorithm: Full Trace, Correctness and Running Times
  4. Full Trace on a Six-Vertex Graph
Free lesson, no account needed 11 min read
Asked in past papers2 times
  • GATE CSE20041996

Algorithms · Dijkstra's Algorithm: Full Trace, Correctness and Running Times

Full Trace on a Six-Vertex Graph

Take this directed graph (0-indexed), source s=0s=0:

0 -> 1 (4)     0 -> 2 (1)
1 -> 3 (2)     2 -> 3 (3)     2 -> 5 (7)
3 -> 4 (1)     5 -> 4 (5)

Edges and weights:

EdgeWeight
0→14
0→21
2→33
3→41
2→57
5→45
1→32

Initial state: d=[0,∞,∞,∞,∞,∞]d=[0,\infty,\infty,\infty,\infty,\infty], S=∅S=\varnothing, Q={0:0, 1:∞, 2:∞, 3:∞, 4:∞, 5:∞}Q=\{0{:}0,\ 1{:}\infty,\ 2{:}\infty,\ 3{:}\infty,\ 4{:}\infty,\ 5{:}\infty\} (key = current dd).

I will show, after each extraction, the extracted vertex, the relaxations performed (with the arithmetic), and the resulting dd array and QQ.


Extraction 1 — min key is vertex 0 (d=0d=0). S={0}S=\{0\}. Relax outgoing edges of 0:

  • (0,1)(0,1): d[0]+4=0+4=4<∞d[0]+4 = 0+4 = 4 < \infty → d[1]=4d[1]=4, π[1]=0\pi[1]=0.
  • (0,2)(0,2): d[0]+1=0+1=1<∞d[0]+1 = 0+1 = 1 < \infty → d[2]=1d[2]=1, π[2]=0\pi[2]=0.

d=[0,4,1,∞,∞,∞]d=[0,4,1,\infty,\infty,\infty]. Q={1:4, 2:1, 3:∞, 4:∞, 5:∞}Q=\{1{:}4,\ 2{:}1,\ 3{:}\infty,\ 4{:}\infty,\ 5{:}\infty\}.


Extraction 2 — min key is vertex 2 (d=1d=1). S={0,2}S=\{0,2\}. Relax outgoing edges of 2:

  • (2,3)(2,3): d[2]+3=1+3=4<∞d[2]+3 = 1+3 = 4 < \infty → d[3]=4d[3]=4, π[3]=2\pi[3]=2.
  • (2,5)(2,5): d[2]+7=1+7=8<∞d[2]+7 = 1+7 = 8 < \infty → d[5]=8d[5]=8, π[5]=2\pi[5]=2.

d=[0,4,1,4,∞,8]d=[0,4,1,4,\infty,8]. Q={1:4, 3:4, 4:∞, 5:8}Q=\{1{:}4,\ 3{:}4,\ 4{:}\infty,\ 5{:}8\}.


Extraction 3 — min key is vertex 1 (d=4d=4). (Tie with vertex 3 at 4; either order is valid — I take 1 first.) S={0,2,1}S=\{0,2,1\}. Relax outgoing edges of 1:

  • (1,3)(1,3): d[1]+2=4+2=6d[1]+2 = 4+2 = 6. Is 6<d[3]=46 < d[3]=4? No → no change.

d=[0,4,1,4,∞,8]d=[0,4,1,4,\infty,8]. Q={3:4, 4:∞, 5:8}Q=\{3{:}4,\ 4{:}\infty,\ 5{:}8\}.


Extraction 4 — min key is vertex 3 (d=4d=4). S={0,2,1,3}S=\{0,2,1,3\}. Relax outgoing edges of 3:

  • (3,4)(3,4): d[3]+1=4+1=5<∞d[3]+1 = 4+1 = 5 < \infty → d[4]=5d[4]=5, π[4]=3\pi[4]=3.

d=[0,4,1,4,5,8]d=[0,4,1,4,5,8]. Q={4:5, 5:8}Q=\{4{:}5,\ 5{:}8\}.


Extraction 5 — min key is vertex 4 (d=5d=5). S={0,2,1,3,4}S=\{0,2,1,3,4\}. Vertex 4 has no outgoing edges. No relaxations.

d=[0,4,1,4,5,8]d=[0,4,1,4,5,8]. Q={5:8}Q=\{5{:}8\}.


Extraction 6 — min key is vertex 5 (d=8d=8). S={0,2,1,3,4,5}S=\{0,2,1,3,4,5\}. Relax outgoing edges of 5:

  • (5,4)(5,4): d[5]+5=8+5=13d[5]+5 = 8+5 = 13. Is 13<d[4]=513 < d[4]=5? No → no change.

d=[0,4,1,4,5,8]d=[0,4,1,4,5,8]. Q=∅Q=\varnothing. Done.


Final distances: δ(0,0)=0\delta(0,0)=0, δ(0,1)=4\delta(0,1)=4, δ(0,2)=1\delta(0,2)=1, δ(0,3)=4\delta(0,3)=4, δ(0,4)=5\delta(0,4)=5, δ(0,5)=8\delta(0,5)=8.

Shortest-path tree (via π\pi): 0→10\to1, 0→20\to2, 2→32\to3, 3→43\to4, 2→52\to5.

0
├──1
└──2
   ├──3
   │  └──4
   └──5

Sanity check of the key path 0→2→3→40\to2\to3\to4: 1+3+1=51+3+1=5 ✓. The alternative 0→1→3→40\to1\to3\to4: 4+2+1=74+2+1=7 (worse). The alternative 0→2→5→40\to2\to5\to4: 1+7+5=131+7+5=13 (worse). So 55 is indeed minimal. ✓

Extraction order: 0,2,1,3,4,50,2,1,3,4,5 — note it is not sorted by vertex number; it is sorted by final distance (with ties broken arbitrarily). This is the single most common place students lose marks: the order is by dd-value, not by label.


Priority-Queue Behaviour (what the exam wants you to show)

The table below is the "distance array + PQ after every extraction" summary. Keys are current dd values; a key of ∞\infty means "still in QQ, not yet reached".

StepExtractd[0]d[0]d[1]d[1]d[2]d[2]d[3]d[3]d[4]d[4]d[5]d[5]QQ after (key:vertex)
init—0∞∞∞∞∞0:0, ∞:1, ∞:2, ∞:3, ∞:4, ∞:5
10041∞∞∞4:1, 1:2, ∞:3, ∞:4, ∞:5
220414∞84:1, 4:3, ∞:4, 8:5
310414∞84:3, ∞:4, 8:5
430414585:4, 8:5
540414588:5
65041458∅

Decrease-key count: each successful relaxation is one decrease-key. In this trace there were 5 successful relaxations (edges 0→1,0→2,2→3,2→5,3→40\to1,0\to2,2\to3,2\to5,3\to4) and 2 failed ones (1→31\to3, 5→45\to4). This matters for the heap analysis in §7.


Correctness Argument

Claim: when Dijkstra extracts uu from QQ, d[u]=δ(s,u)d[u]=\delta(s,u).

Proof (exchange / greedy-choice argument). Suppose, for contradiction, that uu is the first vertex extracted with d[u]>δ(s,u)d[u]>\delta(s,u). Consider a true shortest path PP from ss to uu. Let yy be the first vertex on PP that is still in QQ when uu is extracted (such a yy exists: ss is already finalised, and uu itself is in QQ). Let xx be the predecessor of yy on PP; then x∈Sx\in S (already finalised).

  • Since xx was finalised and its edges were relaxed, d[y]≤d[x]+w(x,y)=δ(s,x)+w(x,y)d[y]\le d[x]+w(x,y)=\delta(s,x)+w(x,y) (using the induction hypothesis d[x]=δ(s,x)d[x]=\delta(s,x)).
  • Since yy lies on a shortest path to uu, δ(s,x)+w(x,y)=δ(s,y)≤δ(s,u)\delta(s,x)+w(x,y)=\delta(s,y)\le\delta(s,u) (sub-path optimality, and the remaining part of PP has non-negative weight).
  • Therefore d[y]≤δ(s,u)<d[u]d[y]\le\delta(s,u)<d[u].

But uu was chosen as the minimum key in QQ, so d[u]≤d[y]d[u]\le d[y]. Contradiction. Hence d[u]=δ(s,u)d[u]=\delta(s,u). ■\blacksquare

Where non-negativity is used: in the step δ(s,y)≤δ(s,u)\delta(s,y)\le\delta(s,u), we need the suffix of PP from yy to uu to have weight ≥0\ge 0. If a negative edge sits on that suffix, δ(s,y)\delta(s,y) could exceed δ(s,u)\delta(s,u), the inequality chain breaks, and the greedy choice is no longer safe. That is precisely the failure in §6.


Why Negative Edges Break It — Worked Counterexample

Take three vertices, source AA:

EdgeWeight
A→B1
A→C2
C→B−2

True shortest paths: δ(A,B)\delta(A,B): path A→C→BA\to C\to B has weight 2+(−2)=02+(-2)=0, which beats A→B=1A\to B=1. So δ(A,B)=0\delta(A,B)=0. And δ(A,C)=2\delta(A,C)=2.

Run Dijkstra: init d=[A:0, B:∞, C:∞]d=[A{:}0,\ B{:}\infty,\ C{:}\infty].

  • Extract A (d=0d=0). Relax: (A,B)(A,B): 0+1=1<∞0+1=1<\infty → d[B]=1d[B]=1. (A,C)(A,C): 0+2=2<∞0+2=2<\infty → d[C]=2d[C]=2. Now d=[0,1,2]d=[0,1,2], Q={B:1, C:2}Q=\{B{:}1,\ C{:}2\}.
  • Extract B (d=1d=1). B has no outgoing edges. d=[0,1,2]d=[0,1,2], Q={C:2}Q=\{C{:}2\}.
  • Extract C (d=2d=2). Relax (C,B)(C,B): d[C]+(−2)=2−2=0d[C]+(-2)=2-2=0. Is 0<d[B]=10<d[B]=1? Yes — but B is already finalised, so Dijkstra never revisits it. The algorithm reports d[B]=1d[B]=1.

Result: Dijkstra outputs δ(A,B)=1\delta(A,B)=1, but the truth is 00. Wrong by 1. The greedy choice of BB (key 1) over CC (key 2) was unsafe because the negative edge C→BC\to B could later undercut it — exactly the inequality that failed in §5.

Exam takeaway: Dijkstra requires w≥0w\ge 0 on every edge. If any edge is negative, use Bellman-Ford (O(VE)O(VE)) or, for a DAG, the topological-order method (§9), which tolerates negative edges.


Running Times — All Three Implementations

The total work is: VV extractions + (at most) EE relaxations, each relaxation possibly a decrease-key. The cost depends on the priority-queue data structure.

ImplementationExtract-min (×V)Decrease-key (×E)TotalWhen to use
Array / linear scanO(V)O(V) each → O(V2)O(V^2)O(1)O(1) each → O(E)O(E)O(V2+E)=O(V2)O(V^2+E)=O(V^2)Dense graphs (E≈V2E\approx V^2)
Binary heapO(log⁡V)O(\log V) each → O(Vlog⁡V)O(V\log V)O(log⁡V)O(\log V) each → O(Elog⁡V)O(E\log V)O((V+E)log⁡V)O((V+E)\log V)Sparse graphs
Fibonacci heapO(log⁡V)O(\log V) amortised → O(Vlog⁡V)O(V\log V)O(1)O(1) amortised → O(E)O(E)O(E+Vlog⁡V)O(E+V\log V)Theoretical best; rarely implemented

Deriving each:

  • Array: scanning the whole array to find the min costs O(V)O(V); done VV times gives O(V2)O(V^2). Relaxation is a direct array write, O(1)O(1), total O(E)O(E). Sum =O(V2+E)=O(V^2+E). Since E≤V2E\le V^2, this is O(V2)O(V^2).
  • Binary heap: each of VV extract-mins is O(log⁡V)O(\log V) → O(Vlog⁡V)O(V\log V). Each of ≤E\le E decrease-keys is O(log⁡V)O(\log V) → O(Elog⁡V)O(E\log V). Sum =O((V+E)log⁡V)=O((V+E)\log V).
  • Fibonacci heap: extract-min is O(log⁡V)O(\log V) amortised, decrease-key is O(1)O(1) amortised. Sum =O(Vlog⁡V+E)=O(E+Vlog⁡V)=O(V\log V+E)=O(E+V\log V).

Choosing in an exam: if the question says "dense graph" or gives E=Θ(V2)E=\Theta(V^2), the array version O(V2)O(V^2) is optimal and simplest. If it says "sparse" or E=O(V)E=O(V), the binary heap O((V+E)log⁡V)O((V+E)\log V) wins. Fibonacci heap is the asymptotically best general bound but has large constants.

Worked numeric check: V=1000V=1000, E=5000E=5000 (sparse). Array: V2=106V^2=10^6. Binary heap: (V+E)log⁡2V=(1000+5000)×10=60000(V+E)\log_2 V=(1000+5000)\times 10=60000. Heap is far better. Now V=1000V=1000, E=500000E=500000 (dense): array =106=10^6; heap =(1000+500000)×10≈5.01×106=(1000+500000)\times10\approx5.01\times10^6. Array wins. This is the standard GATE comparison.


Unweighted Shortest Paths — BFS

When every edge has weight 11 (or the graph is unweighted), Dijkstra degenerates to breadth-first search, and we can drop the priority queue entirely.

Method: use a FIFO queue. Set d[s]=0d[s]=0, all others ∞\infty. Repeatedly dequeue uu, and for each neighbour vv with d[v]=∞d[v]=\infty, set d[v]=d[u]+1d[v]=d[u]+1 and enqueue vv.

Why it is correct: BFS explores vertices in non-decreasing order of distance from ss (level by level), which is exactly Dijkstra's extraction order when all weights are equal. The FIFO queue is the priority queue, because keys are inserted in sorted order automatically.

Running time: O(V+E)O(V+E) — each vertex enqueued once, each edge scanned once. This is strictly better than any Dijkstra variant, so always use BFS for unweighted graphs.

Tiny check: on the §3 graph ignoring weights, from 0: level 0 = {0}; level 1 = {1,2}; level 2 = {3,5}; level 3 = {4}. So d=[0,1,1,2,3,2]d=[0,1,1,2,3,2]. (Different from the weighted answer, as expected.)


Shortest Paths in a DAG — Topological Order, O(V+E)O(V+E)

If the graph is a DAG (directed acyclic), we can compute shortest paths in O(V+E)O(V+E) even with negative edges, because there are no cycles for a negative edge to exploit.

Method:

  1. Topologically sort VV (Kahn's algorithm or DFS, O(V+E)O(V+E)).
  2. Initialise d[s]=0d[s]=0, others ∞\infty.
  3. Process vertices in topological order; for each uu, relax all outgoing edges (u,v)(u,v).

Why it works: in topological order, every predecessor of uu is processed before uu, so by the time we reach uu, d[u]d[u] is already final — no priority queue, no re-extraction needed. Each edge relaxed exactly once.

Running time: O(V+E)O(V+E) for the sort plus O(V+E)O(V+E) for the relaxation =O(V+E)=O(V+E).

Negative edges are fine here: the §6 counterexample graph A→B,A→C,C→BA\to B, A\to C, C\to B is a DAG (topological order A,C,BA,C,B). Processing in that order: d[A]=0d[A]=0; relax AA: d[B]=1d[B]=1, d[C]=2d[C]=2; process CC: relax C→BC\to B: 2+(−2)=0<12+(-2)=0<1 → d[B]=0d[B]=0; process BB. Final d[B]=0d[B]=0 — correct, unlike Dijkstra. This is the key contrast to remember.


Pages 1–2 of 2