cse 202: design and analysis of algorithms a bipartite graph g = (l, r), find a matching in g of...
TRANSCRIPT
Announcements
• Midterm Wednesday May 4
• Midterm is closed book, closed notes
• Syllabus for midterm: Greedy, Dynamic Programming,
Divide and Conquer, and Flows (upto Ford-Fulkerson)
• HW2 is graded!
Last Class: Algorithms for Max-Flow
• Ford-Fulkerson: Running Time = O(m Fmax)
• Other efficient Ford-Fulkerson Style Algorithms:
• Edmonds-Karp: Running Time = O(nm2)
• Capacity Scaling: Running Time = O(m2 log Cmax)
• Preflow-Push: Running Time = O(mn2)
n = #vertices, m = #edges in G, Cmax = max capacity, Fmax = max flowInteger edge capacities
Given a bipartite graph G = (L, R), find a matching in G of maximum cardinality
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
Perfect matching: a matching of size n,
where n=#vertices in L=#vertices in R
Application: Maximum Cardinality Bipartite Matching
A matching M is a set of edges (u, v) such that no two edges share a common vertex
Can be solved using our maxflow algorithm (we will see how)
“Bipartite matching reduces to maximum flow”
i.e., if we have an efficient algorithm for maximum flow then we can use it to solve bipartite matching efficiently.
Given a bipartite graph G = (L, R), find a matching in G of maximum cardinality
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
Perfect matching: a matching of size n,
where n=#vertices in L=#vertices in R
s t
1
1
1
1
1
1 1
1
1
1
11
1
Application: Maximum Cardinality Bipartite Matching
Set up a flow problem in graph H:
Source s: connected to all nodes in LSink t: connected to all nodes in REach edge has unit capacity
Recall: A matching M is a set of edges (u, v) such that no two edges share a common vertex
Size of max flow in H = cardinality of maximum matching in G
L R
L R
Graph H:Graph G:
Reduction to Max-Flow:
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
Bipartite Matching
Property: Cardinality of max matching in G = Size of max flow in H
1
L R L R
Graph H:Graph G:
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
Bipartite Matching
Property: Cardinality of max matching in G = Size of max flow in H
1
1. Suppose G has a matching M. Then, consider the flow f in H:
f(e) = 1, e in M, f(e) = 1, e = (s, u), u in M, f(e) = 1, e = (v, t), v in M f(e) = 0, owThus, size(f) = cardinality(M)
L R L R
Graph H:Graph G:
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
Bipartite Matching
Property: Cardinality of max matching in G = Size of max flow in H
1
2. Suppose H has a max flow of size s. Then H has an integral max flow f of size s.
Let Y = (u, v) be the edges s.t. u in L, v in R, and f(e) = 1. Then, no u or v can be adjacent to >1 nodes in Y (otherwise, capacity constraints violated). Thus Y is a matching Cardinality(Y) = Flow through cut (L,R) = size(f)
L R L R
Graph H:Graph G:
1. Suppose G has a matching M. Then, consider the flow f in H:
f(e) = 1, e in M, f(e) = 1, e = (s, u), u in M, f(e) = 1, e = (v, t), v in M f(e) = 0, owThus, size(f) = cardinality(M)
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
Bipartite Matching
Property: Cardinality of max matching in G = Size of max flow in H
1
L R
Graph H:
L R
Graph G:
Algorithm:
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
Bipartite Matching
Property: Cardinality of max matching in G = Size of max flow in H
1
L R
Graph H:
L R
Graph G:
Algorithm: To find a maximum cardinality matching, construct H, and use Ford-Fulkerson
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
Bipartite Matching
Property: Cardinality of max matching in G = Size of max flow in H
1
L R
Graph H:
L R
Graph G:
Algorithm: To find a maximum cardinality matching, construct H, and use Ford-Fulkerson
Running Time: O(mn)
- Each FF iteration increases flow value by at least 1- Max flow size at most n
Bipartite Matching
Properties of flow graph H:
- Each edge has capacity 1- Each node (except s and t) has either indegree 1 or outdegree 1
Such graphs are called unit graphsNow: a faster algorithm for finding max flows in unit graphs
Arthur
Bill
Charles
David
Angela
Beth
Connie
Doris
s t
1
1
1
1
1
1 1
1
1
1
11
1
1
L R L R
Graph H:Graph G:
Example:
Blocking Flows
A Blocking Flow of G saturates at least one edge in all s-t paths in G
A Max Flow is always a Blocking Flow (Why?)A Blocking Flow may not be a max flow
s
a
b
t
1
1
1
1
1 s
a
b
t
1
1
1
G: f:
Example:
Blocking Flows
A Blocking Flow of G saturates at least one edge in all s-t paths in G
A Max Flow is always a Blocking Flow (Why?)A Blocking Flow may not be a max flow
s
a
b
t
1
1
1
1
1 s
a
b
t
1
1
1
G: f:
Admissible edge: An edge e in a residual graph Gf is admissible if it lies on some s-t shortest path.
Blocking Flows Algorithm (Dinic 70)
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
An edge e in a residual graph Gf is admissible if it lies on a s-t shortest pathA Blocking Flow in G saturates at least one edge in all s-t paths in G
Blocking Flows Algorithm (Dinic 70)
s
a
b
t
106
106
106
106
1
Bad Example for FF:
An edge e in a residual graph Gf is admissible if it lies on a s-t shortest pathA Blocking Flow in G saturates at least one edge in all s-t paths in G
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows Algorithm (Dinic 70)
s
a
b
t
106
106
106
106
1
Bad Example for FF:
Iteration 1
s
a
b
t
106
106
106
106
Hf (before)
An edge e in a residual graph Gf is admissible if it lies on a s-t shortest pathA Blocking Flow in G saturates at least one edge in all s-t paths in G
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows Algorithm (Dinic 70)
s
a
b
t
106
106
106
106
1
Bad Example for FF:
Iteration 1
s
a
b
t
106 106
106106s
a
b
t
106
106
106
106
Hf (before) f
An edge e in a residual graph Gf is admissible if it lies on a s-t shortest pathA Blocking Flow in G saturates at least one edge in all s-t paths in G
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows Algorithm (Dinic 70)
s
a
b
t
106
106
106
106
1
Bad Example for FF:
s
a
b
t
106
106
106
106
1
Iteration 1
s
a
b
t
106 106
106106s
a
b
t
106
106
106
106
Hf (before) f Gf (after)
An edge e in a residual graph Gf is admissible if it lies on a s-t shortest pathA Blocking Flow in G saturates at least one edge in all s-t paths in G
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows Algorithm (Dinic 70)
Why is this algorithm correct?
s
a
b
t
106
106
106
106
1
Iteration 1
s
a
b
t
106 106
106106s
a
b
t
106
106
106
106
Hf (before) f Gf (after)
s
a
b
t
106
106
106
106
1
Bad Example for FF:
An edge e in a residual graph Gf is admissible if it lies on a s-t shortest pathA Blocking Flow in G saturates at least one edge in all s-t paths in G
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
O(m√n)
Proof Outline:1. There can be at most iterations of the blocking flow algorithm in an unit graph2. Each iteration can be implemented in O(m) time in an unit graph
O(√n)
Blocking Flows Algorithm (Dinic 70)
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
Running Time of Blocking Flow Algorithm in unit graphs =
Recall, in an unit graph:- Each edge has capacity 1- Each node (except s and t) has either indegree 1 or outdegree 1
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
Before we prove this property, let us look at some properties of shortest paths and Hf
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
Properties of Shortest Paths:
Let d(u) = shortest path distance of u from s
s
Layer(i) = all nodes u such that d(u)=i
Edges(u, v): Cannot jump downwards across >=2 layers
Red Edge: Invalid: Cannot be in the graph!
Layer 0
Layer 1
Layer 2
Layer 3
Layer 4
For any edge (u, v), d(v) <= d(u) + 1
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
s a b u v w t0 1 2 3 4 5 6di+1
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
s a b u v w t0 1 2 3 4 5 6di+1
di 0 ? ? ? ? ? ?
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
Three kinds of edges e = (u, v) in the path:1. e in Gf and Hf in round i: di(v) = di(u) + 1
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
s a b u v w t0 1 2 3 4 5 6di+1
di 0 ? ? ? ? ? ?
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
Three kinds of edges e = (u, v) in the path:1. e in Gf and Hf in round i: di(v) = di(u) + 1
2. e in Gf but not Hf in round i: di(v) < di(u) + 1
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
s a b u v w t0 1 2 3 4 5 6di+1
di 0 ? ? ? ? ? ?
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
Three kinds of edges e = (u, v) in the path:1. e in Gf and Hf in round i: di(v) = di(u) + 1
2. e in Gf but not Hf in round i: di(v) < di(u) + 1
3. e not in Gf in round i: (v, u) in Hf in round i: di(u) = di(v)+1di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
s a b u v w t0 1 2 3 4 5 6di+1
di 0 ? ? ? ? ? ?
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
Three kinds of edges e = (u, v) in the path:1. e in Gf and Hf in round i: di(v) = di(u) + 1
2. e in Gf but not Hf in round i: di(v) < di(u) + 1
3. e not in Gf in round i: (v, u) in Hf in round i: di(u) = di(v)+1
s a b u v w t0 1 2 3 4 5 6di+1
di 0 1 1 2 1 2 2
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property: Augmenting along a blocking flow strictly increases the s-t distance in Gf
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
sLayer 0
Layer 1
Layer 2
Layer 3
Layer 4
Proof: Consider any s-t shortest path in Gf at round i+1
Three kinds of edges e = (u, v) in the path:1. e in Gf and Hf in round i: di(v) = di(u) + 1
2. e in Gf but not Hf in round i: di(v) < di(u) + 1
3. e not in Gf in round i: (v, u) in Hf in round i: di(u) = di(v)+1
Thus, di(t) <= di+1(t). Equal only if all blue edges. But then, the path is in Hf in round i, which cant be due to blocking flow!
s a b u v w t0 1 2 3 4 5 6di+1
di 0 1 1 2 1 2 2
di(u) = SP-distance(s,u) at round i A downwards edge can’t jump across >1 layers
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property 1: Augmenting along a blocking flow f increases the s-t distance in the residual graph
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
Property 2: After d iterations of blocking flow, size(max flow) <= size(current flow) + n/d
s tResidual Graph Blue = s-t paths
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f
Blocking Flows: Unit Graphs
Property 1: Augmenting along a blocking flow f increases the s-t distance in the residual graph
A blocking flow saturates >=1 edge in all s-t paths in G
An edge in a residual graph Gf is admissible if it lies on some s-t shortest path.
Property 2: After d iterations of blocking flow, size(max flow) <= size(current flow) + n/d
Proof: After d iterations of blocking flow, any s-t path in residual graph is at least d edges longSince Gf is a unit graph, each such s-t path has to involve a disjoint set of verticesThus there can be at most n/d more such augmenting paths
s tResidual Graph Blue = s-t paths
Blocking Flow Algorithm:Start with zero flowRepeat: Let Hf = subgraph of Gf containing only admissible edges Find a blocking flow in Hf, and add it to f