This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Shortest Paths in Edge Weighted GraphsGeneral input: directed or undirected graph with weights on the edges.
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Shortest Paths in Edge Weighted GraphsGeneral input: directed or undirected graph with weights on the edges.
Versions of the problem:1. Given vertices s, t, find shortest s-t path. This seems to involve solving 2.2. “single source shortest path problem”. Given s, find shortest s-v path for all v.3. “all pairs shortest path problem”. Find shortest u-v path for all u and v.
What happens with negative weight edges?
Recall that Dijkstra’s algorithm solves (2) for non-negative weight edges.
What is the shortest path from s to t?
CS341-Lecture17 2 of 16
C
s - c - t has weight 3€s - c- a- b - c - t has weight I
go around cycle twice - weight - I .
ooo so weight is - ? ?Natural idea : find shortest simple path s to tNo one knows a good algorithm for this I
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
2. single source shortest path problem for directed graphs. Given s, find shortest s-v path for all v.
a. no cycles. O(n+m).b. no negative weights. O(m log n). Dijkstra’s algorithm.c. allow negative weights and allow cycles but NO negative weight cycles. O(nm).
Bellman-Ford algorithm — dynamic programming.
3. all pairs shortest path problem for directed graphs. Find shortest u-v path for all u and v.
NO negative weight cycles. O(n3). Floyd-Warshall algorithm — dynamic programming this lecture
this lecture
this lecture
last lecture
Note: what about undirected graphs?
CS341-Lecture17 3 of 16
natural reductionBut what ifwca.b.is negative?
b Thenwegeta negative weightaw
. ⇒ IIb directed cycle .undirected
two directed edges so differenttechniques areedge carb) ca ,b> and Cb,a ) needed .
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
2. single source shortest path problem for directed graphs. Given s, find shortest s-v path for all v.
a. no cycles. O(n+m).b. no negative weights. O(m log n). Dijkstra’s algorithm.c. allow negative weights and allow cycles but NO negative weight cycles. O(nm).
Bellman-Ford algorithm — dynamic programming.
3. all pairs shortest path problem for directed graphs. Find shortest u-v path for all u and v.
NO negative weight cycles. O(n3). Floyd-Warshall algorithm — dynamic programming this lecture
this lecture
this lecture
last lecture
Outline of this lecture.
CS341-Lecture17 4 of 16
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Single source shortest paths in a directed acyclic graph (DAG)
Use topological sort, to find vertex order 1, 2, . . . , n so every directed edge (i,j) has i < j. Recall (from Lecture 14) that DFS can find this in O(n+m) time.
If v < s then there is no path from s to v. So throw away vertices v with v < s.We can then assume that s=1.
Given s, find shortest s-v path for all v.
initialize d(i) = infty for all i; d(1) := 0for i = 1..n
for every edge (i,j)d(j) := min { d(j), d(i) + w(i,j) }
Let d(i) be the distance from vertex 1 to vertex i.
Correctness. Prove by induction on i = 1. .n that when we start the loop for i, then d(i) is the min distance from 1 to i.
Runtime. O(n+m)
CS341-Lecture17 5 of 16
S•→ • a o
iXj
- this assumes s is vertex I
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Dynamic programming for shortest paths.We will use dynamic programming for two shortest path algorithms in directed graphs.
Idea of dynamic programming for shortest paths:if the shortest u-v path goes through vertex x (we can try all x) then it consists of:shortest u-x path + shortest x-v path
these are subproblems
1. they use fewer edges. This leads to dynamic programming where we try paths of ≤ 1 edge, paths of ≤ 2 edges, . . .
2c. Single source shortest paths. NO negative weight cycles. O(nm). Bellman-Ford algorithm.
3. All pairs shortest paths. NO negative weight cycles. O(n3). Floyd-Warshall algorithm.
Use this for single source shortest paths.
Use this for all pairs shortest paths.
2. they don’t use the vertex x. This leads to dynamic programming where we try paths using only vertex 1, using only vertices 1, 2, using only, . . .
In what way are these subproblems “smaller”? Two possibilities:
CS341-Lecture17 6 of 16
⎧⎨⎩
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Bellman-Ford Algorithm. Single source.
Given an edge-weighted directed graph with no negative weight cycles and a source vertex s, find shortest paths from s to all vertices v.
Let di (v) = length of shortest path from s to v using ≤ i edges. Then we want dn-1(v). Why?
Initialize:
General formula i = 1, 2, . . . n-1:
di (v) = mindi-1(v) (use ≤ i-1 edges)minu {di-1(u) + w(u,v)} (use i edges — try all choices for the last edge)
d0(v) := infty for all v; d0(s) := 0
CS341-Lecture17 7 of 16
If we had a path of Z n edges . then Z htt verticesso it would repeat a vertex - giving adirected) cyclep#t we can leave out the cycleseam and get aFihweight E original .
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
⎧⎨⎩
General formula:
di (v) = mindi-1(v) (use ≤ i-1 edges)minu {di-1(u) + w(u,v)} (use i edges — try all choices for the last edge)
Bellman-Ford Algorithm. Single source.
Correctness. By induction. Initialization gives d0. Assuming di-1 is correct, the above formula gives the correct di because we try all possibilities.
d0(v) := infty for all v; d0(s) := 0for i = 2 . . n-1 for each vertex v di (v) := di-1(v) for each edge (u,v) di (v) := min { di (v), di-1(u) + w(u,v) }
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Dynamic programming for shortest paths.We will use dynamic programming for two shortest path algorithms in directed graphs.
Idea of dynamic programming for shortest paths:if the shortest u-v path goes through vertex x (we can try all x) then it consists of:shortest u-x path + shortest x-v path
these are subproblems
1. they use fewer edges. This leads to dynamic programming where we try paths of ≤ 1 edge, paths of ≤ 2 edges, . . .
2c. Single source shortest paths. NO negative weight cycles. O(nm). Bellman-Ford algorithm.
3. All pairs shortest paths. NO negative weight cycles. O(n3). Floyd-Warshall algorithm.
Use this for single source shortest paths.
Use this for all pairs shortest paths.
2. they don’t use the vertex x. This leads to dynamic programming where we try paths using only vertex 1, using only vertices 1, 2, using only, . . .
In what way are these subproblems “smaller”? Two possibilities:
Recall
CS341-Lecture17 12 of 16
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Floyd-Warshall Algorithm. All pairs.Given an edge-weighted directed graph with no negative weight cycles find shortest paths from u to v for all pairs of vertices u,v.We can output the distances as an nxn matrix D[u,v].
We will get a faster algorithm using dynamic programming where intermediate paths use only a subset of the vertices. Recall that V = {1, 2, . . . , n}.Let Di [u,v] = length of shortest path from u to v using intermediate vertices from {1, 2, . . . , i}.
What if we use Bellman-Ford with each vertex u as a source?
Solve subproblems Di [u,v] for all u,v as i goes from 0 to n.The final answers are Dn [u,v].
D0[u,v] = 0 if u=vw(u,v) if (u,v) is an edgeinfty otherwise
⎧⎨⎩
⎧⎨⎩Di[u,v] = min
Di-1[u,v] (don’t use vertex i)Di-1[u,i] + Di-1[i,v] (use vertex i)
Initialize:
General formula i = 1, 2, . . . n:
CS341-Lecture17 13 of 16
O( non. m) -O (n? m)
i
got"
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Correctness. By induction. Initialization gives D0. Assuming Di-1 is correct, the above formula gives the correct Di because we try all possibilities.
D0[u,v] := infty for all u,vfor every vertex u, D0[u,u] := 0for every edge [u,v], D0[u,v] := w(u,v)for i = 1 . . n for u = 1 . . n
for v = 1 . . n Di [u,v] := min { Di-1 [u,v], Di-1[u,i] + Di-1[i,v] }
Floyd-Warshall Algorithm.
Runtime:
Floyd-Warshall Algorithm. All pairs.General formula i = 1, 2, . . . n:
Space:
⎧⎨⎩Di[u,v] = min
Di-1[u,v] (don’t use vertex i)Di-1[u,i] + Di-1[i,v] (use vertex i)
CS341-Lecture17 14 of 16
} initialize
0 (n3) Otis) each Di is nxn matrix .
A. Lubiw, U. WaterlooLecture 17: Dynamic Programming for Shortest PathsCS 341 F21
Floyd-Warshall Algorithm. All pairs.
Reduce space to O(n2) by reusing the same D(u,v): D[u,v] := min { D[u,v], D[u,i] + D[i,v] }
The algorithm finds the lengths of shortest paths.
Finding the actual shortest paths.
Compute Next[u,v] = the first vertex (after u) on a shortest u to v path.If we update
D[u,v] := D[u,i] + D[i,v] then update
Next[u,v] :=
Note that we recover the path going forward (whereas Bellman-Ford used parent pointers going from back to front).
CS341-Lecture17 15 of 16
i
Next Cu . i] natator
A. Lubiw, U. WaterlooCS 341 F21
Summary of Lecture 17
- Bellman-Ford algorithm, single source shortest paths
- Floyd-Warshall algorithm, all pairs shortest paths