Single-Source Shortest Paths Chapter 24 1 CPTR 430 Algorithms Single-Source Shortest Paths
Motivation ■ We plan a trip from Collegedale to Silicon Valley ■ Our map has distances between interstate highway interchanges marked ■ We want to layout the shortest route ■ Proposal: Try all possible routes and select the shortest one 2 CPTR 430 Algorithms Single-Source Shortest Paths
Try-All-Routes Algorithm The try-all-routes approach is combinatorially explosive! 3 CPTR 430 Algorithms Single-Source Shortest Paths
✟ ✠ ✞ ✞ ✂✞ ✄ ✂ � ✝ � ✆ ✁ ☎ ✁ ✡ ✄ ✂ ✂ ✄ ✁ � ✂ Shortest-Path Problem ■ G V E is a weighted, directed graph ■ Weight function w : E ■ Weight of a path p v 0 v 1 v k is the sum of the weights of the edges that make up the path: k ∑ w p w v i v i 1 i 1 4 CPTR 430 Algorithms Single-Source Shortest Paths
✄ ✂ ✄ ✁ ✂ ✂ ✁ ✄ � � � ✄ ✁ ✁ ✄ ✁ Shortest-Path Weight The shortest-path weight from u to v is u p w p v if there is a path from u to v min δ u v ∞ otherwise A shortest path from vertex u to vertex v is any path with weight δ w p u v 5 CPTR 430 Algorithms Single-Source Shortest Paths
Model ■ In our trip planner problem ❚ Each interchange is a vertex in the graph ❚ Each road connecting interchanges is an edge in the graph ❚ The distance between two interchanges represents an edge weight ■ Edge weights may be interpreted as metrics other than distance: ❚ time ❚ cost ❚ penalties Any quantity that accumulates linearly along a path that we wish to minimize 6 CPTR 430 Algorithms Single-Source Shortest Paths
Breadth-first Search BFS (Chapter 22) is one type of shortest path problem ■ BFS is (usually) applied to unweighted, undirected graphs ■ Viewed another way, unweighted graphs are just weighted graphs with unit-weight edges ■ The concepts of BFS can be applied to shortest paths in weighted, directed graphs 7 CPTR 430 Algorithms Single-Source Shortest Paths
� � � � Shortest Path Variants ■ Single-source SP ❚ Find the shortest path from a source vertex s V to every vertex v V ■ Single-destination SP ❚ Find the shortest path to a destination vertex t V from every vertex v V ❚ For undirected and symmetric directed graphs, just use the single- source SP algorithm and reverse all the edges 8 CPTR 430 Algorithms Single-Source Shortest Paths
✂ � � � � Shortest Path Variants ■ Single-pair SP ❚ Find the shortest path from a source vertex u V to a destination vertex v V ❚ Apply the single-source SP algorithm with u as the source vertex; the shortest path to v is found in the solution ❚ No known algorithms for this problem run faster asymptotically than the single-source algorithm applied to this problem ■ All-pairs SP ❚ Find the SP between u and v , u v V ❚ Can run single-source SP algorithm on every vertex ❚ A more efficient algorithm exists 9 CPTR 430 Algorithms Single-Source Shortest Paths
Optimal Substructure ■ The shortest path between two vertices contains other shortest paths ■ SP problems thus exhibit an optimal substructure; recall ❚ Dynamic programming (Chapter 15) ❚ Greedy algorithms (Chapters 16, 23) Dijkstra’s SP algorithm is a greedy algorithm 10 CPTR 430 Algorithms Single-Source Shortest Paths
✁ ✟ ✂ ✟ ✂ � ✂ � � ✞ ✂ � ✝ ✞ ✂ ✞ ✂✞ ✞ ✂✞ � � ✁ ✂ ✄ ☎ ✆ ✝ ✂ Lemma 24.1 Subpaths of shortest paths are shortest paths ■ Given a weighted digraph G V E with weight function w : E ■ Let p v 0 v 1 v k be a shortest path from vertex v 0 to vertex v k ■ For any i j such that 0 i j k , let p i j v i v i v j be a 1 subpath of p from vertex v i to vertex v j p i j is a shortest path from v i to v j 11 CPTR 430 Algorithms Single-Source Shortest Paths
� ✄ ✄ ✄ ✁ ✁ ✄ ✂ ✄ ✁ ✂ ✂ ✁ ✁ ✄ � � ✁ ✁ ✁ � ✁ ✂ ✄ ✁ ✂ ✄ ☎ ✂ ✁ ✄ ✝ ✄ Proof of Lemma 24.1 We’ll use a proof by contradiction ■ Let p be a shortest path from v 0 to v k p i j p jk p 0 i ■ Decompose path p into three segments: v 0 v i v i v j v j v k ✄ ✂✁ ✄ ✂✁ w p w p 0 i w p i j w p jk ✄ ✆☎ ■ Assume there exists a path p i j from v i to v j such that w p w p i j i j p p jk p 0 i i j v 0 v i v i v j v j v k is a path ✄ ✂✁ ✄ ✂✁ from v 0 to v k with weight w p w p 0 i w p w p jk w p i j ■ This contradicts the assumption that p is a shortest path from v 0 to v k 12 CPTR 430 Algorithms Single-Source Shortest Paths
Negative-Weight Edges ■ Some SP algorithms can handle graphs with negative edges ❚ E.g., Bellman-Ford algorithm while others cannot ❚ E.g., Dijkstra’s algorithm ■ We will focus on graphs with non-negative edges ■ Most practical problems involve non-negative edges exclusivley 13 CPTR 430 Algorithms Single-Source Shortest Paths
Cycles ■ Any path that contains a positive-weight cycle can be made shorter if the cycle is removed from the path ■ Negative-weight cycles are pathological for SP problems, so they are not allowed (Why?) ■ 0 -weight cycles do not affect the path lengths, so we generally remove 0 -weight cycles from paths, if necessary 14 CPTR 430 Algorithms Single-Source Shortest Paths
� ✄ ✂ � � ✄ � � ✁ ✂ � ✄ ✄ � ✂ � � ✁ ✁ � ✁ ✁ ✄ � ✄ � � � ☎ � ✁ ✁ � � ✄ ✁ ✂ ✁ � � Representing Shortest Paths ■ Use a representation similar to BFS (Chapter 22) V , π v v is the predecessor of v on a shortest path from s (start ■ vertex) to v ❚ π v is either another vertex or null ❚ The predecessors can be traced back from a given vertex v to s to determine a shortest path from s to v ❚ Given a vertex v with π v null , the printPath() algorithm (Chapter 22) will print a shortest path from s to v is induced by the π values ■ The predecessor subgraph G π V π E π π ❚ V π v V v s null π ❚ E π v v E v V π s 15 CPTR 430 Algorithms Single-Source Shortest Paths
� ✄ � ✄ ✄ � ✄ � ✄ ✄ ✄ ✂ ✄ ✁ � ✄ Shortest-paths Tree ■ When the shortest-path algorithm is finished, G π is a tree that contains a shortest path from the source vertex s to every other vertex reachable from s ■ A shortest-path tree, G V E , is a directed subgraph of G , where ❚ V V and E E ❚ V is the set of vertices reachable from s ❚ G is a tree rooted at s v V , the unique simple path from s to v is a shortest path from s ❚ to v in G ■ Shortest-path trees for given graph G and start vertex s are not unique ■ Shortest-path trees are like breadth-first trees (Chapter 22) But here shortest paths are defi ned based on edge weights, not simply the number of edges in the path 16 CPTR 430 Algorithms Single-Source Shortest Paths
� ✁ ✁ � ☎ � � � � ✂ ✁ � � � � � ✁ � ✁ ✁ ✂ � � � ✄ Shortest-path Estimate ■ Each vertex v V maintains a shortest-path estimate, d v , an upper bound on the weight of a shortest path from s to v ■ Initially, ∞ ❚ d v v V s 0 ❚ d s ❚ π v v V null and π ■ The SP algorithm traverses the graph and updates the d v v values as necessary 17 CPTR 430 Algorithms Single-Source Shortest Paths
� ✁ ✄ � � � � ✄ ✂ ✂ � ✁ ✁ ✁ ✁ � ✁ ✁ ✁ ✄ ✁ ✁ ✂ ✁ ✄ � ✁ ✁ ✂ ✂ ✁ ✄ ✁ ✁ � � Relaxation ■ An edge u v is relaxed when the SP algorithm determines if the shortest path found so far to v can be improved if u v is used in the shortest path and π u v d v v results a shorter path are updated ■ ■ d v d u w u v d v d u w u v , and π v u ■ Relaxation is the only way shortest-path estimates and predecessors are updated 18 CPTR 430 Algorithms Single-Source Shortest Paths
Relaxation Example [ ] [ ] 9 6 [ ] 5 [ ] 5 d u = d v = d u = d v = 2 2 u u v v π[ ] = π[ ] = v x v x Relax Relax ( , ) u v ( , ) u v [ ] 5 [ ] [ ] 5 [ ] d u = 7 d u = 6 d v = d v = 2 2 u u v v π[ ] = π[ ] = u v x v 19 CPTR 430 Algorithms Single-Source Shortest Paths
✄ ✁ ✂ ✄ � � � ✂ � ✄ ✂ ✁ ✂ ✁ ✄ � � ✁ ✁ � ✁ ✁ ✂ ✄ � � ✁ ✂ ✂ ✄ � ✁ ✁ ✂ ✄ Properties of Shortest Paths and Relaxation ■ Triangle inequality E , δ δ u v s v s u w u v ❚ ❚ This is a standard metric property ■ Upper-bound property δ ✁ ✁� v V d v s v ❚ It is always true that is assigned δ ❚ Once d v s v , it never changes ■ No-path property δ ∞ at all times ❚ If no path exists from s to v , then d v s v 20 CPTR 430 Algorithms Single-Source Shortest Paths
Recommend
More recommend