Publicité

Session 13 - Single Source Shortest Path Method.pptx

25 Mar 2023
Publicité

Contenu connexe

Publicité

Session 13 - Single Source Shortest Path Method.pptx

  1. Analysis and Design of Algorithms Session -13 1 Single Source Shortest Path Problem
  2. Insights • Introduction • Example • Dijkstra’s Algorithm – Pseudocode • Dijkstra’s Algorithm – Time Complexity
  3. Single Source Shortest Path Problem (SSSP) – An Introduction Single Source Shortest Path Problem (SSSP) • In a Single Source Shortest Paths Problem, we are given a Graph G = (V, E), we want to find the shortest path from a given source vertex s ∈ V to every vertex v ∈ V.
  4. 4 -4 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco
  5. 4 -5 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 2 3 4 5 0 6 7 8 5 is a Source Vertex
  6. 4 -6 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1500 5 0 6 250 7 inf 8 inf Updated 4 & 6 Vertices
  7. 4 -7 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1500 5 0 6 250 7 inf 8 inf Vertex 6 selected
  8. 4 -8 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1250 5 0 6 250 7 inf 8 inf Updated Vertex 4
  9. 4 -9 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1250 5 0 6 250 7 inf 8 1650 Updated Vertex 8
  10. 4 -10 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1250 5 0 6 250 7 1150 8 1650 Updated Vertex 7
  11. 4 -11 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1250 5 0 6 250 7 1150 8 1650 Vertex 7 selected
  12. 4 -12 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1250 5 0 6 250 7 1150 8 1650 No changes in Vertex 4 & 8
  13. 4 -13 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 inf 4 1250 5 0 6 250 7 1150 8 1650 Vertex 4 selected
  14. 4 -14 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Updated Vertex 3
  15. 4 -15 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 inf 2 inf 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Vertex 8 selected
  16. 4 -16 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 3350 2 inf 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Updated Vertex 1
  17. 4 -17 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 3350 2 inf 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Vertex 3 selected
  18. 4 -18 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 3350 2 3250 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Updated Vertex 2
  19. 4 -19 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 3350 2 3250 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Vertex 2 selected
  20. 4 -20 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 3350 2 3250 3 2450 4 1250 5 0 6 250 7 1150 8 1650 No changes in Vertex 1
  21. 4 -21 300 4 2 6 7 8 5 3 1 250 1500 900 1000 1400 1000 1700 1200 800 1000 Single Source Shortest Path Problem (SSSP) Boston New York Miami New Orleans Los Angeles Chicago Denver San Francisco City Dist 1 3350 2 3250 3 2450 4 1250 5 0 6 250 7 1150 8 1650 Vertex 1 selected
  22. 22 Algorithm ShortestPaths (v, cost, dist, n) // dist[j], 1<= j <=n, is set to the length of the shortest path from vertex v to vertex j in a // di-graph G with n vertices. dist[v] is set to zero. G is represented by its cost adjacency // matrix cost[l :n, 1:n]. { for i :=1 to n do { // Initialize S. S[i]:=false; dist[i] := cost[v, i]; } S[v]:=true; dist[v] :=0.0; // Put v in S. for num := 2 to n -1 do { // Determine n-1 paths from v. Choose u from among those vertices not in S such that dist[u] is minimum; S[u]:=true; // Put u in S. for (each w adjacent to u with S[w]= false) do // Update distances. if (dist[w] >(dist[u]+cost[u, w,])) then dist[w] :=dist[u]+cost[u, w]; }// end of for loop }
  23. 23 Algorithm ShortestPaths (v, cost, dist, n) { for i :=1 to n do { // Initialize S. S[i]:=false; dist[i] := cost[v, i]; } S[v]:=true; dist[v] :=0.0; // Put v in S. for num := 2 to n -1 do { // Determine n-1 paths from v. Choose u from among those vertices not in S such that dist[u] is minimum; S[u]:=true; // Put u in S. for (each w adjacent to u with S[w]= false) do // Update distances. if (dist[w] >(dist[u]+cost[u, w,])) then dist[w] :=dist[u]+cost[u, w]; }// end of for loop }
  24. 24 Algorithm ShortestPaths (v, cost, dist, n) { for i :=1 to n do { // Initialize S. S[i]:=false; dist[i] := cost[v, i]; } S[v]:=true; dist[v] :=0.0; // Put v in S. for num := 2 to n -1 do { // Determine n-1 paths from v. Choose u from among those vertices not in S such that dist[u] is minimum; S[u]:=true; // Put u in S. for (each w adjacent to u with S[w]= false) do // Update distances. if (dist[w] >(dist[u]+cost[u, w,])) then dist[w] :=dist[u]+cost[u, w]; }// end of for loop }
  25. 25 Algorithm ShortestPaths (v, cost, dist, n) { for i :=1 to n do { // Initialize S. S[i]:=false; dist[i] := cost[v, i]; } S[v]:=true; dist[v] :=0.0; // Put v in S. for num := 2 to n -1 do { // Determine n-1 paths from v. Choose u from among those vertices not in S such that dist[u] is minimum; S[u]:=true; // Put u in S. for (each w adjacent to u with S[w]= false) do // Update distances. if (dist[w] >(dist[u]+cost[u, w,])) then dist[w] :=dist[u]+cost[u, w]; }// end of for loop }
  26. 26 Algorithm ShortestPaths (v, cost, dist, n) { for i :=1 to n do { // Initialize S. S[i]:=false; dist[i] := cost[v, i]; } S[v]:=true; dist[v] :=0.0; // Put v in S. for num := 2 to n -1 do { // Determine n-1 paths from v. Choose u from among those vertices not in S such that dist[u] is minimum; S[u]:=true; // Put u in S. for (each w adjacent to u with S[w]= false) do // Update distances. if (dist[w] >(dist[u]+cost[u, w,])) then dist[w] :=dist[u]+cost[u, w]; }// end of for loop }
  27. 27 Algorithm ShortestPaths (v, cost, dist, n) { for i :=1 to n do { // Initialize S. S[i]:=false; dist[i] := cost[v, i]; } S[v]:=true; dist[v] :=0.0; // Put v in S. for num := 2 to n -1 do { // Determine n-1 paths from v. Choose u from among those vertices not in S such that dist[u] is minimum; S[u]:=true; // Put u in S. for (each w adjacent to u with S[w]= false) do // Update distances. if (dist[w] >(dist[u]+cost[u, w,])) then dist[w] :=dist[u]+cost[u, w]; }// end of for loop }
  28. 28 Thank You
Publicité