Download presentation
Presentation is loading. Please wait.
Published byKathleen Bradford Modified over 6 years ago
1
Lecture 7 Shortest Path Shortest-path problems
Single-source shortest path All-pair shortest path
2
Overview Shortest-path problems Single-source shortest path algorithms
Bellman-Ford algorithm Dijkstra algorithm All-Pair shortest path algorithms Floyd-Warshall algorithm
3
Weighted graph Beijing Qingdao Lhasa Shanghai
Single-source shortest path All-pair shortest path Guangzhou
4
Shortest Path
5
Cycles. Can a shortest path contains cycles?
Optimal substructure: Subpaths of shortest paths are shortest paths. Cycles. Can a shortest path contains cycles? Negative weights.
6
Where are we? Shortest-path problems
Single-source shortest path algorithms Bellman-Ford algorithm Dijkstra algorithm All-Pair shortest path algorithms Floyd-Warshall algorithm
7
Relaxing The process of relaxing an edge (u,v) consists of testing whether we can improve the shortest path to v found so far by going through u.
8
Shortest-path properties
Notation: We fix the source to be s. λ[v]: the length of path computed by our algorithms from s to v. δ[v]: the length of the shortest path from s to v. Triangle property δ[v] <= δ[u] + w(u,v) for any edge (u,v). Upper-bound property δ[v] <= λ[v] for any vertex v. Convergence property If s⇒u→v is a shortest path, and if λ[u] = δ[u], then after relax(u,v), we have λ[v] = δ[v].
9
Bellman-Ford algorithm
E = {(t,x),(t,y),(t,z),(x,t),(y,x),(y,z),(z,x),(z,s),(s,t),(s,y)} Θ(mn)
10
Negative cycle
11
Quiz Run Bellman-Ford algorithm for the following graph, provided by
E = {(S,A),(S,G),(A,E),(B,A),(B,C),(C,D),(D,E),(E,B),(F,A),(F,E),(G,F)}
12
Correctness Correctness of Bellman-Ford
If G contains no negative-weight cycles reachable from s, then the algorithm returns TRUE, and for all v, λ[v] = δ[v], otherwise the algorithm returns FALSE. Proof. No negative cycle. By the fact that the length of simple paths is bounded by |V| - 1. After i-th iteration of relax, λ[vi] = δ[vi]. s v v1 vj vi Negative cycle. Assume vj,…, vi, vj. is a negative cycle and assume Bellman-Ford returns TRUE. λ[vk+1] <= λ[vk] + w(vk,vk+1) Sum all the inequalities together, we will get controdictions.
13
Relax in topological order.
Observation 1 If there is no cycle (DAG), ... Relax in topological order. Θ(m) s v v1 vj vi
14
Weighted DAG application
seam
15
If (s, vi) is the lightest edge sourcing from s, then λ[vi] = δ[vi]
Observation 2 If there is no negative edge, ... vj vi v1 s If (s, vi) is the lightest edge sourcing from s, then λ[vi] = δ[vi]
16
Dijkstra algorithm PV primitives Structural programming
Distributed algorithms Edsger Wybe Dijkstra in 2002
17
Dijkstra algorithm
19
Correctness Assume u is chosen in Step 7, and 1. λ[u] > δ[u]
2. s ⇒ x → y ⇒ u is the shortest path δ[u] = δ[x] + w(x,y) + w(p2) = λ[x] + w(x,y) + w(p2) ≥ λ[x] + w(x,y) ≥ λ[y] ≥ λ[u]
20
Time complexity Θ(n2)
21
What is the time complexity?
How about use d-heap?
22
Comparisons Array Binary heap d-ary heap Dijkstra O(n2) O(mlogn)
O(dnlogdn + mlogdn)
23
Dense graph dense: m = n1+ε, ε is not too small. d-heap, d = m/n
complexity: O(dnlogdn + mlogdn) = O(m)
24
Where are we? Shortest-path problems
Single-source shortest path algorithms Bellman-Ford algorithm Dijkstra algorithm All-Pair shortest path algorithms Floyd-Warshall algorithm
25
All-pairs shortest path
Define $d_{i,j}^{k}$ to be the length of a shortest path from $i$ to $j$ that does not pass any vertex in $\{k+1,k+2,\ldots,n\}$. Clearly \[d_{ij}^{k} = \left\{\begin{array}{ll} l[i,j] & {\rm if}\ k=0 \\ min\{d_{i,j}^{k-1},d_{i,k}^{k-1}+d_{k,j}^{k-1}\} & {\rm if}\ 1\le k\le n \end{array}\right.\]
26
Floyd-Warshall algorithm
\left[ \begin{array}{ccccc} 0 & 10 & \infty & 5 & \infty\\ \infty & 0 & 1 & 2 & \infty\\ \infty & \infty & 0 & \infty & 4\\ \infty & 3 & 9 & 0 & 2\\ 2 & \infty & 6 & \infty & 0 \end{array} \right]
27
Floyd-Warshall algorithm
Θ(n3)
28
Quiz Run Floyd-Warshall algorithm for the following graph:
29
Conclusion Dijkstra’s algorithm.
Nearly linear-time when weights are nonnegative. Acyclic edge-weighted digraphs. Faster than Dijkstra’s algorithm. Negative weights are no problem. Negative weights and negative cycles. If no negative cycles, can find shortest paths via Bellman-Ford. If negative cycles, can find one via Bellman-Ford. All-pair shortest path. can be solved via Floyd-Warshall Floyd-Warshall can also compute the transitive closure of directed graph.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.