CSC373 Week 5: Network Flow (contd)
373F19 - Nisarg Shah & Karan Singh 1
Nisarg Shah 373F19 - Nisarg Shah & Karan Singh 1 Recap Some - - PowerPoint PPT Presentation
CSC373 Week 5: Network Flow (contd) Nisarg Shah 373F19 - Nisarg Shah & Karan Singh 1 Recap Some more DP Edit distance (aka sequence alignment) Traveling salesman problem (TSP) Start of network flow Problem statement
373F19 - Nisarg Shah & Karan Singh 1
373F19 - Nisarg Shah & Karan Singh 2
➢ Edit distance (aka sequence alignment) ➢ Traveling salesman problem (TSP)
➢ Problem statement ➢ Ford-Fulkerson algorithm ➢ Running time ➢ Correctness using max-flow, min-cut
373F19 - Nisarg Shah & Karan Singh 3
➢ Edmonds-Karp algorithm (shortest augmenting path)
➢ Bipartite matching & Hall’s theorem ➢ Edge-disjoint paths & Menger’s theorem ➢ Multiple sources/sinks ➢ Circulation networks ➢ Lower bounds on flows ➢ Survey design ➢ Image segmentation
373F19 - Nisarg Shah & Karan Singh 4
➢ 𝐻𝑔 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 & Karan Singh 5
s t u v
20/20 20/30 20/20 0/10 0/10
s t u v
𝟑𝟏 𝟐𝟏 𝟑𝟏 𝟐𝟏 𝟐𝟏 𝟑𝟏
Flow 𝑔 Residual graph 𝐻𝑔
373F19 - Nisarg Shah & Karan Singh 6
373F19 - Nisarg Shah & Karan Singh 7
➢ #Augmentations:
➢ Time for an augmentation:
➢ Total time: 𝑃( 𝑛 + 𝑜 ⋅ 𝐷)
373F19 - Nisarg Shah & Karan Singh 8
MaxFlow(𝐻): // initialize: Set 𝑔 𝑓 = 0 for all 𝑓 in 𝐻 // Find shortest 𝑡-𝑢 path in 𝐻𝑔 & augment: While 𝑄 = BFS(s, t,Residual(𝐻, 𝑔))!=None: 𝑔 = Augment(𝑔, 𝑄) UpdateResidual(𝐻,𝑔) EndWhile Return 𝑔
Minimum number of edges
373F19 - Nisarg Shah & Karan Singh 9
➢ Lemma 1: The length of the shortest 𝑡 → 𝑢 path in 𝐻𝑔
➢ Lemma 2: After at most 𝑛 augmentations, the length of
➢ Theorem: The algorithm takes 𝑃(𝑛2𝑜) time.
373F19 - Nisarg Shah & Karan Singh 10
➢ Level: ℓ(𝑤) = length of shortest 𝑡 → 𝑤 path ➢ Level graph 𝑀𝐻 = (𝑊, 𝐹𝑀) is a subgraph of 𝐻 where we
373F19 - Nisarg Shah & Karan Singh 11
➢ Level: ℓ(𝑤) = length of shortest 𝑡 → 𝑤 path ➢ Level graph 𝑀𝐻 = (𝑊, 𝐹𝑀) is a subgraph of 𝐻 where we
373F19 - Nisarg Shah & Karan Singh 12
➢ Length of the shortest 𝑡 → 𝑢 path in 𝐻𝑔 never decreases.
➢ Let 𝑔 and 𝑔′ be flows before and after an augmentation
Residual graph 𝐻𝑔 Residual graph 𝐻𝑔′
373F19 - Nisarg Shah & Karan Singh 13
➢ Length of the shortest 𝑡 → 𝑢 path in 𝐻𝑔 never decreases.
➢ Let 𝑔 and 𝑔′ be flows before and after an augmentation
➢ Augmentation happens along a path in 𝑀𝐻𝑔 ➢ For each edge on the path, we either remove it, add an
➢ Opposite direction edges can’t help reduce the length of
➢ QED!
373F19 - Nisarg Shah & Karan Singh 14
➢ After at most 𝑛 augmentations, the length of the
➢ In each augmentation step, we remove at least one edge
path equal to its capacity
➢ No new edges are added in 𝑀𝐻𝑔 unless the length of the
➢ This cannot happen more than 𝑛 times! ∎
373F19 - Nisarg Shah & Karan Singh 15
➢ Lemma 1: The length of the shortest 𝑡 → 𝑢 path in 𝐻𝑔
➢ Lemma 2: After at most 𝑛 augmentations, the length of
➢ Theorem: The algorithm takes 𝑃(𝑛2𝑜) time.
373F19 - Nisarg Shah & Karan Singh 16
➢ Some graphs require Ω(𝑛𝑜) augmentation steps ➢ But we may be able to reduce the time to run each
➢ Dinitz’s algorithm [1970] ⇒ 𝑃(𝑛𝑜2) ➢ Sleator–Tarjan algorithm [1983] ⇒ 𝑃(𝑛 𝑜 log 𝑜)
373F19 - Nisarg Shah & Karan Singh 17
373F19 - Nisarg Shah & Karan Singh 18
Rail network connecting Soviet Union with Eastern European countries
(Tolstoǐ 1930s)
373F19 - Nisarg Shah & Karan Singh 19
Rail network connecting Soviet Union with Eastern European countries
(Tolstoǐ 1930s) Min-cut
373F19 - Nisarg Shah & Karan Singh 20
➢ If edge capacities are integers, then the max-flow
➢ Easy to check that each augmentation step preserves
373F19 - Nisarg Shah & Karan Singh 21
➢ Given a bipartite graph 𝐻 = (𝑉 ∪ 𝑊, 𝐹), find a maximum
373F19 - Nisarg Shah & Karan Singh 22
➢ Add a source node 𝑡 and target node 𝑢 ➢ Add edges, all of capacity 1:
𝑉 𝑊 𝑉 𝑊
373F19 - Nisarg Shah & Karan Singh 23
➢ There is a 1-1 correspondence between matchings of size
➢ Take a matching 𝑁 =
➢ Construct the corresponding unique flow 𝑔
𝑁 where…
➢ This flow has value 𝑙
373F19 - Nisarg Shah & Karan Singh 24
➢ There is a 1-1 correspondence between matchings of size
➢ Take any flow 𝑔 with value 𝑙 ➢ The corresponding unique matching 𝑁
𝑔 = set of edges
vertices in 𝑉
373F19 - Nisarg Shah & Karan Singh 25
➢ where 𝑜 = 𝑉 = 𝑊
➢ 𝑃(𝑛 ⋅ 𝑜 ⋅ 𝐷), where 𝐷 = sum of capacities of edges
➢ Q: What’s the runtime when used for bipartite matching?
➢ Dinitz’s algorithm runs in time 𝑃 𝑛 𝑜 when all edge
373F19 - Nisarg Shah & Karan Singh 26
➢ Well, when the corresponding flow network has value 𝑜 ➢ But can we interpret this condition in terms of edges of the
➢ For 𝑇 ⊆ 𝑉, let 𝑂 𝑇 ⊆ 𝑊 be the set of all nodes in 𝑊 adjacent
➢ If 𝐻 has a perfect matching, 𝑂 𝑇
➢ Because each node in 𝑇 must be matched to a distinct node
373F19 - Nisarg Shah & Karan Singh 27
➢ All 𝑉 → 𝑊 edges now have ∞ capacity ➢ 𝑡 → 𝑉 and 𝑊 → 𝑢 edges are still unit capacity
𝑉 𝑊 𝑉 𝑊 ∞ ∞ 1 1 1 1
373F19 - Nisarg Shah & Karan Singh 28
➢ 𝐻 has a perfect matching iff 𝑂 𝑇
➢ Suppose 𝐻 doesn’t have a perfect matching
➢ 𝑑𝑏𝑞 𝐵, 𝐶 = 𝑉 ∩ 𝐶 + 𝑊 ∩ 𝐵 < 𝑜 = 𝑉
≤ 𝑊 ∩ 𝐵 < |𝑉 ∩ 𝐵|. ∎
373F19 - Nisarg Shah & Karan Singh 29
➢ 1955: 𝑃(𝑛𝑜2) → Ford-Fulkerson ➢ 1973: 𝑃 𝑛 𝑜 → blocking flow (Hopcroft-Karp, Karzanov) ➢ 2004: 𝑃 𝑜2.378 → fast matrix multiplication (Mucha–
➢ 2013: ෨
Τ 10 7 → electrical flow (Mądry)
➢ Best running time is still an open question
➢ Hall’s theorem → Tutte’s theorem ➢ 1965: 𝑃(𝑜4) → Blossom algorithm (Edmonds) ➢ 1980/1994: 𝑃 𝑛 𝑜 → Micali-Vazirani
373F19 - Nisarg Shah & Karan Singh 30
➢ Given a directed graph 𝐻 = (𝑊, 𝐹), two nodes 𝑡 and 𝑢,
➢ Two 𝑡 → 𝑢 paths 𝑄 and 𝑄′ are edge-disjoint if they don’t
373F19 - Nisarg Shah & Karan Singh 31
➢ Communication networks
➢ Assign unit capacity on all edges
373F19 - Nisarg Shah & Karan Singh 32
➢ There is 1-1 correspondence between 𝑙 edge-disjoint
➢ If 𝑄
1, … , 𝑄𝑙 are 𝑙 edge-disjoint 𝑡 → 𝑢 paths, define the
➢ 𝑔 𝑓 = 1 whenever 𝑓 ∈ 𝑄
1 ∪ ⋯ ∪ 𝑄𝑙 and 0 otherwise
➢ Since paths are edge-disjoint, it satisfies flow
373F19 - Nisarg Shah & Karan Singh 33
➢ There is 1-1 correspondence between 𝑙 edge-disjoint
➢ Let 𝑔 be an integral flow of value 𝑙 ➢ 𝑙 outgoing edges from 𝑡 have unit flow ➢ Pick one such edge (𝑡, 𝑣1)
haven’t used up yet).
➢ Repeat this for the other 𝑙 − 1 edges coming out of 𝑡
373F19 - Nisarg Shah & Karan Singh 34
➢ Equals max flow in this network ➢ By max-flow min-cut theorem, also equals minimum cut ➢ Exercise: minimum cut = minimum number of edges we
373F19 - Nisarg Shah & Karan Singh 35
➢ Show that to compute the maximum number of edge-
➢ In any directed/undirected graph, the maximum number
373F19 - Nisarg Shah & Karan Singh 36
➢ Given a directed graph 𝐻 = (𝑊, 𝐹) with edge capacities
373F19 - Nisarg Shah & Karan Singh 37
➢ Add a new source 𝑡, edges from 𝑡 to each 𝑡𝑗 with ∞ capacity ➢ Add a new sink 𝑢, edges from each 𝑢𝑘 to 𝑢 with ∞ capacity ➢ Find max-flow from 𝑡 to 𝑢 ➢ Claim: 1 − 1 correspondence between flows in two networks
373F19 - Nisarg Shah & Karan Singh 38
➢ Directed graph 𝐻 = (𝑊, 𝐹) ➢ Edge capacities 𝑑 ∶ 𝐹 → ℕ ➢ Node demands 𝑒 ∶ 𝑊 → ℤ
➢ Some circulation 𝑔 ∶ 𝐹 → ℕ satisfying
➢ Note that you need σ𝑤:𝑒 𝑤 >0 𝑒(𝑤) = σ𝑤:𝑒 𝑤 <0 −𝑒(𝑤) ➢ What are demands?
373F19 - Nisarg Shah & Karan Singh 39
➢ 𝑒 𝑤 > 0 : You need to take some flow out at 𝑤
➢ 𝑒 𝑤 < 0 : You need to put some flow in at 𝑤
more outgoing flow than incoming flow
➢ 𝑒 𝑤 = 0 : Node has flow conservation
373F19 - Nisarg Shah & Karan Singh 40
373F19 - Nisarg Shah & Karan Singh 41
➢ Add a new source 𝑡 and a new sink 𝑢 ➢ For each “supply” node 𝑤 with 𝑒 𝑤 < 0, add edge (𝑡, 𝑤)
➢ For each “demand” node 𝑤 with 𝑒 𝑤 > 0, add edge
373F19 - Nisarg Shah & Karan Singh 42
373F19 - Nisarg Shah & Karan Singh 43
373F19 - Nisarg Shah & Karan Singh 44
➢ Directed graph 𝐻 = (𝑊, 𝐹) ➢ Edge capacities 𝑑 ∶ 𝐹 → ℕ and lower bounds ℓ ∶ 𝐹 → ℕ ➢ Node demands 𝑒 ∶ 𝑊 → ℤ
➢ Some circulation 𝑔 ∶ 𝐹 → ℕ satisfying
➢ Note that you still need σ𝑤:𝑒 𝑤 >0 𝑒(𝑤) = σ𝑤:𝑒 𝑤 <0 −𝑒(𝑤)
373F19 - Nisarg Shah & Karan Singh 45
➢ Do the following operation to each edge
➢ Proof sketch: 𝑔(𝑓) gives a valid circulation in 𝐻 iff
373F19 - Nisarg Shah & Karan Singh 46
➢ We want to design a survey about 𝑛 products
➢ There are 𝑜 consumers ➢ Consumer 𝑗 owns a subset of products 𝑃𝑗
➢ We want to ask each consumer 𝑗 between 𝑑𝑗 and 𝑑𝑗
′
➢ We want to ask between 𝑞𝑘 and 𝑞𝑘
′ question about each
➢ Is there a survey meeting all these requirements?
373F19 - Nisarg Shah & Karan Singh 47
➢ 𝑑𝑗 = 𝑑𝑗
′ = 𝑞𝑘 = 𝑞𝑘 ′ = 1 for all 𝑗 and 𝑘
➢ Use circulation with lower bounds model ➢ Create a network with special nodes 𝑡 and 𝑢 ➢ Edge from 𝑡 to node of consumer 𝑗 with flow ∈ [𝑑𝑗, 𝑑𝑗
′]
➢ Edge from consumer 𝑗 to product 𝑘 ∈ 𝑃𝑗 with flow ∈ [0,1] ➢ Edge from node of product 𝑘 to sink 𝑢 with flow ∈ [𝑞𝑘, 𝑞𝑘
′]
➢ Edge from 𝑢 to 𝑡 with flow in [0, ∞] ➢ All demands and supplies are 0
373F19 - Nisarg Shah & Karan Singh 48
➢ Feasible survey iff feasible circulation in this network
373F19 - Nisarg Shah & Karan Singh 49
➢ Given an image, separate “foreground” from “background”
Remove background
373F19 - Nisarg Shah & Karan Singh 50
➢ Given an image, separate “foreground” from “background”
Remove background
373F19 - Nisarg Shah & Karan Singh 51
➢ Given an image (2D array of pixels), and likelihood
➢ Want to prevent having too many
373F19 - Nisarg Shah & Karan Singh 52
➢ An image (2D array of pixels) ➢ 𝑏𝑗 = likelihood of pixels 𝑗 being in foreground ➢ 𝑐𝑗 = likelihood of pixels 𝑗 being in background ➢ 𝑞𝑗,𝑘 = penalty for separating pixels 𝑗 and 𝑘 (i.e. labeling
➢ Label each pixel as “foreground” or “background” ➢ Minimize total penalty
𝑐𝑗 is high but 𝑗 is labeled foreground, or 𝑞𝑗,𝑘 is high but 𝑗 and 𝑘 are separated
373F19 - Nisarg Shah & Karan Singh 53
➢ 𝑏𝑗 = likelihood of pixels 𝑗 being in foreground ➢ 𝑐𝑗 = likelihood of pixels 𝑗 being in background ➢ 𝑞𝑗,𝑘 = penalty for separating pixels 𝑗 and 𝑘 ➢ Let 𝐹 = pairs of neighboring pixels
➢ Minimize total penalty
𝑗∈𝐵
𝑐𝑗 +
𝑘∈𝐶
𝑏𝑘 +
𝑗,𝑘 ∈𝐹 𝐵∩ 𝑗,𝑘 =1
𝑞𝑗,𝑘
373F19 - Nisarg Shah & Karan Singh 54
➢ Want to divide the set of pixels 𝑊 into (𝐵, 𝐶) to minimize
𝑗∈𝐵
𝑐𝑗 +
𝑘∈𝐶
𝑏𝑘 +
𝑗,𝑘 ∈𝐹 𝐵∩ 𝑗,𝑘 =1
𝑞𝑗,𝑘
➢ Add a node 𝑤𝑗 for each pixel 𝑗 ➢ Add a source node 𝑡, sink node 𝑢 ➢ Add 𝑡 → 𝑤𝑗 edge with capacity 𝑏𝑗 and 𝑤𝑗 → 𝑢 edge with
➢ For neighboring (𝑗, 𝑘), add both 𝑤𝑗 → 𝑤𝑘 and 𝑤𝑘 → 𝑤𝑗
373F19 - Nisarg Shah & Karan Singh 55
➢ Here’s what the network looks like
373F19 - Nisarg Shah & Karan Singh 56
➢ Consider the min-cut (𝐵, 𝐶)
𝑑𝑏𝑞 𝐵, 𝐶 =
𝑗∈𝐵
𝑐𝑗 +
𝑘∈𝐶
𝑏𝑘 +
𝑗,𝑘 ∈𝐹 𝑗∈𝐵,𝑘∈𝐶
𝑞𝑗,𝑘
➢ Exactly what we want to minimize!
If 𝑗 and 𝑘 are labeled differently, it will add 𝑞𝑗,𝑘 exactly once
373F19 - Nisarg Shah & Karan Singh 57