week 10.1, monday, oct 21 - purdue university€¦ · slide 1 author: susanne hambrusch created...
TRANSCRIPT
1
Week 10.1, Monday, Oct 21
Homework 5 Due October 26 @ 11:59PM on Gradescope
6.8 Shortest Paths (with Negative Weights)
3
Shortest Paths
Shortest path problem. Given a directed graph G = (V, E), with edge weights cvw, find shortest path from node s to node t.
Ex. Nodes represent agents in a financial setting and cvw is cost of transaction in which we buy from agent v and sell immediately to w.
s
3
t
2
6
7
45
10
18-16
9
6
15 -8
30
20
44
16
11
6
19
6
allow negative weights
Clicker Question
For which of the following directed graphs will Dijkstra find the shortest path from s to t?
4
u
t
s v
2
1
3
-6
u
t
s v
2
1
-3
-6
u
t
s v
-2
3
4
-6
u
t
s v
-6
3
10
-6
A B
C D
E: None of the above
5
Clicker Question
For which of the following directed graphs will Dijkstra find the shortest path from s to t?
6
u
t
s v
2
1
3
-6
u
t
s v
2
1
-3
-6
u
t
s v
-2
3
4
-6
u
t
s v
-6
3
10
-6
A B
C D
E: None of the above
7
Shortest Paths: Dynamic Programming
Def. OPT(i, v) = length of shortest v-t path P using at most i edges.
Case 1: P uses at most i-1 edges.– OPT(i, v) = OPT(i-1, v)
Case 2: P uses exactly i edges.– if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t
path using at most i-1 edges
Remark. By previous observation, if no negative cycles, thenOPT(n-1, v) = length of shortest v-t path.
Fact: If there is a negative cycle then OPT(n,v) < OPT(n-1,v) for some node v
OPT i, v =
0 𝑖𝑖 = 0, 𝑣𝑣 = 𝑡𝑡∞ 𝑖𝑖 = 0, 𝑣𝑣 ≠ 𝑡𝑡
min OPT i − 1, v , min(𝑣𝑣,𝑤𝑤)∈𝐸𝐸
OPT i − 1, w + cvw otherwise
v t…
i-1 edges
v t…wi edges
8
Shortest Paths: Implementation
Analysis. Θ(mn) time, Θ(n2) space.
Finding the shortest paths. Maintain a "successor" for each table entry i.e. if (v,w) is the first edge on the shortest i-edge path P from v to t then Successor[v] = w
Shortest-Path(G, t) {foreach node v ∈ V
M[0, v] ← ∞M[0, t] ← 0
for i = 1 to n-1foreach node v ∈ V
M[i, v] ← M[i-1, v]foreach edge (v, w) ∈ E
M[i, v] ← min { M[i, v], M[i-1, w] + cvw }}
M[i-1,v] no longer used
9
Bellman-Ford: Efficient Implementation
Push-Based-Shortest-Path(G, s, t) {foreach node v ∈ V {
M[v] ← ∞successor[v] ← φ
}
M[t] = 0for i = 1 to n-1 {
foreach node w ∈ V {if (M[w] has been updated in previous iteration){
foreach node v such that (v, w) ∈ E {if (M[v] > M[w] + cvw) {
M[v] ← M[w] + cvwsuccessor[v] ← w
}}
}}If no M[w] value changed in iteration i, stop.
}}
Maintain only one array M[v] = shortest v-t path that we havefound so far.
No need to check edges of the form (v, w) unless M[w] changedin previous iteration.
10
Shortest Paths: Practical Improvements
Practical improvements. Maintain only one array M[v] = shortest v-t path that we have
found so far. No need to check edges of the form (v, w) unless M[w] changed
in previous iteration.
Theorem. Throughout the algorithm, M[v] is length of some v-t path, and after i rounds of updates, the value M[v] is no larger than the length of shortest v-t path using ≤ i edges.
Overall impact. Memory: O(n) additional memory beyond O(m+n) for input (adjacency list). Running time: O(mn) worst case, but substantially faster in practice.
Detect Negative Cycle. Run Outer Loop for n iterations (for i = 1 to n-1 n) If any M[v]changes in iteration n negative cycle
11
All Pairs Shortest PathInput:Graph G=(V,E), directed and weighted, with weights w(e)Output:Shortest path matrix D, where d(u,v) represents the cost of the shortest paths from u to v• The vertices on a shortest path are typically generated on a
need basis
One solution: solve n single-source problems• No negative weights: O(nm +n2logn) time using Dijkstra
12
How do we get started on a dynamic programming formulation?
• We compute n2 entries of matrix D• We do not know how many edges the shortest path from
u to v contains• We do not know in what order vertices are visited • The principle of optimality holds for subpaths in a
shortest path
How do we build up solutions in a systematic way?
u v yx … …
13
A First DP SolutionInput is adjacency matrix A; no negative cycles
d(i,j)r = cost of shortest path from i to j
using at most r edges
We know
• d(i,i)0 = 0 and d(i,j)0 = ∞ for i≠j
• determine d(i,j)r from earlier computed values
• d(i,j)n-1 represent the shortest paths
d(i, j)r = min𝑘𝑘: (k,j)∈E(or k=j)
d(i, k)r−1 + w(k, j)
i ji
k’s
≤ r – 1 edgesO(n2m) time to fill in DP table
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
Floyd-Warshall algorithm
Define cij(k) = weight of a shortest path from i to j
with intermediate vertices belonging to the set {1, 2, …, k}.
i ≤ k ≤ k ≤ k ≤ k j
Thus, δ(i, j) = cij(n).
16
Floyd-Warshall all-pair shortest pathsInput is an adjacency matrix A
c(i,j)k = cost of the shortest path from i to j with intermediate
vertices belonging to set {1, 2, 3, …, k}
• c(i,j)0 = A(i,j)
• c(i,j)n is the final answer
Recursive formulation:c(i,j)k = min { c(i,j)k-1, c(i,k)k-1 + c(k,j)k-1 }
O(n3) time algorithm
Floyd-Warshall recurrencecij
(k) = mink {cij(k–1), cik
(k–1) + ckj(k–1)}
P := shortest path from i to j such that intermediate vertices are in set {1, 2, …, k}
i j
k
icij
(k–1)
cik(k–1) ckj
(k–1)
Case 1: path P only uses intermediate vertices from {1,…,k-1}
Case 2: path P includes node k
Pseudocode for Floyd-Warshall
for k ← 1 to ndo for i ← 1 to n
do for j ← 1 to ndo if cij > cik + ckj
then cij ← cik + ckjrelaxation
• Can drop the superscripts (extra relaxations can’t hurt)• Θ(n3) time.• Simple to code.