Single-Source Shortest Paths
Chapter 24
CPTR 430 Algorithms Single-Source Shortest Paths
Single-Source Shortest Paths Chapter 24 1 CPTR 430 Algorithms - - PowerPoint PPT Presentation
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
CPTR 430 Algorithms Single-Source Shortest Paths
■ We plan a trip from Collegedale to Silicon Valley ■ Our map has distances between interstate highway interchanges
■ We want to layout the shortest route ■ Proposal: Try all possible routes and select the shortest one
CPTR 430 Algorithms Single-Source Shortest Paths
CPTR 430 Algorithms Single-Source Shortest Paths
■ G
■ Weight function w : E
☎ ✆■ Weight of a path p
i
✠1
1
✂CPTR 430 Algorithms Single-Source Shortest Paths
CPTR 430 Algorithms Single-Source Shortest Paths
■ 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
CPTR 430 Algorithms Single-Source Shortest Paths
■ BFS is (usually) applied to unweighted, undirected graphs ■ Viewed another way, unweighted graphs are just weighted graphs with
■ The concepts of BFS can be applied to shortest paths in weighted,
CPTR 430 Algorithms Single-Source Shortest Paths
■ Single-source SP ❚ Find the shortest path from a source vertex s
■ Single-destination SP ❚ Find the shortest path to a destination vertex t
❚ For undirected and symmetric directed graphs, just use the single-
CPTR 430 Algorithms Single-Source Shortest Paths
■ Single-pair SP ❚ Find the shortest path from a source vertex u
❚ Apply the single-source SP algorithm with u as the source vertex; the
❚ No known algorithms for this problem run faster asymptotically than
■ All-pairs SP ❚ Find the SP between u and v,
❚ Can run single-source SP algorithm on every vertex ❚ A more efficient algorithm exists
CPTR 430 Algorithms Single-Source Shortest Paths
■ The shortest path between two vertices contains other shortest paths ■ SP problems thus exhibit an optimal substructure; recall ❚ Dynamic programming
❚ Greedy algorithms
CPTR 430 Algorithms Single-Source Shortest Paths
■ Given a weighted digraph G
■ Let p
■ For any i
✂1
✂✞ ✞ ✞ ✂CPTR 430 Algorithms Single-Source Shortest Paths
■ Let p be a shortest path from v0 to vk ■ Decompose path p into three segments: v0
■ Assume there exists a path p
✄i j from vi to v j such that w
✁i j
✄✆☎i j
✂i j
✄✂✁■ This contradicts the assumption that p is a shortest path
CPTR 430 Algorithms Single-Source Shortest Paths
■ Some SP algorithms can handle graphs with negative edges ❚ E.g., Bellman-Ford algorithm
❚ E.g., Dijkstra’s algorithm ■ We will focus on graphs with non-negative edges ■ Most practical problems involve non-negative edges exclusivley
CPTR 430 Algorithms Single-Source Shortest Paths
■ Any path that contains a positive-weight cycle can be made shorter if
■ Negative-weight cycles are pathological for SP problems, so they are
■ 0-weight cycles do not affect the path lengths, so we generally remove
CPTR 430 Algorithms Single-Source Shortest Paths
■ Use a representation similar to BFS (Chapter 22) ■
❚ π
❚ The predecessors can be traced back from a given vertex v to s to
❚ Given a vertex v with π
■ The predecessor subgraph Gπ
❚ Vπ
❚ Eπ
CPTR 430 Algorithms Single-Source Shortest Paths
■ When the shortest-path algorithm is finished, Gπ is a tree that contains
■ A shortest-path tree, G
✄❚ V
✄❚ V
✄❚ 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)
CPTR 430 Algorithms Single-Source Shortest Paths
■ Each vertex v
■ Initially, ❚ d
❚ d
■ The SP algorithm traverses the graph and updates the d
CPTR 430 Algorithms Single-Source Shortest Paths
■ An edge
✁■
✁■ d
■ Relaxation is the only way shortest-path estimates and predecessors
CPTR 430 Algorithms Single-Source Shortest Paths
CPTR 430 Algorithms Single-Source Shortest Paths
■ Triangle inequality ❚
❚ This is a standard metric property ■ Upper-bound property ❚ It is always true that
❚ Once d
■ No-path property ❚ If no path exists from s to v, then d
CPTR 430 Algorithms Single-Source Shortest Paths
■ Convergence property ❚ If s
✂■ Path-relaxation property ❚ If p
1
✂❚ The path-relaxation property holds regardless of other intermixed
■ Predecessor-subgraph property ❚ d
CPTR 430 Algorithms Single-Source Shortest Paths
■ Solves the single-source SP problem on graphs ❚ that are weighted ❚ directed ❚ contain only non-negative edge weights ■ S is a set of vertices whose final SP weights from source s have already
■
CPTR 430 Algorithms Single-Source Shortest Paths