Single Source Shortest Paths Dijkstra’s Alg. (no negative lengths!) f 8 4 1 2 b 9 7 2 1 3 source a b c e f 8 9 4 1 2 3 7 What is a shortest path from a to b ? a -> b = 8 a -> f -> c -> b = 7
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source Initially: S, the set containing vertices for which we already know their distance from the source, is empty. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π - empty 0, NIL , NIL
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source Step 1: Add the source, vertex a, to vertex set S, and relax the edges that leave vertex a. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 1 a (0,NIL) 8, a , NIL 9, a 4, a
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source Step 2: Add vertex f to vertex set S as it has minimum d-value in Q=V-S, and relax the edges that leave f. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 1 a 0, NIL 8, a , NIL 9, a 4, a 2 f a, f (0,NIL) 8, a 5, f 9, a (4, a)
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source Step 3: Add vertex c to vertex set S as it has minimum d-value in Q=V-S, and relax the edges that leave c. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 1 a 0, NIL 8, a , NIL 9, a 4, a 2 f a, f (0,NIL) 8, a 5, f 9, a (4, a) 3 c a, f, c (0,NIL) 7, c (5, f) 8, c (4, a)
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source Step 4: Add vertex b to vertex set S as it has minimum d-value in Q=V-S, and relax the edges that leave b. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 1 a 0, NIL 8, a , NIL 9, a 4, a 2 f a, f (0,NIL) 8, a 5, f 9, a (4, a) 3 c a, f, c (0,NIL) 7, c (5, f) 8, c (4, a) 4 b a, f, c, b (0,NIL) (7, c) (5, f) 8, c (4, a)
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source Step 5: Add vertex e to vertex set S as it has minimum d-value in Q=V-S, and relax the edges that leave e. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 1 a 0, NIL 8, a , NIL 9, a 4, a 2 f a, f (0,NIL) 8, a 5, f 9, a (4, a) 3 c a, f, c (0,NIL) 7, c (5, f) 8, c (4, a) 4 b a, f, c, b (0,NIL) (7, c) (5, f) 8, c (4, a) 5 e a, f, c, b, e (0,NIL) (7, c) (5, f) (8, c) (4, a)
Shortest Paths - Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source After Step 5: As Q=V-S is now empty, the algorithm terminates. attributes d and π step v vertex set S a.d, a.π b.d, b.π c.d, c.π e.d, e.π f.d, f. π 1 a 0, NIL 8, a , NIL 9, a 4, a 2 f a, f (0,NIL) 8, a 5, f 9, a (4, a) 3 c a, f, c (0,NIL) 7, c (5, f) 8, c (4, a) 4 b a, f, c, b (0,NIL) (7, c) (5, f) 8, c (4, a) 5 e a, f, c, b, e (0,NIL) (7, c) (5, f) (8, c) (4, a)
found by Dijkstra’s Algorithm The Shortest Path Tree rooted at a found by Dijkstra’s Algorithm 1 2 a e c f 8 4 b 9 7 2 1 3 source