Download presentation
Presentation is loading. Please wait.
Published byJörg Mathias Fischer Modified over 5 years ago
1
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Dijkstra’s Example By Joe James
2
Dijkstra Example Problem: Find the shortest distance from source S to all other vertices given in the graph. Step 1: Initialize distances from source (S) to all other vertices. We immedietely see the neighbors A, D,and C of S.
3
Dijkstra Example Step 2: Relax all out-edges from S. Trace the neighbours of S and compute the shortest distance to them Step 3: Choose the closest vertex to move to, which is D, i.e., 5 < 6 < 8.
4
Dijkstra Example Step 4: Relax all out-edges from D
Step 5: Check out (relax) the shortest out-edge out of D, which is D-A = 2.
5
Dijkstra Example Step 6: Add the current distance (S-D) to the shortest out-edge of D, which is D-A = 2. Now the shortest distance from S to A is 7 via vertex D
6
Dijkstra Example Step 8: C is the next nearest vertex from S
C has anly one out-edge remaining, which is C-F. The distance S-F via C is 9+6 and via D is 5+4. Thus, C is also done. Step 7: Process all the distances from S to all out-edges of D.
7
Dijkstra Example Step 9: The next shortest distance from S to its neighbors is now A. Step 10: A has one out-edge (A-E) left. Hence, Distance from S to E over A is 8. A is done and we jump to E, which has two out-edges to relax, E-B and E-G.
8
Dijkstra Example Step 11: We are done with E and move to F, because it is the closest node to the source that we have’nt visited yet. Step 12: F has one out-edge (F-G). So we move to G. G has no edge to relax so we move to B.
9
Dijkstra Example Step 13: G has no edge to relax so we move to B, which also has no edges to relax. So, we are done. This finalizes the shortest path from sorurce S to every other vertices in the weighted graph.
10
Another Example of Dijkstra's Shortest Path Algorithm
Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44
11
Dijkstra's Shortest Path Algorithm
PQ = { s, 2, 3, 4, 5, 6, 7, t } 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label
12
Dijkstra's Shortest Path Algorithm
PQ = { s, 2, 3, 4, 5, 6, 7, t } Here 2 24 3 9 s 18 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 distance label
13
Dijkstra's Shortest Path Algorithm
PQ = { 2, 3, 4, 5, 6, 7, t } Node discovered X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X
14
Dijkstra's Shortest Path Algorithm
PQ = { 2, 3, 4, 5, 6, 7, t } Here X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 distance label 15 X
15
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, 6, 7, t } X 9 2 24 3 9 s 18 14 X 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 15 X
16
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, 6, 7, t } Node discovered X 33 X 9 2 24 3 9 s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X
17
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, 6, 7, t } X 33 X 9 2 24 3 9 Here s 18 14 X 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44 15 X
18
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, 7, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 X
19
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, 7, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 30 X 4 19 11 15 5 5 6 20 16 t 7 44 15 Here X
20
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 4 30 19 11 15 5 5 6 20 16 t 7 44 59 15 X X
21
Dijkstra's Shortest Path Algorithm
PQ = { 3, 4, 5, t } Here 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 4 30 X 19 11 15 5 5 6 20 16 t 7 44 59 15 X X
22
Dijkstra's Shortest Path Algorithm
PQ = { 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 4 30 X 19 11 15 5 5 6 20 16 t 7 44 51 59 15 X X X
23
Dijkstra's Shortest Path Algorithm
PQ = { 4, 5, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 44 X 35 X 34 30 X 4 19 11 15 5 5 6 20 Here 16 t 7 44 51 59 15 X X X
24
Dijkstra's Shortest Path Algorithm
PQ = { 4, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
25
Dijkstra's Shortest Path Algorithm
PQ = { 4, t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 Here 5 6 20 16 t 7 44 50 51 X 59 15 X X X
26
Dijkstra's Shortest Path Algorithm
PQ = { t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
27
Dijkstra's Shortest Path Algorithm
PQ = { t } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X 30 X 4 19 11 15 5 5 6 20 16 t 7 44 Here 50 51 X 59 15 X X X
28
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
29
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 X 33 X X 9 2 24 3 9 s 18 14 X 14 2 6 6 45 44 X 35 X 34 X X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59 15 X X X
30
The Algoritm 1. Assign to every node a distance value. Set it to zero for our initial node and to infinity (-9999) for all other nodes. 2. Mark all nodes as unvisited. Set initial node as current. 3. For current node, consider all its unvisited neighbors and calculate their distance (from the initial node). For example, if current node (A) has distance of 6, and an edge connecting it with another node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance. 4. When we are done considering all neighbors of the current node, mark it as visited. A visited node will not be checked ever again; its distance recorded now is final and minimal. 5. If all nodes have been visited, finish. Otherwise, set the unvisited node with the smallest distance (from the initial node) as the next "current node" and continue from step 3.
31
Pseudo Code of the Dijkstra’s Alg
Algorithm Dijkstra(Graph, source): for each vertex v in Graph: // Initializations dist[v] := infinity // Unknown distance function from source to v previous[v] := undefined // Previous node in optimal path from source dist[source] := // Distance from source to source Q := the set of all nodes in Graph // All nodes in the graph are unoptimized - thus are in Q while Q is not empty: // The main loop u:= dequeue (Q) // u := vertex in Q with smallest dist[] if dist[u] = infinity: break // all remaining vertices are inaccessible from source dequeue(u,Q) // remove u from Q for each neighbor v of u: // where v has not yet been removed from Q. alt := dist[u] + dist_between(u, v) // Dist. from start point to neighbor if alt < dist[v]: // Relax (u,v) dist[v] := alt previous[v] := u return dist
32
Izmir University of Economics
Homework Assignments 9.5, 9.7, 9.10, 9.40, 9.42, 9.44, 9.46, 9.47, 9.52 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.