i t d ti t al ith introduction to algorithms
TRANSCRIPT
![Page 1: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/1.jpg)
I t d ti t Al ithIntroduction to Algorithms
All-Pairs Shortest Paths
My T. Thai @ UF
![Page 2: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/2.jpg)
Single-Source Shortest Paths ProblemSingle-Source Shortest Paths Problem
Input: A weighted, directed graph G = (V, E) Output: An n × n matrix of shortest-path
distances δ. δ(i, j) is the weight of a shortest path ( j) g pfrom i to j.
1 2 3 4 5
1 0 1 ‐3 2 ‐4
2 3 0 ‐4 1 ‐1
3 7 4 0 5 3
4 2 ‐1 ‐5 0 ‐2
5 8 5 1 6 0
My T. [email protected]
2
5 8 5 1 6 0
![Page 3: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/3.jpg)
Can use algorithms for Single-Source Shortest Paths
Run BELLMAN-FORD once from each vertex Time:
If there are no negative-weight edges, could run Dijkstra’s algorithm once from each vertexDijkstra s algorithm once from each vertex Time:
My T. [email protected]
3
![Page 4: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/4.jpg)
OutlineOutline Shortest paths and matrix multiplication
Floyd-Warshall algorithm Floyd Warshall algorithm
Johnson’s algorithm Johnson’s algorithm
My T. [email protected]
4
![Page 5: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/5.jpg)
Recursive solutionRecursive solution
Optimal substructure: subpaths of shortest paths are shortest paths
Recursive solution: Let = weight of shortestgpath from i to j that contains ≤ m edges.
WhWhere wij:My T. Thai
![Page 6: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/6.jpg)
Computing the shortest-path weights bottom upbottom up
All simple shortest paths contain ≤ n − 1 edges
Compute from bottom up: L(1), L(2), . . . , L(n-1). Compute from bottom up: L , L , . . . , L . Compute L(i+1) from L(i) by extending oneL(i) by extending one more edge
iTime:
My T. [email protected]
6
![Page 8: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/8.jpg)
Shortest paths and matrix multiplicationShortest paths and matrix multiplication Extending shortest paths by one more edge
lik t i d t L(i+1) L(i) Wlikes matrix product: L(i+1)= L(i).W Compute L(1), L(2), L(4) . . . , L(r) with
Time:Time:
My T. [email protected]
8
![Page 10: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/10.jpg)
Floyd-Warshall algorithmFloyd Warshall algorithm For path p = <v1, v2, . . . , vl> , v2 … vl-1 are
intermediate vertices from v1 to vl
Define = shortest-path weight of any path from i to j with all intermediate vertices in {1, 2, . . . , k}
Consider a shortest path with all intermediate i i {1 2 k}vertices in {1, 2, . . . , k}:
If k is not an intermediate vertex, all intermediate vertices in {1 2 k -1}in {1, 2, . . . , k -1}
If k is an intermediate vertex:
![Page 11: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/11.jpg)
Floyd-Warshall algorithmFloyd Warshall algorithm Recursive formula:
Time: N t i h t t Time: Note: since we have at most n vertices, return
My T. [email protected]
11
![Page 12: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/12.jpg)
Constructing a shortest pathConstructing a shortest path is the predecessor of vertex j on a shortest
path from vertex i with all intermediate vertices in the set {1, 2, . . . , k}
(Use vertex k)
My T. [email protected]
12
![Page 16: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/16.jpg)
Johnson’s algorithmJohnson s algorithm Reweighting edges to get non-negative weight
edges: For all u, v V, p is a shortest path using
w if and only if p is a shortest path using
For all (u, v) E, Run Dijkstra’s algorithm once from each vertexj g
My T. [email protected]
16
![Page 18: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/18.jpg)
Proof of lemma 25 1Proof of lemma 25.1 First, we prove
i h l With cycle ,
My T. [email protected]
18
![Page 19: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/19.jpg)
Producing nonnegative weightsProducing nonnegative weights Construct
Since no edges enter s, has the same set of cycles Gas G
has a negative-weight cycle if and only if G does
Define: Define: Claim: Proof: Triangle inequality of shortest paths Proof: Triangle inequality of shortest paths
My T. [email protected]
19
![Page 23: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/23.jpg)
SummarySummary Dynamic-programming algorithm based on matrix
multiplication Define sub-optimal solutions based on the length of
thpaths
Use the technique of “repeated squaring” Time: Time:
Floyd-Warshall algorithm Define sub optimal solutions based on the set of Define sub-optimal solutions based on the set of
allowed intermediate vertices Time:
My T. [email protected]
23
![Page 24: I t d ti t Al ith Introduction to Algorithms](https://reader031.vdocuments.site/reader031/viewer/2022013113/58a2d62b1a28ab8e608be2ff/html5/thumbnails/24.jpg)
Johnson’s algorithm Reweight edges to non-negative weight edges Run Dijkstra’s algorithm once from each vertex Time: Faster than Floyd-Warshall algorithm when the graph
is dense E = o(V2)
My T. [email protected]
24