Step 1 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F 0 0 inf 1 inf 2 inf 3 inf 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Dijkstra's algorithm on an undirected weighted graph. Source = A A A . Cells 0..5 0..5 0..5 of the d i s t dist d i s t array track the tentative distance to A , B , C , D , E , F A, B, C, D, E, F A , B , C , D , E , F respectively. Initialise d i s t [ A ] = 0 dist[A] = 0 d i s t [ A ] = 0 ; every other node stays at ∞ \infty ∞ . Push A A A into the priority queue.
Step 2 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F 0 0 4 1 inf 2 inf 3 inf 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Settle A A A . Relax edge A → B A\to B A → B with weight 4 4 4 : d i s t [ B ] = min ( ∞ , 0 + 4 ) = 4 dist[B] = \min(\infty, 0+4) = 4 d i s t [ B ] = min ( ∞ , 0 + 4 ) = 4 . Update and keep scanning from A A A .
Step 3 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F 0 0 4 1 2 2 inf 3 inf 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge A → C A\to C A → C with weight 2 2 2 : d i s t [ C ] = min ( ∞ , 0 + 2 ) = 2 dist[C] = \min(\infty, 0+2) = 2 d i s t [ C ] = min ( ∞ , 0 + 2 ) = 2 . A A A has no more outgoing edges. The smallest tentative distance in the queue is now d i s t [ C ] = 2 dist[C] = 2 d i s t [ C ] = 2 .
Step 4 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F 0 0 4 1 2 2 inf 3 inf 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Pop C C C (key = 2 = 2 = 2 ). Settle C C C : this is the final distance because all edge weights are non-negative.
Step 5 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 0 0 3 1 2 2 inf 3 inf 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge C → B C\to B C → B with weight 1 1 1 : d i s t [ B ] = min ( 4 , 2 + 1 ) = 3 dist[B] = \min(4, 2+1) = 3 d i s t [ B ] = min ( 4 , 2 + 1 ) = 3 . The detour A → C → B A \to C \to B A → C → B is cheaper than the direct A → B A \to B A → B edge.
Step 6 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 0 0 3 1 2 2 10 3 inf 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge C → D C\to D C → D with weight 8 8 8 : d i s t [ D ] = min ( ∞ , 2 + 8 ) = 10 dist[D] = \min(\infty, 2+8) = 10 d i s t [ D ] = min ( ∞ , 2 + 8 ) = 10 . Tentative only – a shorter path through B B B may appear.
Step 7 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 0 0 3 1 2 2 10 3 12 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge C → E C\to E C → E with weight 10 10 10 : d i s t [ E ] = min ( ∞ , 2 + 10 ) = 12 dist[E] = \min(\infty, 2+10) = 12 d i s t [ E ] = min ( ∞ , 2 + 10 ) = 12 . Still tentative.
Step 8 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 0 0 3 1 2 2 10 3 12 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Pop B B B (key = 3 = 3 = 3 ). Settle B B B . The queue now holds D = 10 D=10 D = 10 and E = 12 E=12 E = 12 .
Step 9 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 0 0 3 1 2 2 8 3 12 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge B → D B\to D B → D with weight 5 5 5 : d i s t [ D ] = min ( 10 , 3 + 5 ) = 8 dist[D] = \min(10, 3+5) = 8 d i s t [ D ] = min ( 10 , 3 + 5 ) = 8 . The new predecessor of D D D is B B B , not C C C .
Step 10 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 0 0 3 1 2 2 8 3 12 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Pop D D D (key = 8 = 8 = 8 ). Settle D D D .
Step 11 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 Arrow from G.node[D] to G.node[E]: 8+2=10 8+2=10 0 0 3 1 2 2 8 3 10 4 inf 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge D → E D\to E D → E with weight 2 2 2 : d i s t [ E ] = min ( 12 , 8 + 2 ) = 10 dist[E] = \min(12, 8+2) = 10 d i s t [ E ] = min ( 12 , 8 + 2 ) = 10 . Improved via A → C → B → D → E A\to C\to B\to D\to E A → C → B → D → E .
Step 12 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 Arrow from G.node[D] to G.node[E]: 8+2=10 8+2=10 0 0 3 1 2 2 8 3 10 4 14 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge D → F D\to F D → F with weight 6 6 6 : d i s t [ F ] = min ( ∞ , 8 + 6 ) = 14 dist[F] = \min(\infty, 8+6) = 14 d i s t [ F ] = min ( ∞ , 8 + 6 ) = 14 . Tentative.
Step 13 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 Arrow from G.node[D] to G.node[E]: 8+2=10 8+2=10 0 0 3 1 2 2 8 3 10 4 14 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Pop E E E (key = 10 = 10 = 10 ). Settle E E E . Only F F F remains in the queue with key 14 14 14 .
Step 14 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 Arrow from G.node[D] to G.node[E]: 8+2=10 8+2=10 0 0 3 1 2 2 8 3 10 4 13 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Relax edge E → F E\to F E → F with weight 3 3 3 : d i s t [ F ] = min ( 14 , 10 + 3 ) = 13 dist[F] = \min(14, 10+3) = 13 d i s t [ F ] = min ( 14 , 10 + 3 ) = 13 . One last improvement.
Step 15 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 Arrow from G.node[D] to G.node[E]: 8+2=10 8+2=10 0 0 3 1 2 2 8 3 10 4 13 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Pop F F F (key = 13 = 13 = 13 ). Settle F F F . The priority queue is empty – all nodes are settled.
Step 16 / 16 Edge from node A to node B 4 Edge from node A to node C 2 Edge from node B to node C 1 Edge from node B to node D 5 Edge from node C to node D 8 Edge from node C to node E 10 Edge from node D to node E 2 Edge from node D to node F 6 Edge from node E to node F 3 A B C D E F Arrow from G.node[C] to G.node[B]: 2+1=3 2+1=3 Arrow from G.node[B] to G.node[D]: 3+5=8 3+5=8 Arrow from G.node[D] to G.node[E]: 8+2=10 8+2=10 0 0 3 1 2 2 8 3 10 4 13 5 d i s t [ A , B , C , D , E , F ] dist[A,B,C,D,E,F] d i s t [ A , B , C , D , E , F ] Shortest-path tree rooted at A A A : A → C A\!\to\!C A → C , C → B C\!\to\!B C → B , B → D B\!\to\!D B → D , D → E D\!\to\!E D → E , E → F E\!\to\!F E → F . Final distances: d i s t = [ 0 , 3 , 2 , 8 , 10 , 13 ] dist = [0, 3, 2, 8, 10, 13] d i s t = [ 0 , 3 , 2 , 8 , 10 , 13 ] . Non-tree edges are dimmed; each would form a longer alternative.