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 ➢ Edit distance (aka sequence alignment)
373F19 - Nisarg Shah 3
➢ Traveling salesman problem (TSP)
➢ Problem statement ➢ Ford-Fulkerson algorithm ➢ Running time ➢ Correctness
373F19 - Nisarg Shah 4
➢ 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 5
➢ 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 6
➢ Consider 𝑤𝑜 (the last node before returning to 𝑤1 = 1)
ending at 𝑑
node
➢ 𝑃𝑄𝑈 𝑇, 𝑑 = minimum total travel distance when starting at 1, visiting
each node in 𝑇 exactly once, and ending at 𝑑 ∈ 𝑇
➢ The original answer is min
𝑑∈𝑇 𝑃𝑄𝑈 𝑇, 𝑑 + 𝑒𝑑,1, where 𝑇 = {2, … , 𝑜}
373F19 - Nisarg Shah 7
➢ 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 8
𝑃𝑄𝑈 𝑇, 𝑑 = min
𝑛∈𝑇∖ 𝑑 𝑃𝑄𝑈 𝑇 ∖ 𝑑 , 𝑛 + 𝑒𝑛,𝑑
Final solution = min
𝑑∈ 2,…,𝑜 𝑃𝑄𝑈 2, … , 𝑜 , 𝑑 + 𝑒𝑑,1
➢ But computing the optimal solution with 𝑇 = 𝑙 only requires
storing the optimal solutions with 𝑇 = 𝑙 − 1
➢ Using this observation, how much can we reduce the space
complexity?
373F19 - Nisarg Shah 9
➢ “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 10
373F19 - Nisarg Shah 11
➢ A directed graph 𝐻 = (𝑊, 𝐹) ➢ Edge capacities 𝑑 ∶ 𝐹 → ℝ≥0 ➢ Source node 𝑡, target node 𝑢
➢ Maximum “flow” from 𝑡 to 𝑢
373F19 - Nisarg Shah 12
➢ For simplicity, assume that… ➢ No edges enter 𝑡 ➢ No edges leave 𝑢 ➢ Edge capacity 𝑑(𝑓) is a non-
negative integer
when 𝑑(𝑓) can be a rational number
373F19 - Nisarg Shah 13
➢ An 𝑡-𝑢 flow is a function 𝑔: 𝐹 → ℝ≥0 ➢ Intuitively, 𝑔(𝑓) is the “amount of material” carried on
edge 𝑓
373F19 - Nisarg Shah 14
∀𝑓 ∈ 𝐹 ∶ 0 ≤ 𝑔 𝑓 ≤ 𝑑(𝑓)
∀𝑤 ∈ 𝑊 ∖ 𝑡, 𝑢 ∶ σ𝑓 entering 𝑤 𝑔 𝑓 = σ𝑓 leaving 𝑤 𝑔 𝑓
Flow in = flow out at every node other than 𝑡 and 𝑢 Flow out at 𝑡 = flow in at 𝑢
373F19 - Nisarg Shah 15
➢ Given a directed graph 𝐻 = (𝑊, 𝐹) with edge capacities
𝑑: 𝐹 → ℝ≥0, find a flow 𝑔∗ with the maximum value.
373F19 - Nisarg Shah 16
1. Start from zero flow (𝑔 𝑓 = 0 for each 𝑓). 2. While there exists an 𝑡-𝑢 path 𝑄 in 𝐻 such that 𝑔 𝑓 < 𝑑(𝑓) for each 𝑓 ∈ 𝑄 a. Find one such path 𝑄 b. Increase the flow on each edge 𝑓 ∈ 𝑄 by min
𝑓∈𝑄 𝑑 𝑓 − 𝑔 𝑓
373F19 - Nisarg Shah 17
373F19 - Nisarg Shah 18
373F19 - Nisarg Shah 19
373F19 - Nisarg Shah 20
373F19 - Nisarg Shah 21
373F19 - Nisarg Shah 22
373F19 - Nisarg Shah 23
373F19 - Nisarg Shah 24
is not allowed to decrease it.
bad decisions
373F19 - Nisarg Shah 25
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 26
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 27
➢ 𝐻𝑔 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 28
s t u v
20/20 20/30 20/20 0/10 0/10
s t u v
𝟑𝟏 𝟐𝟏 𝟑𝟏 𝟐𝟏 𝟐𝟏 𝟑𝟏
Flow 𝑔 Residual graph 𝐻𝑔
373F19 - Nisarg Shah 29
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 30
s t u v
20/20 20/30 20/20 0/10 0/10
Flow 𝑔 s t u v
𝟑𝟏 𝟐𝟏 𝟑𝟏 𝟐𝟏 𝟐𝟏
Residual graph 𝐻𝑔 Path 𝑸 → send flow = bottleneck = 10
𝟑𝟏
s t u v
20/20 10/30 20/20 10/10 10/10
New flow 𝑔
373F19 - Nisarg Shah 31
s t u v
𝟑𝟏 𝟑𝟏 𝟑𝟏 𝟐𝟏 𝟐𝟏
New residual graph 𝐻𝑔 No 𝒕-𝒖 path because no outgoing edge from 𝒕
𝟐𝟏
373F19 - Nisarg Shah 32
➢ 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 33
➢ Each node on the path (except 𝑡 and 𝑢) has exactly two
incident edges
s t +𝑦 +𝑦 −𝑦 −𝑦 +𝑦
373F19 - Nisarg Shah 34
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 35
➢ #Augmentations:
➢ Time for an augmentation:
➢ Total time: 𝑃( 𝑛 + 𝑜 ⋅ 𝐷)
373F19 - Nisarg Shah 36
➢ 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 37
➢ 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 38
➢ Number of integers provided as input ➢ Total number of bits provided as input
➢ Strongly polynomial time
➢ Weakly polynomial time
➢ Pseudo-polynomial time
number of “unary digits” required to write input)
373F19 - Nisarg Shah 39
➢ Find the shortest augmenting path using BFS
➢ Find the maximum bottleneck capacity augmenting path
➢ …
373F19 - Nisarg Shah 40
➢ 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 ൗ
𝑛 𝑜 log 𝑜
King-Rao-Tarjan
➢ 2013: 𝑃(𝑜 𝑛) Orlin
373F19 - Nisarg Shah 41
must terminate in 𝑃 𝑛 + 𝑜 ⋅ 𝐷 time
algorithm must terminate with the optimal flow
373F19 - Nisarg Shah 42
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 43
➢ For a node 𝑣: 𝑔𝑝𝑣𝑢 𝑣 , 𝑔𝑗𝑜(𝑣) = total flow out of and into 𝑣 ➢ For a set of nodes 𝑌: 𝑔𝑝𝑣𝑢 𝑌 , 𝑔𝑗𝑜(𝑌) defined similarly
➢ Capacity: 0 ≤ 𝑔 𝑓 ≤ 𝑑(𝑓) ➢ Flow conservation: 𝑔𝑝𝑣𝑢 𝑣 = 𝑔𝑗𝑜(𝑣) for all 𝑣 ≠ 𝑡, 𝑢
373F19 - Nisarg Shah 44
𝐵 ∩ 𝐶 = ∅) with 𝑡 ∈ 𝐵, and 𝑢 ∈ 𝐶
373F19 - Nisarg Shah 45
𝑤 𝑔 = 𝑔𝑝𝑣𝑢 𝐵 − 𝑔𝑗𝑜(𝐵)
constraint over all nodes in 𝐵
373F19 - Nisarg Shah 46
𝑤 𝑔 ≤ 𝑑𝑏𝑞(𝐵, 𝐶)
𝑤 𝑔 = 𝑔𝑝𝑣𝑢 𝐵 − 𝑔𝑗𝑜 𝐵 ≤ 𝑔𝑝𝑣𝑢 𝐵 = σ𝑓 leaving 𝐵 𝑔(𝑓) ≤ σ𝑓 leaving 𝐵 𝑑(𝑓) = 𝑑𝑏𝑞(𝐵, 𝐶)
373F19 - Nisarg Shah 47
𝑤 𝑔 ≤ 𝑑𝑏𝑞(𝐵, 𝐶)
➢ Value of flow generated by Ford-Fulkerson = capacity of some cut
➢ 1) Max flow = min cut ➢ 2) Ford-Fulkerson generates max flow.
373F19 - Nisarg Shah 48
➢ 𝑔 = flow returned by Ford-Fulkerson ➢ 𝐵∗ = nodes reachable from 𝑡 in 𝐻𝑔 ➢ 𝐶∗ = remaining nodes 𝑊 ∖ 𝐵∗ ➢ Note: We look at the residual graph 𝐻𝑔, but define the cut in 𝐻
Graph 𝐻
𝐻𝑔
373F19 - Nisarg Shah 49
➢ Claim: 𝐵∗, 𝐶∗ is a valid cut
paths in 𝐻𝑔, so 𝑢 ∉ 𝐵∗
Graph 𝐻
𝐻𝑔
373F19 - Nisarg Shah 50
➢ Blue edges = edges going out of 𝐵∗ in 𝐻 ➢ Red edges = edges coming into 𝐵∗ in 𝐻
Graph 𝐻
𝐻𝑔
373F19 - Nisarg Shah 51
➢ Each blue edge 𝑣, 𝑤 must be saturated
➢ Each red edge (𝑤, 𝑣) must have zero flow
Graph 𝐻
𝐻𝑔
373F19 - Nisarg Shah 52
➢ Each blue edge 𝑣, 𝑤 must be saturated ⇒ 𝑔𝑝𝑣𝑢 𝐵∗ = 𝑑𝑏𝑞(𝐵∗, 𝐶∗) ➢ Each red edge (𝑤, 𝑣) must have zero flow ⇒ 𝑔𝑗𝑜 𝐵∗ = 0 ➢ So 𝑤 𝑔 = 𝑔𝑝𝑣𝑢 𝐵∗ − 𝑔𝑗𝑜(𝐵∗) = 𝑑𝑏𝑞 𝐵∗, 𝐶∗ ∎
Graph 𝐻
𝐻𝑔
373F19 - Nisarg Shah 53
In any graph, the value of the maximum flow is equal to the capacity of the minimum cut.
➢ Run Ford-Fulkerson to find a max flow 𝑔 ➢ Construct its residual graph 𝐻𝑔 ➢ Let 𝐵∗ = set of all nodes reachable from 𝑡 in 𝐻𝑔
➢ Then (𝐵∗, 𝑊 ∖ 𝐵∗) is a min cut
373F19 - Nisarg Shah 54
Question
iteration, it terminates with a final flow value of 1.
(a) 𝐻 has a single 𝑡-𝑢 path. (b) 𝐻 has an edge 𝑓 such that all 𝑡-𝑢 paths go through 𝑓. (c) The minimum cut capacity in 𝐻 is greater than 1. (d) The minimum cut capacity in 𝐻 is less than 1.
373F19 - Nisarg Shah 55
like an algorithmic framework
➢ It seems more like a single problem
versatile single problem