Presentation is loading. Please wait.

Presentation is loading. Please wait.

D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University.

Similar presentations


Presentation on theme: "D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University."— Presentation transcript:

1 D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University

2 S HORTEST P ATH Given a graph G, a source vertex s, and a destination vertex d. Find a shortest path between s and d. Unweighted Graph Can be solved using simple BFS from s Weighted Graph Bellman-Ford Algorithm (single source) Dijkstra’s Algorithm (no negative cycle) Floyd-Warshall Algorithm (all-pairs shortest path) etc.

3 S INGLE S OURCE S HORTEST P ATH Given a graph G and a source vertex s. Find the shortest path from s to all other vertices. Can solve one-pair shortest path problem. Very similar to Prim’s MST Algorithm

4 R ECALL, P RIM ’ S MST At each step, a light edge is added to the tree A that connects A to an isolated vertex A b d a c 14 8 5 22 Light edge ≡ edge with smallest weight that connects A to an isolated vertex

5 D IJKSTRA ’ S A LGORITHM Maintain a set A of vertices whose shortest path (from source vertex s ) is already known a b d c s 4 8 2 3

6 D IJKSTRA ’ S A LGORITHM On each iteration, pick a vertex u  S with the smallest shortest path estimate b’ d’ a’ c’ 14 8 22 5 a b d c s 4 8 2 3 sse = 2+22 = 24 sse = 3+8 = 11 sse = 8+5 = 13 sse = 4+14 = 18

7 S HORTEST PATH ESTIMATE d’ 8 5 a b c s 8 2 3 4 sse is calculated as minimum of 2+4 = 6 3+8 = 11 8+5 = 13 = 6 sse is calculated as minimum of 2+4 = 6 3+8 = 11 8+5 = 13 = 6

8 E XAMPLE a0a0 bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10 A

9 E XAMPLE a0a0 b4b4 c i h8h8 gf d e 8 8 11 4 7 4 2 7 6 12 14 9 10 A

10 E XAMPLE a0a0 b4b4 c 12 i h8h8 gf d e 8 8 11 4 7 4 2 7 6 12 14 9 10 A distance(a,h)via b is 4+11, which is worse than directly a-h (8)

11 E XAMPLE a0a0 b4b4 c 12 i 15 h8h8 g9g9 f d e 8 8 11 4 7 4 2 7 6 12 14 9 10 A

12 E XAMPLE a0a0 b4b4 c 12 i 15 h8h8 g9g9 f 11 d e 8 8 4 7 4 2 7 6 12 14 9 10 A distance(a,i) via g is 9+6, which is equally good as via h (8+7)

13 E XAMPLE a0a0 b4b4 c 12 i 15 h8h8 g9g9 f 11 d 25 e 21 8 8 11 4 7 4 2 7 6 12 14 9 10 A distance(a,c)via f is 11+4, which is worse than via c (12)

14 E XAMPLE a0a0 b4b4 c 12 i 14 h8h8 g9g9 f 11 d 19 e 21 8 8 11 4 7 4 2 7 6 12 14 9 10 A distance(a,i) via c is 12+2, which is better than via h (15) distance(a,d) via c is 12+7, which is better than via f (25)

15 E XAMPLE a0a0 b4b4 c 12 i 14 h8h8 g9g9 f 11 d 19 e 21 8 8 11 4 7 4 2 7 6 12 14 9 10 A

16 E XAMPLE a0a0 b4b4 c 12 i 14 h8h8 g9g9 f 11 d 19 e 21 8 8 11 4 7 4 2 7 6 12 14 9 10 A distance(a,e) via d is 19+9, which is worse than via f (21)

17 E XAMPLE a0a0 b4b4 c 12 i 14 h8h8 g9g9 f 11 d 19 e 21 8 8 11 4 7 4 2 7 6 12 14 9 10 A

18 P SEUDOCODE 1. Dijkstra-SSSP(G, w, s) 2. for each u  V do 3. dist[u] =  4. parent[u] = NIL 5. dist[s] = 0 6. Q = all vertices of G 7. While Q ≠  do 8. u = EXTRACT-MIN(Q) 9. for each v adjacent to u do 10. if v  Q and dist[u]+w(u,v)<dist[v] then 11. parent[v] = u 12. dist[v] = dist[u]+w(u,v) Initialization : Set all vertices’ dist to  except the source, so that it will be the first vertex processed. Parent of each vertex is set to NIL. Min-priority queue Q contains all vertices.

19 I MPLEMENTATION The performance of Dijkstra’s Algorithm depends on how we implement the min-priority queue Q (very similar analysis to Prim’s MST) Suppose we implement Q using a min-heap: Store vertices’ id on the heap’s array Store vertices’ dist on a separate array Store vertices’ location on a separate array Additionally, we need an array parent to store the resulting tree


Download ppt "D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University."

Similar presentations


Ads by Google