Dijkstras Shortest Path Algorithm CutlerHead SingleSource Shortest Paths

  • Slides: 18
Download presentation
Dijkstra’s Shortest Path Algorithm Cutler/Head

Dijkstra’s Shortest Path Algorithm Cutler/Head

Single-Source Shortest Paths • We wish to find the shortest route between Binghamton and

Single-Source Shortest Paths • We wish to find the shortest route between Binghamton and NYC. Given a NYS road map with all the possible routes how can we determine our shortest route? • We could try to enumerate all possible routes. It is certainly easy to see we do not need to consider a route that goes through Buffalo. 9/16/2021 Cutler/Head 2

Modeling Problem • We can model this problem with a directed graph. Intersections correspond

Modeling Problem • We can model this problem with a directed graph. Intersections correspond to vertices, roads between intersections correspond to edges and distance corresponds to weights. One way roads correspond to the direction of the edge. • The problem: – Given a weighted digraph and a vertex s in the graph: find a shortest path from s to t 9/16/2021 Cutler/Head 3

The distance of a shortest path Case 1: The graph may have negative edges

The distance of a shortest path Case 1: The graph may have negative edges but no negative cycles. The shortest distance from s to t can be computed. s 1 A B -3 8 t d(s, t)=6 Case 2: The graph contains negative weight cycles, and a path from s to t includes an edge on a negative weight cycle. The shortest path distance is -. s 9/16/2021 1 A 1 -3 B Cutler/Head 8 t d(s, t)=- 4

Shortest Path Algorithms • Dijkstra’s algorithm does NOT allow negative edges. – Uses a

Shortest Path Algorithms • Dijkstra’s algorithm does NOT allow negative edges. – Uses a greedy heuristic. – undirected/directed graph • Bellman-Ford’s and Floyd’s algorithms work correctly for any graph and can detect negative cycles. – Must be directed graph if negative edges are included. 9/16/2021 Cutler/Head 5

Solving Optimization Problems • Often solved in 2 stages: – First Stage: solves an

Solving Optimization Problems • Often solved in 2 stages: – First Stage: solves an intermediate problem and saves an additional data structure. – Second Stage: uses the data Structure to construct the solution to the problem. 9/16/2021 Cutler/Head 6

Dijkstra’s shortest path algorithm Dijkstra’s algorithm solves the single source shortest path problem in

Dijkstra’s shortest path algorithm Dijkstra’s algorithm solves the single source shortest path problem in 2 stages. – Stage 1: A greedy algorithm computes the shortest distance from s to all other nodes in the graph and saves a data structure. – Stage 2 : Uses the data structure for finding a shortest path from s to t. CS 333 /Class 25 7

Main idea • Assume that the shortest distances from the starting node s to

Main idea • Assume that the shortest distances from the starting node s to the rest of the nodes are d(s, s) d(s, s 1) d(s, s 2) … d(s, sn-1) • In this case a shortest path from s to si may include any of the vertices {s 1, s 2 … si-1} but cannot include any sj where j > i. • Dijkstra’s main idea is to select the nodes and compute the shortest distances in the order s, s 1, s 2 , …, sn-1 CS 333 /Class 25 8

