Download presentation
Presentation is loading. Please wait.
1
CSC 213 Lecture 23: Shortest Path Algorithms
2
Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs, travel times, etc. Example: Weights are miles between the two airports ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205
3
Shortest Paths Find path of minimum weight between two vertices u and v Path’s weight is sum of the edge weights Example: Shortest path between Providence and Honolulu ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205
4
Shortest Path Assumptions Assumptions: Subpath of shortest path is also shortest path (no negative weights) Shortest path from start vertex to all other vertices is a tree Example: Single-source/multi-sink shortest path solution starting at Providence ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205
5
Dijkstra’s Algorithm Distance from vertex s to vertex v is length of shortest path between s and v Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s Note: Relies upon several assumptions Graph is connected Edge weights are nonnegative (0 or larger)
6
Dijkstra’s Algorithm (§ 12.6.1) Beginning from s, grow cloud of vertices Store distance from s through cloud to each vertex v At each step Add to cloud the vertex with smallest distance that is not in cloud currently Update distances of vertices adjacent vertex added to cloud
7
Edge Relaxation Consider edge e where vertex u added to cloud most recently z is not in the cloud Relaxation of e updates distance to z d(z) 75 d(u) 50 10 z s u d(z) 60 d(u) 50 10 z s u e e
8
Example CB A E D F 0 428 4 8 71 25 2 39 C B A E D F 0 328 511 4 8 71 25 2 39 C B A E D F 0 328 58 4 8 71 25 2 39 C B A E D F 0 327 58 4 8 71 25 2 39
9
Example (cont.) CB A E D F 0 327 58 4 8 71 25 2 39 CB A E D F 0 327 58 4 8 71 25 2 39
10
Shortest Paths Tree Using template method pattern, extend Dijkstra’s algorithm to return tree of shortest paths from start vertex Also store vertex’s parent edge in tree Edge relaxation step also updates the vertex’s parent label
11
Why Dijkstra’s Algorithm Works Dijkstra was smart. Smarter even than me Dijkstra’s algorithm is greedy. Vertices added in increasing distances order Suppose it didn’t find shortest distance to a vertex F, but did find shortest distance to D But edge from D to F was relaxed when D was added to cloud But so long as d( F )>d( D ), F ’s distance must be correct. CB A E D F 0 327 58 4 8 71 25 2 39
12
Why No Negative-Weight Edges? CB A E D F 0 457 59 4 8 71 25 6 0-8 Suppose edge with negative weight exists When we add incident vertex to cloud, may discover smaller distance to opposite vertex But only consider vertices not in vertices C ’s true distance is 1, but added to cloud with distance of 5!
13
Not All Is Lost Algorithms computing shortest paths that include negative weights do exist Bellman-Ford algorithm takes time O ( nm ) DAG-based algorithms takes time O ( n + m ) Merlin algorithm takes time O ( n + m )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.