CSC373 Week 4: Dynamic Programming (contd) Network Flow (start)
373F19 - Nisarg Shah 1
Nisarg Shah 373F19 - Nisarg Shah 1 Recap Dynamic Programming - - PowerPoint PPT Presentation
CSC373 Week 4: Dynamic Programming (contd) Network Flow (start) Nisarg Shah 373F19 - Nisarg Shah 1 Recap Dynamic Programming Basics Optimal substructure property Bellman equation Top-down (memoization) vs bottom-up
373F19 - Nisarg Shah 1
373F19 - Nisarg Shah 2
➢ Optimal substructure property ➢ Bellman equation ➢ Top-down (memoization) vs bottom-up implementations
➢ Weighted interval scheduling ➢ Knapsack problem ➢ Single-source shortest paths ➢ Chain matrix product
373F19 - Nisarg Shah 3
➢ Edit distance (aka sequence alignment) ➢ Traveling salesman problem (TSP)
➢ Problem statement ➢ Ford-Fulkerson algorithm ➢ Running time ➢ Correctness
➢ How similar are strings 𝑌 = 𝑦1, … , 𝑦𝑛 and 𝑍 = 𝑧1, … , 𝑧𝑜?
➢ We can do these operations on any symbol in either string ➢ How many deletions & replacements does it take to match
the two strings?
373F19 - Nisarg Shah 4
373F19 - Nisarg Shah 5
6 replacements, 1 deletion 1 replacement, 1 deletion
➢ Input
➢ Goal
➢ Want to delete/replace at one end and recurse
373F19 - Nisarg Shah 6
➢ Goal: match 𝑦1, … , 𝑦𝑛 and 𝑧1, … , 𝑧𝑜 ➢ Consider the last symbols 𝑦𝑛 and 𝑧𝑜 ➢ Three options:
➢ Hence in the DP, we need to compute the optimal solutions
for matching 𝑦1, … , 𝑦𝑗 with 𝑧1, … , 𝑧𝑘 for all (𝑗, 𝑘)
373F19 - Nisarg Shah 7
𝐹 𝑗, 𝑘 = if 𝑗 = 𝑘 = 0 𝑒 𝑧𝑘 + 𝐹[𝑗, 𝑘 − 1] if 𝑗 = 0 ∧ 𝑘 > 0 𝑒 𝑦𝑗 + 𝐹[𝑗 − 1, 𝑘] if 𝑗 > 0 ∧ 𝑘 = 0 min{𝐵, 𝐶, 𝐷}
where 𝐵 = 𝑒 𝑦𝑗 + 𝐹 𝑗 − 1, 𝑘 , 𝐶 = 𝑒 𝑧𝑘 + 𝐹 𝑗, 𝑘 − 1 𝐷 = 𝑠 𝑦𝑗, 𝑧𝑘 + 𝐹[𝑗 − 1, 𝑘 − 1]
373F19 - Nisarg Shah 8
373F19 - Nisarg Shah 9
𝐹 𝑗, 𝑘 = if 𝑗 = 𝑘 = 0 𝑒 𝑧𝑘 + 𝐹[𝑗, 𝑘 − 1] if 𝑗 = 0 ∧ 𝑘 > 0 𝑒 𝑦𝑗 + 𝐹[𝑗 − 1, 𝑘] if 𝑗 > 0 ∧ 𝑘 = 0 min{𝐵, 𝐶, 𝐷}
where 𝐵 = 𝑒 𝑦𝑗 + 𝐹 𝑗 − 1, 𝑘 , 𝐶 = 𝑒 𝑧𝑘 + 𝐹 𝑗, 𝑘 − 1 𝐷 = 𝑠 𝑦𝑗, 𝑧𝑘 + 𝐹[𝑗 − 1, 𝑘 − 1]
➢ To compute 𝐹[⋅, 𝑘], we only need 𝐹 ⋅, 𝑘 − 1 stored ➢ So we can forget 𝐹[⋅, 𝑘] as soon as we reach 𝑘 + 2 ➢ But this is not enough if we want to compute the actual
solution (sequence of operations)
373F19 - Nisarg Shah 10
time and 𝑃(𝑜 + 𝑛) space too!
This slide is not in the scope of the course
373F19 - Nisarg Shah 11
𝑒(𝑦𝑗) 𝑒(𝑧𝑘) This slide is not in the scope of the course
373F19 - Nisarg Shah 12
➢ 𝐹[𝑗, 𝑘] = length of shortest path from (0,0) to (𝑗, 𝑘)
𝑒(𝑦𝑗) 𝑒(𝑧𝑘) This slide is not in the scope of the course
373F19 - Nisarg Shah 13
➢ Shortest path from (0,0) to (𝑛, 𝑜) passes through (𝑟, Τ
𝑜 2)
where 𝑟 minimizes length of shortest path from (0,0) to (𝑟, Τ
𝑜 2) + length of shortest path from (𝑟, Τ 𝑜 2) to (𝑛, 𝑜) This slide is not in the scope of the course
373F19 - Nisarg Shah 14
➢ Find 𝑟 using divide-and-conquer ➢ Find shortest paths from (0,0) to (𝑟, Τ
𝑜 2) and (𝑟, Τ 𝑜 2) to
(𝑛, 𝑜) using DP
This slide is not in the scope of the course
373F19 - Nisarg Shah 15
373F19 - Nisarg Shah 16
➢ Directed graph 𝐻 = (𝑊, 𝐹) ➢ Distance 𝑒𝑗,𝑘 is the distance from node 𝑗 to node 𝑘
➢ Minimum distance which needs to be traveled to start
from some node 𝑤, visit every other node exactly once, and come back to 𝑤
373F19 - Nisarg Shah 17
➢ Let’s start at node 𝑤1 = 1
➢ Want to visit the other nodes in some order, say 𝑤2, … , 𝑤𝑜 ➢ Total distance is 𝑒1,𝑤2 + 𝑒𝑤2,𝑤3 + ⋯ + 𝑒𝑤𝑜−1,𝑤𝑜 + 𝑒𝑤𝑜,1
➢ Check all possible orderings ➢ 𝑜 − 1 ! = Θ
𝑜 ⋅
𝑜 𝑓 𝑜
(Stirling’s approximation)
373F19 - Nisarg Shah 18
➢ Consider 𝑤𝑜 (the last node before returning to 𝑤1 = 1)
2, … , 𝑜 ∖ 𝑑
to visit and where we need to end
➢ 𝑃𝑄𝑈 𝑇, 𝑑 = minimum total distance of starting at 1,
visiting each node in 𝑇 exactly once, and ending at 𝑑 ∈ 𝑇 (without counting the distance for returning from 𝑑 to 1)
as min
𝑑∈𝑇 𝑃𝑄𝑈 𝑇, 𝑑 + 𝑒𝑑,1, where 𝑇 = {2, … , 𝑜}
373F19 - Nisarg Shah 19
➢ To compute 𝑃𝑄𝑈[𝑇, 𝑑], we condition over the vertex
which is visited right before 𝑑
𝑃𝑄𝑈 𝑇, 𝑑 = min
𝑛∈𝑇∖ 𝑑 𝑃𝑄𝑈 𝑇 ∖ 𝑑 , 𝑛 + 𝑒𝑛,𝑑
Final solution = min
𝑑∈ 2,…,𝑜 𝑃𝑄𝑈 2, … , 𝑜 , 𝑑 + 𝑒𝑑,1
➢ Much better than the naïve solution which has
Τ
𝑜 𝑓 𝑜
373F19 - Nisarg Shah 20
𝑃𝑄𝑈 𝑇, 𝑑 = min
𝑛∈𝑇∖ 𝑑 𝑃𝑄𝑈 𝑇 ∖ 𝑑 , 𝑛 + 𝑒𝑛,𝑑
Final solution = min
𝑑∈ 2,…,𝑜 𝑃𝑄𝑈 2, … , 𝑜 , 𝑑 + 𝑒𝑑,1
➢ But computing the optimal solution with 𝑇 = 𝑙 only requires
storing the optimal solutions with 𝑇 = 𝑙 − 1
the space complexity?
373F19 - Nisarg Shah 21
➢ “Generalize” the problem first
𝑦1, … , 𝑦𝑛 and 𝑍 = 𝑧1, … , 𝑧𝑜, we compute 𝐹[𝑗, 𝑘] = edit distance between 𝑗-prefix of 𝑌 and 𝑘-prefix of 𝑍 for all (𝑗, 𝑘)
structure of the “subproblem” which must be solved optimally to get an optimal solution to the overall problem
➢ Remember the difference between DP and divide-and-
conquer
➢ Sometimes you can save quite a bit of space by only
storing solutions to those subproblems that you need in the future
373F19 - Nisarg Shah 22
373F19 - Nisarg Shah 23
➢ A directed graph 𝐻 = (𝑊, 𝐹) ➢ Edge capacities 𝑑 ∶ 𝐹 → ℝ≥0 ➢ Source node 𝑡, target node 𝑢
➢ Maximum “flow” from 𝑡 to 𝑢
373F19 - Nisarg Shah 24
➢ For simplicity, assume that… ➢ No edges enters 𝑡 ➢ No edges comes out of 𝑢 ➢ Edge capacity 𝑑(𝑓) is a non-
negative integer
when 𝑑(𝑓) can be a rational number
373F19 - Nisarg Shah 25
➢ An 𝑡-𝑢 flow is a function 𝑔: 𝐹 → ℝ≥0 ➢ Intuitively, 𝑔(𝑓) is the “amount of material” carried on
edge 𝑓
373F19 - Nisarg Shah 26
∀𝑓 ∈ 𝐹 ∶ 0 ≤ 𝑔 𝑓 ≤ 𝑑(𝑓)
∀𝑤 ∈ 𝑊 ∖ 𝑡, 𝑢 ∶ σ𝑓 into 𝑤 𝑔 𝑓 = σ𝑓 leaving 𝑤 𝑔 𝑓
Flow in = flow out at every node other than 𝑡 and 𝑢 Flow out at 𝑡 = flow in at 𝑢
373F19 - Nisarg Shah 27
➢ Given a directed graph 𝐻 = (𝑊, 𝐹) with edge capacities
𝑑: 𝐹 → ℝ≥0, find a flow 𝑔∗ with the maximum value.
373F19 - Nisarg Shah 28
𝑔 𝑓 < 𝑑(𝑓) for each 𝑓 ∈ 𝑄
a. Find one such path 𝑄 b. Increase the flow on each edge 𝑓 ∈ 𝑄 by min
𝑓∈𝑄 𝑑 𝑓 − 𝑔 𝑓
373F19 - Nisarg Shah 29
373F19 - Nisarg Shah 30
373F19 - Nisarg Shah 31
373F19 - Nisarg Shah 32
373F19 - Nisarg Shah 33
373F19 - Nisarg Shah 34
373F19 - Nisarg Shah 35
373F19 - Nisarg Shah 36
is not allowed to decrease it.
bad decisions
373F19 - Nisarg Shah 37
s t u v
𝟑𝟏/20 𝟑𝟏/30 𝟑𝟏/20 0/10 0/10
Suppose we start by sending 20 units of flow along this path s t u v
𝟑𝟏/20 𝟐𝟏/30 𝟑𝟏/20 𝟐𝟏/10 𝟐𝟏/10
But the optimal configuration requires 10 fewer units of flow on 𝑣 → 𝑤
373F19 - Nisarg Shah 38
We can essentially send a “reverse” flow of 10 units along 𝑤 → 𝑣 s t u v
𝟑𝟏/20 𝟐𝟏/30 𝟑𝟏/20 𝟐𝟏/10 𝟐𝟏/10
So now we get this optimal flow s t u v
𝟑𝟏/20 𝟑𝟏/30 𝟑𝟏/20 𝟐𝟏/10 𝟐𝟏/10 𝟐𝟏
373F19 - Nisarg Shah 39
➢ 𝐻𝑔 has the same vertices as 𝐻 ➢ For each edge e = (𝑣, 𝑤) in 𝐻, 𝐻𝑔 has at most two edges
amount by which we can reduce flow on 𝑓, which is 𝑔(𝑓)
373F19 - Nisarg Shah 40
s t u v
20/20 20/30 20/20 0/10 0/10
s t u v
𝟑𝟏 𝟐𝟏 𝟑𝟏 𝟐𝟏 𝟐𝟏 𝟑𝟏
Flow 𝑔 Residual graph 𝐻𝑔
373F19 - Nisarg Shah 41
all edges in 𝑄
units of flow along 𝑄
➢ What does it mean to send 𝑦 units of flow along 𝑄? ➢ For each forward edge 𝑓 ∈ 𝑄, increase the flow on 𝑓 by 𝑦 ➢ For each reverse edge 𝑓𝑠𝑓𝑤 ∈ 𝑄, decrease the flow on 𝑓 by 𝑦
373F19 - Nisarg Shah 42
➢ If we increase flow on 𝑓, we can do so by at most the
capacity of forward edge 𝑓 in 𝐻𝑔, which is 𝑑 𝑓 − 𝑔 𝑓
= 𝑑(𝑓)
➢ If we decrease flow on 𝑓, we can do so by at most the
capacity of reverse edge 𝑓𝑠𝑓𝑤 in 𝐻𝑔, which is 𝑔 𝑓
373F19 - Nisarg Shah 43
➢ Each node on the path (except 𝑡 and 𝑢) has exactly two
incident edges
s t +𝑦 +𝑦 −𝑦 −𝑦 +𝑦
373F19 - Nisarg Shah 44
MaxFlow(𝐻):
// initialize: Set 𝑔 𝑓 = 0 for all 𝑓 in 𝐻 // while there is an 𝑡-𝑢 path in 𝐻𝑔: While 𝑄 = FindPath(s, t,Residual(𝐻, 𝑔))!=None: 𝑔 = Augment(𝑔, 𝑄) UpdateResidual(𝐻,𝑔) EndWhile Return 𝑔
373F19 - Nisarg Shah 45
➢ #Augmentations:
➢ Time for an augmentation:
➢ Total time: 𝑃( 𝑛 + 𝑜 ⋅ 𝐷)
373F19 - Nisarg Shah 46
➢ This is pseudo-polynomial time ➢ 𝐷 can be exponentially large in the input length (the number
➢ Note: We assumed integer capacities, but this also gives a
pseudo-polynomial time algorithm for rational capacities
373F19 - Nisarg Shah 47
➢ Not if we choose an arbitrary path in 𝐻𝑔 at each step ➢ In the graph below, we might end up repeatedly sending 1
unit of flow across 𝑏 → 𝑐 and then reversing it
373F19 - Nisarg Shah 48
➢ Find the shortest augmenting path using BFS
➢ Find the maximum bottleneck capacity augmenting path
depends on the number of bits used to write integers)
➢ …
373F19 - Nisarg Shah 49
➢ 1972: 𝑃 𝑜 𝑛2 Edmonds-Karp ➢ 1980: 𝑃 𝑜 𝑛 log2 𝑜 Galil-Namaad ➢ 1983: 𝑃 𝑜 𝑛 log 𝑜 Sleator-Tarjan ➢ 1986: 𝑃 𝑜 𝑛 log
Τ
𝑜2 𝑛
Goldberg-Tarjan
➢ 1992: 𝑃 𝑜 𝑛 + 𝑜2+𝜗 King-Rao-Tarjan ➢ 1996: 𝑃 𝑜 𝑛 log
ൗ
𝑛 𝑜 log 𝑜 𝑜 King-Rao-Tarjan
➢ 2013: 𝑃(𝑜 𝑛) Orlin
373F19 - Nisarg Shah 50
must do so in 𝑃 𝑛 + 𝑜 ⋅ 𝐷 time
algorithm must terminate with the optimal flow
373F19 - Nisarg Shah 51
(i.e. 𝐵 ∪ 𝐶 = 𝑊, 𝐵 ∩ 𝐶 = ∅), 𝑡 ∈ 𝐵, and 𝑢 ∈ 𝐶
373F19 - Nisarg Shah 52
𝑤 𝑔 = 𝑔𝑝𝑣𝑢 𝐵 − 𝑔𝑗𝑜(𝐵)
(exercise!)
373F19 - Nisarg Shah 53
𝑤 𝑔 ≤ 𝑑𝑏𝑞(𝐵, 𝐶)
𝑤 𝑔 = 𝑔𝑝𝑣𝑢 𝐵 − 𝑔𝑗𝑜 𝐵 ≤ 𝑔𝑝𝑣𝑢 𝐵 =
𝑓 leaving 𝐵
𝑔 𝑓 ≤
𝑓 leaving 𝐵
𝑑 𝑓 = 𝑑𝑏𝑞(𝐵, 𝐶)
373F19 - Nisarg Shah 54
𝑤 𝑔 ≤ 𝑑𝑏𝑞(𝐵, 𝐶)
capacity of any cut.
equal to the minimum capacity of any cut.
➢ To demonstrate the correctness (i.e. optimality) of Ford-
Fulkerson algorithm, all we need to show is that the flow it generates is equal to the capacity of some cut.
373F19 - Nisarg Shah 55
➢ Let 𝑔∗ denote the flow returned by Ford-Fulkerson. ➢ Look at 𝐻𝑔∗ but define a cut in 𝐻
373F19 - Nisarg Shah 56
➢ 𝐵∗, 𝐶∗ is a valid cut because there is no 𝑡-𝑢 path in 𝐻𝑔∗
when Ford-Fulkerson terminates, so 𝑢 ∉ 𝐵∗
373F19 - Nisarg Shah 57
➢ Blue edges = edges going out of 𝐵∗ in 𝐻 ➢ Red edges = edges coming into 𝐵∗ in 𝐻
373F19 - Nisarg Shah 58
➢ Each blue edge 𝑣, 𝑤 must be saturated
➢ Each red edge (𝑤, 𝑣) must have zero flow
373F19 - Nisarg Shah 59
➢ Each blue edge 𝑣, 𝑤 must be saturated ➢ Each red edge (𝑤, 𝑣) must have zero flow ➢ So 𝑤 𝑔∗ = 𝑑𝑏𝑞 𝐵∗, 𝐶∗ ∎
373F19 - Nisarg Shah 60
flow is equal to the capacity of the minimum cut.
be used to find the min cut
➢ Find the max flow 𝑔∗ ➢ Let 𝐵∗ = set of all nodes reachable from 𝑡 in 𝐻𝑔∗
➢ Then (𝐵∗, 𝑊 ∖ 𝐵∗) is min cut
373F19 - Nisarg Shah 61
programming, this doesn’t seem like a framework
➢ It is more like a single problem
this single problem
➢ Hence, it is a very versatile technique