CSC373 Week 5: Network Flow (contd)
373F20 - Nisarg Shah 1
Nisarg Shah 373F20 - Nisarg Shah 1 Recap Some more DP Traveling - - PowerPoint PPT Presentation
CSC373 Week 5: Network Flow (contd) Nisarg Shah 373F20 - Nisarg Shah 1 Recap Some more DP Traveling salesman problem (TSP) Start of network flow Problem statement Ford-Fulkerson algorithm Running time Correctness
373F20 - Nisarg Shah 1
373F20 - Nisarg Shah 2
➢ Traveling salesman problem (TSP)
➢ Problem statement ➢ Ford-Fulkerson algorithm ➢ Running time ➢ Correctness using max-flow, min-cut
373F20 - Nisarg Shah 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
373F20 - Nisarg Shah 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 𝑔(𝑓)
373F20 - Nisarg Shah 5
s t u v
20/20 20/30 20/20 0/10 0/10
s t u v
𝟑𝟏 𝟐𝟏 𝟑𝟏 𝟐𝟏 𝟐𝟏 𝟑𝟏
Flow 𝑔 Residual graph 𝐻𝑔
373F20 - Nisarg Shah 6
373F20 - Nisarg Shah 7
➢ #Augmentations:
➢ Time for an augmentation:
➢ Total time: 𝑃( 𝑛 + 𝑜 ⋅ 𝐷)
373F20 - Nisarg Shah 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
373F20 - Nisarg Shah 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.
373F20 - Nisarg Shah 10
➢ Level: ℓ(𝑤) = length of shortest 𝑡 → 𝑤 path ➢ Level graph 𝑀𝐻 = (𝑊, 𝐹𝑀) is a subgraph of 𝐻 where we
373F20 - Nisarg Shah 11
➢ Level: ℓ(𝑤) = length of shortest 𝑡 → 𝑤 path ➢ Level graph 𝑀𝐻 = (𝑊, 𝐹𝑀) is a subgraph of 𝐻 where we
373F20 - Nisarg Shah 12
➢ Length of the shortest 𝑡 → 𝑢 path in 𝐻𝑔 never decreases.
➢ Let 𝑔 and 𝑔′ be flows before and after an augmentation
Residual graph 𝐻𝑔 Residual graph 𝐻𝑔′
373F20 - Nisarg Shah 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!
NOT IN SYLLABUS
373F20 - Nisarg Shah 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! ∎
NOT IN SYLLABUS
373F20 - Nisarg Shah 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.
373F20 - Nisarg Shah 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 𝑜)
373F20 - Nisarg Shah 17
373F20 - Nisarg Shah 18
Rail network connecting Soviet Union with Eastern European countries
(Tolstoǐ 1930s)
373F20 - Nisarg Shah 19
Rail network connecting Soviet Union with Eastern European countries
(Tolstoǐ 1930s) Min-cut
373F20 - Nisarg Shah 20
➢ If edge capacities are integers, then the max-flow
➢ Easy to check that each augmentation step preserves
373F20 - Nisarg Shah 21
➢ Given a bipartite graph 𝐻 = (𝑉 ∪ 𝑊, 𝐹), find a maximum
373F20 - Nisarg Shah 22
➢ Add a source node 𝑡 and target node 𝑢 ➢ Add edges, all of capacity 1:
𝑉 𝑊 𝑉 𝑊
373F20 - Nisarg Shah 23
➢ There is a 1-1 correspondence between matchings of size
➢ Take a matching 𝑁 =
➢ Construct the corresponding unique flow 𝑔
𝑁 where…
➢ This flow has value 𝑙
373F20 - Nisarg Shah 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 𝑉
373F20 - Nisarg Shah 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
373F20 - Nisarg Shah 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
373F20 - Nisarg Shah 27
➢ All 𝑉 → 𝑊 edges now have ∞ capacity ➢ 𝑡 → 𝑉 and 𝑊 → 𝑢 edges are still unit capacity
𝑉 𝑊 𝑉 𝑊 ∞ ∞ 1 1 1 1
373F20 - Nisarg Shah 28
➢ 𝐻 has a perfect matching iff 𝑂 𝑇
➢ Suppose 𝐻 doesn’t have a perfect matching ➢ Hence, max-flow = min-cut < 𝑜 ➢ Let (𝐵, 𝐶) be the min-cut
373F20 - Nisarg Shah 29
➢ 𝐻 has a perfect matching iff 𝑂 𝑇
➢ 𝑑𝑏𝑞 𝐵, 𝐶 = 𝑉 ∩ 𝐶 + 𝑊 ∩ 𝐵 < 𝑜 = 𝑉 ➢ So 𝑊 ∩ 𝐵 < |𝑉 ∩ 𝐵| ➢ But 𝑂 𝑉 ∩ 𝐵 ⊆ 𝑊 ∩ 𝐵 because the cut doesn’t include any
➢ So 𝑂 𝑉 ∩ 𝐵
373F20 - Nisarg Shah 30
➢ 1955: 𝑃(𝑛𝑜) → 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
373F20 - Nisarg Shah 31
➢ Given a directed graph 𝐻 = (𝑊, 𝐹), two nodes 𝑡 and 𝑢,
➢ Two 𝑡 → 𝑢 paths 𝑄 and 𝑄′ are edge-disjoint if they don’t
373F20 - Nisarg Shah 32
➢ Communication networks
➢ Assign unit capacity on all edges
373F20 - Nisarg Shah 33
➢ There is 1-1 correspondence between sets of 𝑙 edge-
➢ Let 𝑄
1, … , 𝑄𝑙 be a set of 𝑙 edge-disjoint 𝑡 → 𝑢 paths
➢ Define flow 𝑔 where 𝑔 𝑓 = 1 whenever 𝑓 ∈ 𝑄𝑗 for some
➢ Since paths are edge-disjoint, flow conservation and
➢ Unique integral flow of value 𝑙
373F20 - Nisarg Shah 34
➢ 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 𝑡
373F20 - Nisarg Shah 35
➢ Equals max flow in this network ➢ By max-flow min-cut theorem, also equals minimum cut ➢ Exercise: minimum cut = minimum number of edges we
373F20 - Nisarg Shah 36
➢ Show that to compute the maximum number of edge-
➢ In any directed/undirected graph, the maximum number
373F20 - Nisarg Shah 37
➢ Given a directed graph 𝐻 = (𝑊, 𝐹) with edge capacities
373F20 - Nisarg Shah 38
➢ 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
373F20 - Nisarg Shah 39
➢ Directed graph 𝐻 = (𝑊, 𝐹) ➢ Edge capacities 𝑑 ∶ 𝐹 → ℕ ➢ Node demands 𝑒 ∶ 𝑊 → ℤ
➢ Some circulation 𝑔 ∶ 𝐹 → ℕ satisfying
➢ Note that you need σ𝑤:𝑒 𝑤 >0 𝑒(𝑤) = σ𝑤:𝑒 𝑤 <0 −𝑒(𝑤) ➢ What are demands?
373F20 - Nisarg Shah 40
➢ 𝑒 𝑤 > 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
373F20 - Nisarg Shah 41
373F20 - Nisarg Shah 42
➢ Add a new source 𝑡 and a new sink 𝑢 ➢ For each “supply” node 𝑤 with 𝑒 𝑤 < 0, add edge (𝑡, 𝑤)
➢ For each “demand” node 𝑤 with 𝑒 𝑤 > 0, add edge
373F20 - Nisarg Shah 43
373F20 - Nisarg Shah 44
373F20 - Nisarg Shah 45
➢ Directed graph 𝐻 = (𝑊, 𝐹) ➢ Edge capacities 𝑑 ∶ 𝐹 → ℕ and lower bounds ℓ ∶ 𝐹 → ℕ ➢ Node demands 𝑒 ∶ 𝑊 → ℤ
➢ Some circulation 𝑔 ∶ 𝐹 → ℕ satisfying
➢ Note that you still need σ𝑤:𝑒 𝑤 >0 𝑒(𝑤) = σ𝑤:𝑒 𝑤 <0 −𝑒(𝑤)
373F20 - Nisarg Shah 46
➢ Do the following operation to each edge
➢ Proof sketch: 𝑔(𝑓) gives a valid circulation in 𝐻 iff
373F20 - Nisarg Shah 47
➢ We want to design a survey about 𝑛 products
′ consumers
➢ There are a total of 𝑜 consumers
′ questions
➢ Is there a survey meeting all these requirements?
373F20 - Nisarg Shah 48
➢ 𝑑𝑗 = 𝑑𝑗
′ = 𝑞𝑘 = 𝑞𝑘 ′ = 1 for all 𝑗 and 𝑘
➢ Create a network with special nodes 𝑡 and 𝑢 ➢ Edge from 𝑡 to each consumer 𝑗 with flow ∈ [𝑑𝑗, 𝑑𝑗
′]
➢ Edge from each consumer 𝑗 to each product 𝑘 ∈ 𝑃𝑗 with
➢ Edge from each product 𝑘 to 𝑢 with flow ∈ [𝑞𝑘, 𝑞𝑘
′]
➢ Edge from 𝑢 to 𝑡 with flow in [0, ∞] ➢ All demands and supplies are 0
373F20 - Nisarg Shah 49
➢ Feasible survey iff feasible circulation in this network
373F20 - Nisarg Shah 50
➢ Given an image, separate “foreground” from “background”
Remove background
373F20 - Nisarg Shah 51
➢ Given an image, separate “foreground” from “background”
Remove background
373F20 - Nisarg Shah 52
➢ Given an image (2D array of pixels), and likelihood
➢ Want to prevent having too many
373F20 - Nisarg Shah 53
➢ An image (2D array of pixels) ➢ 𝑏𝑗 = likelihood of pixel 𝑗 being in foreground ➢ 𝑐𝑗 = likelihood of pixel 𝑗 being in background ➢ 𝑞𝑗,𝑘 = penalty for “separating” pixels 𝑗 and 𝑘 (i.e. labeling
➢ Label each pixel as “foreground” or “background” ➢ Minimize “total penalty”
but 𝑗 is labeled foreground, or 𝑞𝑗,𝑘 is high but 𝑗 and 𝑘 are separated
373F20 - Nisarg Shah 54
➢ 𝑏𝑗 = 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
𝑞𝑗,𝑘
373F20 - Nisarg Shah 55
➢ Want to divide the set of pixels 𝑊 into (𝐵, 𝐶) to minimize
𝑗∈𝐵
𝑐𝑗 +
𝑘∈𝐶
𝑏𝑘 +
𝑗,𝑘 ∈𝐹 𝐵∩ 𝑗,𝑘 =1
𝑞𝑗,𝑘
➢ Nodes:
➢ Edges:
373F20 - Nisarg Shah 56
➢ Here’s what the network looks like
373F20 - Nisarg Shah 57
➢ Consider the min-cut (𝐵, 𝐶)
𝑑𝑏𝑞 𝐵, 𝐶 =
𝑗∈𝐵
𝑐𝑗 +
𝑘∈𝐶
𝑏𝑘 +
𝑗,𝑘 ∈𝐹 𝑗∈𝐵,𝑘∈𝐶
𝑞𝑗,𝑘
➢ Exactly what we want to minimize!
If 𝑗 and 𝑘 are labeled differently, it will add 𝑞𝑗,𝑘 exactly once
373F20 - Nisarg Shah 58
373F20 - Nisarg Shah 59
➢ There are 𝑜 tasks ➢ Performing task 𝑗 generates a profit of 𝑞𝑗
➢ There is a set 𝐹 of precedence relations
➢ Find a subset of tasks 𝑇 which, subject to the precedence
373F20 - Nisarg Shah 60
➢ Nodes = tasks, node weights = profits, ➢ Edges = precedence constraints ➢ Goal: find a subset of nodes 𝑇 with highest total weight
3
7 3
373F20 - Nisarg Shah 61
➢ Add source 𝑡 and target 𝑢 ➢ min-cut (𝐵, 𝐶) ⇒ want desired solution to be 𝑇 = 𝐵 ∖ {𝑡} ➢ Goals:
➢ Add each 𝑗, 𝑘 ∈ 𝐹 with infinite capacity ➢ For each 𝑗:
373F20 - Nisarg Shah 62
3
7 3
373F20 - Nisarg Shah 63
3
7 3 s t 3 7 3 3 1 2 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
373F20 - Nisarg Shah 64
A possible cut
3
7 3 s t 3 7 3 3 1 2 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
373F20 - Nisarg Shah 65
, where the constant is independent of the choice of (𝐵, 𝐶)