cse 421 - courses.cs.washington.edu...•orient all edges from x to y, andassign infinite (or unit)...
TRANSCRIPT
CSE 421
Network Flows, Matching
Shayan Oveis Gharan
1
Network Flows
Residual Graph
Original edge: e = (u, v) Î E.• Flow f(e), capacity c(e).
Residual edge.• "Undo" flow sent.• e = (u, v) and eR = (v, u).• Residual capacity:
𝑐! 𝑒 = $𝑐 𝑒 − 𝑓 𝑒 𝑖𝑓 𝑒 ∈ 𝐸𝑓 𝑒 𝑖𝑓 𝑒" ∈ 𝐸
Residual graph: Gf = (V, Ef ).• Residual edges with positive residual capacity.• 𝐸! = 𝑒 ∶ 𝑓 𝑒 < 𝑐 𝑒 ∪ {𝑒 ∶ 𝑓(𝑒") > 0}.
3
u v17
6
capacity
u v11
residual capacity
6residual capacity
flow
Ford-Fulkerson Alg: Greedy on Gf
4
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity0
0
0 0
00
00
0
s
2
3
4
5 t10 9
8
10
62Gf:
10
4
10
Find Path
Ford-Fulkerson Alg: Greedy on Gf
5
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity4
4
0 0
00
04
0
s
2
3
4
5 t10 9
8
10
62Gf:
10
4
10
Update Flow
Ford-Fulkerson Alg: Greedy on Gf
6
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity4
4
0 0
00
04
0
s
2
3
4
5 t10 9
8
10
62Gf:
6 6
Update Residual
4
4
4
Ford-Fulkerson Alg: Greedy on Gf
7
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity4
4
0 0
00
04
0
s
2
3
4
5 t10
8 6Gf:
6
Find Path
4
4
4
9 10
6 2
Ford-Fulkerson Alg: Greedy on Gf
8
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity6
4
0 2
20
04
2
s
2
3
4
5 t10
8 6Gf:
64
4
4
9 10
6 2
Update Flow
Ford-Fulkerson Alg: Greedy on Gf
9
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity6
4
0 2
20
04
2
s
2
3
4
5 t10
8 6Gf:
66
4
4
7 8
4 2
Update Residual
2 2
Ford-Fulkerson Alg: Greedy on Gf
10
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity6
4
0 2
20
04
2
s
2
3
4
5 t10
6Gf:
66
4
4
7
2
2 2
8
8
4
Find Path
Ford-Fulkerson Alg: Greedy on Gf
11
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
0 2
24
04
6
s
2
3
4
5 t10
6Gf:
66
4
4
7
2
2 2
8
8
4
Update Flow
Ford-Fulkerson Alg: Greedy on Gf
12
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
0 2
24
04
6
s
2
3
4
5 t10
6Gf:
610
4
4
7
2
2 6
4
4
Update Residual
4
Ford-Fulkerson Alg: Greedy on Gf
13
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
0 2
24
04
6
s
2
3
4
5 t
Gf: 10
4
4
72 6
4
Find Path
4
6642
10
Ford-Fulkerson Alg: Greedy on Gf
14
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
2 2
06
26
6
s
2
3
4
5 t
Gf: 10
4
4
72 6
4
Update Flow
4
6642
10
Ford-Fulkerson Alg: Greedy on Gf
15
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
2 2
06
26
6
s
2
3
4
5 t
Gf: 10
4
6
72 6
4
Update Residual
6
422
8
24
2
Ford-Fulkerson Alg: Greedy on Gf
16
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
2 2
06
26
6
s
2
3
4
5 t
Gf: 10
4
6
2 6
Find Path
6
422 24
2
8 7 4
Ford-Fulkerson Alg: Greedy on Gf
17
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
6 6
06
26
10
s
2
3
4
5 t
Gf: 10
4
6
2 6
Update Flow
6
422 24
2
8 7 4
Ford-Fulkerson Alg: Greedy on Gf
18
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
6 6
06
26
10
s
2
3
4
5 t
Gf: 10
4
6
6 10
Update Residual
6
422 24
6
4 3
Ford-Fulkerson Alg: Greedy on Gf
19
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
6 6
06
26
10
s
2
3
4
5 t
Gf: 10
4
6
6 10
Find Path
6
222
6
44
34
Ford-Fulkerson Alg: Greedy on Gf
20
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
9 9
06
59
10
s
2
3
4
5 t
Gf: 10
4
6
6 10
Update FLow
6
222
6
44
34
Ford-Fulkerson Alg: Greedy on Gf
21
s
2
3
4
5 t10
10
9
8
4
10
1062G:
capacity10
4
9 9
06
59
10
s
2
3
4
5 t
Gf: 10
4
9
9 10
Find Path
6
225
9
11
1
Augmenting Path Algorithm
22
Augment(f, c, P) {b ¬ bottleneck(P) foreach e Î P {
if (e Î E) f(e) ¬ f(e) + bc(e) ¬ c(e) – bc(eR) ¬ c(eR) + b
else f(eR) ¬ f(eR) – bc(eR) ¬ c(eR) + bc(e) ¬ c(e) - b
}return f
}
Ford-Fulkerson(G, s, t, c) {foreach e Î E f(e) ¬ 0. Gf is residual graphwhile (there exists augmenting path P) {
f ¬ Augment(f, c, P)}
return f}
Smallest capacity edge on P
Forward edge
Reverse edge
Max Flow Min Cut Theorem
Augmenting path theorem. Flow f is a max flow iff there are no augmenting paths. Max-flow min-cut theorem. [Ford-Fulkerson 1956] The value of the max s-t flow is equal to the value of the min s-t cut.Proof strategy. We prove both simultaneously by showing the TFAE:
(i) There exists a cut (A, B) such that v(f) = cap(A, B).(ii) Flow f is a max flow.(iii) There is no augmenting path relative to f.
(i) Þ (ii) This was the corollary to weak duality lemma.
(ii) Þ (iii) We show contrapositive.Let f be a flow. If there exists an augmenting path, then we can improve f by sending flow along that path.
23
Pf of Max Flow Min Cut Theorem
(iii) => (i)No augmenting path for f => there is a cut (A,B): v(f)=cap(A,B)
• Let f be a flow with no augmenting paths.• Let A be set of vertices reachable from s in residual graph.• By definition of A, s Î A.• By definition of f, t Ï A.
24
𝑣 𝑓 = 5# $%& $' (
𝑓 𝑒 − 5# )* &$ (
𝑓(𝑒)
= 5# $%& $' (
𝑐 𝑒
= 𝑐𝑎𝑝(𝐴, 𝐵)
Running Time
Assumption. All capacities are integers between 1 and C.
Invariant. Every flow value 𝑓(𝑒) and every residual capacities 𝑐𝑓 (𝑒) remains an integer throughout the algorithm.
Theorem. The algorithm terminates in at most 𝑣(𝑓∗) £ 𝑛𝐶 iterations, if 𝑓∗ is optimal flow.Pf. Each augmentation increase value by at least 1.
Corollary. If C = 1, Ford-Fulkerson runs in O(mn) time.
Integrality theorem. If all capacities are integers, then there exists a max flow f for which every flow value f(e) is an integer.Pf. Since algorithm terminates, theorem follows from invariant.
25
Applications of Max Flow:Bipartite Matching
Maximum Matching Problem
Given an undirected graph G = (V, E).A set 𝑀 ⊆ 𝐸 is a matching if each node appears in at most one edge in M.Goal: find a matching with largest cardinality.
27
Bipartite Matching Problem
Given an undirected bibpartite graph 𝐺 = (𝑋 ∪ 𝑌, 𝐸)A set 𝑀 ⊆ 𝐸 is a matching if each node appears in at most one edge in M.Goal: find a matching with largest cardinality.
28
1
3
5
1'
3'
5'
2
4
2'
4'X Y
Bipartite Matching using Max Flow
Create digraph H as follows:• Orient all edges from X to Y, and assign infinite (or unit) capacity.• Add source s, and unit capacity edges from s to each node in L.• Add sink t, and unit capacity edges from each node in R to t.
29
s
1
3
5
1'
3'
5'
t
2
4
2'
4'
1 1
∞
YX
H
Bipartite Matching: Proof of Correctness
Thm. Max cardinality matching in G = value of max flow in H.Pf. £Given max matching M of cardinality k.Consider flow f that sends 1 unit along each of k edges of M.f is a flow, and has cardinality k. ▪
30
1
3
5
1'
3'
5'
2
4
2'
4'
G
4 4'
s
1
3
5
1'
3'
5'
t
2 2'1 1
∞ H
Bipartite Matching: Proof of Correctness
Thm. Max cardinality matching in G = value of max flow in H.Pf. (of ≥) Let f be a max flow in H of value k.Integrality theorem Þ k is integral and we can assume f is 0-1.Consider M = set of edges from X to Y with f(e) = 1.• each node in X and Y participates in at most one edge in M• |M| = k: consider s-t cut (𝑠 ∪ 𝑋, 𝑡 ∪ 𝑌)
31
s
1
3
5
1'
3'
5'
t
2
4
2'
4'
1 1
∞ H 1
3
5
1'
3'
5'
2
4
2'
4'
G
Perfect Bipartite Matching
Perfect Bipartite Matching
Def. A matching M Í E is perfect if each node appears in exactly one edge in M.
Q. When does a bipartite graph have a perfect matching?
Structure of bipartite graphs with perfect matchings:• Clearly we must have |X| = |Y|.• What other conditions are necessary?• What conditions are sufficient?
33
Perfect Bipartite Matching: N(S)
Def. Let S be a subset of nodes, and let N(S) be the set of nodes adjacent to nodes in S.
Observation. If a bipartite graph G has a perfect matching, then |N(S)| ³ |S| for all subsets S ⊆ 𝑋.Pf. Each 𝑣 ∈ 𝑆 has to be matched to a unique node in N(S).
34
S
N(S)
SN(S)
Marriage Theorem
Thm: [Frobenius 1917, Hall 1935] Let 𝐺 = (𝑋 ∪ 𝑌, 𝐸) be a bipartite graph with |X| = |Y|. Then, G has a perfect matching iff 𝑁 𝑆 ≥ 𝑆 for all subsets 𝑆 ⊆ 𝑋.
Pf. ÞThis was the previous observation.If |N(S)| < |S| for some S, then there is no perfect matching.
35
Marriage Theorem
Pf. ∃𝑆 ⊆ 𝑋 s.t., |𝑁 𝑆 | < |𝑆| ⇐ G does not a perfect matchingFormulate as a max-flow and let (𝐴, 𝐵) be the min s-t cutG has no perfect matching => 𝑣 𝑓∗ < |𝑋|. So, 𝑐𝑎𝑝 𝐴, 𝐵 < |𝑋|Define 𝑋" = 𝑋 ∩ 𝐴, 𝑋# = 𝑋 ∩ 𝐵, 𝑌" = 𝑌 ∩ 𝐴Then, 𝑐𝑎𝑝 𝐴, 𝐵 = 𝑋# + |𝑌"|Since min-cut does not use ∞ edges, 𝑁 𝑋" ⊆ 𝑌"𝑁 𝑋" ≤ 𝑌" = 𝑐𝑎𝑝 𝐴, 𝐵 − 𝑋# = 𝑐𝑎𝑝 𝐴, 𝐵 − 𝑋 + 𝑋" < |𝑋"|
36
s t
𝑋(
𝑋, 𝑌,
𝑌(
∞
Bipartite Matching Running Time
Which max flow algorithm to use for bipartite matching?Generic augmenting path: O(m val(f*) ) = O(mn).Capacity scaling: O(m2 log C ) = O(m2).Shortest augmenting path: O(m n1/2).
Non-bipartite matching.Structure of non-bipartite graphs is more complicated, but
well-understood. [Tutte-Berge, Edmonds-Galai]Blossom algorithm: O(n4). [Edmonds 1965]Best known: O(m n1/2). [Micali-Vazirani 1980]
37
Edge Disjoint Paths
Edge Disjoint Paths Problem
Given a digraph G = (V, E) and two nodes s and t, find the max number of edge-disjoint s-t paths.
Def. Two paths are edge-disjoint if they have no edge in common.
Ex: communication networks.
39
s
2
3
4
5
6
7
t
Max Flow Formulation
Assign a unit capacitary to every edge. Find Max flow from s to t.
Thm. Max number edge-disjoint s-t paths equals max flow value.Pf. £Suppose there are k edge-disjoint paths 𝑃$, … , 𝑃%.Set f(e) = 1 if e participates in some path 𝑃& ; else set f(e) = 0.Since paths are edge-disjoint, f is a flow of value k. ▪
40
s t
1
1
1
1
1
1
11
1
1
1
1
1
1
Max Flow Formulation
Thm. Max number edge-disjoint s-t paths equals max flow value.Pf. ≥ Suppose max flow value is kIntegrality theorem Þ there exists 0-1 flow f of value k.Consider edge (s, u) with f(s, u) = 1.• by conservation, there exists an edge (u, v) with f(u, v) = 1• continue until reach t, always choosing a new edgeThis produces k (not necessarily simple) edge-disjoint paths. ▪
41
s t
1
1
1
1
1
1
11
1
1
1
1
1
1
We can return to u so we can have cycles. But we can eliminate cycles if desired