Example d(s, s) =0 d(s, s 1)=5 d(s, s 2)=6 d(s, s 3)=8 d(s,

Example d(s, s) =0 d(s, s 1)=5 d(s, s 2)=6 d(s, s 3)=8 d(s, s 4)=15 s 4 15 10 s s 3 3 s 1 from s to s 2 includes s 1 as 2 4 5 Note: The shortest path 8 1 an intermediate node but cannot include s 3 or s 4. 9/16/2021 Cutler/Head 9

Dijkstra’s greedy selection rule • Assume s 1, s 2 … si-1 have been

Dijkstra’s greedy selection rule • Assume s 1, s 2 … si-1 have been selected, and their shortest distances have been stored in Solution • Select node si and save d(s, si) if si has the shortest distance from s on a path that may include only s 1, s 2 … si-1 as intermediate nodes. We call such paths special • To apply this selection rule efficiently, we need to maintain for each unselected node v the distance of the shortest special path from s to v, D[v]. 9/16/2021 Cutler/Head 10

Application Example Solution = {(s, 0)} D[s 1]=5 for path [s, s 1] D[s

Application Example Solution = {(s, 0)} D[s 1]=5 for path [s, s 1] D[s 2]= for path [s, s 2] D[s 3]=10 for path [s, s 3] D[s 4]=15 for path [s, s 4]. s 4 Solution = {(s, 0), (s 1, 5) } D[s 2]= 6 for path [s, s 1, s 2] D[s 3]=9 for path [s, s 1, s 3] D[s 4]=15 for path [s, s 4] 15 10 s Solution = {(s, 0), (s 1, 5), (s 2, 6) } D[s 3]=8 for path [s, s 1, s 2, s 3] D[s 4]=15 for path [s, s 4] 8 s 3 3 2 s 2 4 5 s 1 1 Solution = {(s, 0), (s 1, 5), (s 2, 6), (s 3, 8), (s 4, 15) } 9/16/2021 Cutler/Head 11

Implementing the selection rule • Node near is selected and added to Solution if

Implementing the selection rule • Node near is selected and added to Solution if D(near) D(v) for any v Ï Solution. s 4 Solution = {(s, 0)} D[s 1]=5 D[s 2]= D[s 1]=5 D[s 3]=10 D[s 1]=5 D[s 4]=15 Node s 1 is selected Solution = {(s, 0), (s 1, 5) } 15 10 s s 3 3 2 s 2 4 5 s 1 CS 333 /Class 25 8 1 12

Updating D[ ] • After adding near to Solution, D[v] of all nodes v

Updating D[ ] • After adding near to Solution, D[v] of all nodes v Ï Solution are updated if there is a shorter special path from s to v that contains node near, i. e. , if (D[near] + w(near, v ) < D[v]) then D[v]=D[near] + w(near, v ) Solution after adding near s 3 3 2 D[near ] = 5 2 6 CS 333 /Class 25 D[ v ] = 9 is updated to D[v]=5+2=7 13

Updating D Example Solution = {(s, 0)} D[s 1]=5, D[s 2]= , D[s 3]=10,

Updating D Example Solution = {(s, 0)} D[s 1]=5, D[s 2]= , D[s 3]=10, D[s 4]=15. s 4 Solution = {(s, 0), (s 1, 5) } D[s 2]= D[s 1]+w(s 1, s 2)=5+1=6, D[s 3]= D[s 1]+w(s 1, s 3)=5+4=9, D[s 4]=15 Solution = {(s, 0), (s 1, 5), (s 2, 6) } D[s 3]=D[s 2]+w(s 2, s 3)=6+2=8, D[s 4]=15 15 10 s 8 s 3 3 2 s 2 4 5 s 1 1 Solution = {(s, 0), (s 1, 5), (s 2, 6), (s 3, 8), (s 4, 15) } 9/16/2021 Cutler/Head 14

Dijkstra’s Algorithm for Finding the Shortest Distance from a Single Source Dijkstra(G, s) 1.

Dijkstra’s Algorithm for Finding the Shortest Distance from a Single Source Dijkstra(G, s) 1. for each v V 2. do D [ v ] 3. D [ s ] 0 4. PQ make-PQ(D, V) 5. while PQ 6. do near PQ. extract. Min () 7. for each v Adj(near ) 8 if D [ v ] > D [ near ] + w( near , v ) 9. then D [ v ] D [ near ] + w( near, v ) 10. PQ. decrease. Priority. Value (D[v], v ) 11. return the label D[u] of each vertex u 9/16/2021 Cutler/Head 15

1. for each v V 2. do D [ v ] 3. D [

1. for each v V 2. do D [ v ] 3. D [ s ] 0 4. PQ make-PQ(D, V) Time Analysis Using Heap implementation Lines 1 -4 run in O (V ) Max Size of PQ is | V | 5. while PQ 6. do near PQ. extract. Min () 7. for each v Adj(near ) 8 if D [ v ] > D [ near ] + w( near , v ) 9. then D [ v ] D[near] + w(near, v) 10. PQ. decrease. Priority. Value (D[v ], v ) 11. return the label D[u] of each vertex u Assume a node in PQ can be accessed in O(1) ** Decrease key for v requires O(lg. V ) provided the node in heap with v’s data can be accessed in O(1) 9/16/2021 (5) Loop = O (V ) - Only decreases (6+(5)) O (V ) O( lg V ) (7+(5)) Loop = O( deg(near) ) = O( E ) (8+(7+(5))) O(1) O( E ) (9) O(1) (10+(7+(5))) Decrease- Key operation on the heap can be implemented in O( lg V) O( E ). So total time for Dijkstra's Algorithm is O ( V lg V + E lg V ) What is O(E ) ? For Sparse Graph = O(V lg V ) For Dense Graph = O(V 2 lg V ) Cutler/Head 16

Example 4 b a 2 4 c 1 10 d 9/16/2021 Cutler/Head 2 5

Example 4 b a 2 4 c 1 10 d 9/16/2021 Cutler/Head 2 5 e 17

Solution for example S D(a) D(b) D(c) D(d) D(e) a 0() () () 4

Solution for example S D(a) D(b) D(c) D(d) D(e) a 0() () () 4 (a, b) 4 (a, c) () 4 (a, c) 14(b, d) () 5 (c, d) 6(c, e) b c d e 9/16/2021 6(c, e) Cutler/Head 18