max flow and min-cost max flow - duke compsci...
TRANSCRIPT
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow and Min-Cost Max FlowDuke COMPSCI 309s
Siyang Chen
Spring 2014
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
Let G = (V ,E ) be directed graph with two special nodes s and t(called the source and sink, respectively). Suppose each edge has acapacity cu,v . A flow for is a function which assigns an amountfu,v to each edge, where f satisfies the following properties:
▶ For each u, v , fu,v ≤ cu,v . (Flows respect the capacityconstraint.)
▶ For each u ̸= s, t,∑
v fv ,u =∑
v fu,v . (Flow is conserved.)
▶ For each u, fu,s = ft,u = 0. (Flow does not flow into thesource or out of the sink.)
The maximum flow problem is to find a flow assignment whichmaximises the sum of the flow out of the source (or into the sink).
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
Let G = (V ,E ) be directed graph with two special nodes s and t(called the source and sink, respectively). Suppose each edge has acapacity cu,v . A flow for is a function which assigns an amountfu,v to each edge, where f satisfies the following properties:
▶ For each u, v , fu,v ≤ cu,v . (Flows respect the capacityconstraint.)
▶ For each u ̸= s, t,∑
v fv ,u =∑
v fu,v . (Flow is conserved.)
▶ For each u, fu,s = ft,u = 0. (Flow does not flow into thesource or out of the sink.)
The maximum flow problem is to find a flow assignment whichmaximises the sum of the flow out of the source (or into the sink).
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
How do we solve the max flow problem?
The two most commonalgorithms (Ford-Fulkerson and Edmonds-Karp) have the followingintuitive structure:
1. Find some path p from s to t which isn’t at full capacity yet.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more such paths.
Lot of tutorials and reference implementations available online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
How do we solve the max flow problem? The two most commonalgorithms (Ford-Fulkerson and Edmonds-Karp) have the followingintuitive structure:
1. Find some path p from s to t which isn’t at full capacity yet.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more such paths.
Lot of tutorials and reference implementations available online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
How do we solve the max flow problem? The two most commonalgorithms (Ford-Fulkerson and Edmonds-Karp) have the followingintuitive structure:
1. Find some path p from s to t which isn’t at full capacity yet.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more such paths.
Lot of tutorials and reference implementations available online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
How do we solve the max flow problem? The two most commonalgorithms (Ford-Fulkerson and Edmonds-Karp) have the followingintuitive structure:
1. Find some path p from s to t which isn’t at full capacity yet.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more such paths.
Lot of tutorials and reference implementations available online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
How do we solve the max flow problem? The two most commonalgorithms (Ford-Fulkerson and Edmonds-Karp) have the followingintuitive structure:
1. Find some path p from s to t which isn’t at full capacity yet.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more such paths.
Lot of tutorials and reference implementations available online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow
How do we solve the max flow problem? The two most commonalgorithms (Ford-Fulkerson and Edmonds-Karp) have the followingintuitive structure:
1. Find some path p from s to t which isn’t at full capacity yet.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more such paths.
Lot of tutorials and reference implementations available online.
..........
.....
.....................................................................
.....
......
.....
.....
.
March of the Penguins
Example problem: [March of the Penguins]
Solution:
1. For each island t, let’s determine whether we can send allpenguins to it. We can treat t as the sink and let the flow bethe number of penguins we send to t.
2. Suppose we can jump off island i at most mi times. We canmodel this constraint by splitting i into two nodes – iin andiout – and creating an edge between them with capacity mi .(This ‘node-splitting’ technique is a common way oftranslating node constraints into edge constraints.)
3. Lastly, we create a virtual source node which is connected toevery island i via an edge of capacity ni , the number ofpenguins initially on i .
..........
.....
.....................................................................
.....
......
.....
.....
.
March of the Penguins
Example problem: [March of the Penguins]Solution:
1. For each island t, let’s determine whether we can send allpenguins to it. We can treat t as the sink and let the flow bethe number of penguins we send to t.
2. Suppose we can jump off island i at most mi times. We canmodel this constraint by splitting i into two nodes – iin andiout – and creating an edge between them with capacity mi .(This ‘node-splitting’ technique is a common way oftranslating node constraints into edge constraints.)
3. Lastly, we create a virtual source node which is connected toevery island i via an edge of capacity ni , the number ofpenguins initially on i .
..........
.....
.....................................................................
.....
......
.....
.....
.
March of the Penguins
Example problem: [March of the Penguins]Solution:
1. For each island t, let’s determine whether we can send allpenguins to it. We can treat t as the sink and let the flow bethe number of penguins we send to t.
2. Suppose we can jump off island i at most mi times. We canmodel this constraint by splitting i into two nodes – iin andiout – and creating an edge between them with capacity mi .(This ‘node-splitting’ technique is a common way oftranslating node constraints into edge constraints.)
3. Lastly, we create a virtual source node which is connected toevery island i via an edge of capacity ni , the number ofpenguins initially on i .
..........
.....
.....................................................................
.....
......
.....
.....
.
March of the Penguins
Example problem: [March of the Penguins]Solution:
1. For each island t, let’s determine whether we can send allpenguins to it. We can treat t as the sink and let the flow bethe number of penguins we send to t.
2. Suppose we can jump off island i at most mi times. We canmodel this constraint by splitting i into two nodes – iin andiout – and creating an edge between them with capacity mi .(This ‘node-splitting’ technique is a common way oftranslating node constraints into edge constraints.)
3. Lastly, we create a virtual source node which is connected toevery island i via an edge of capacity ni , the number ofpenguins initially on i .
..........
.....
.....................................................................
.....
......
.....
.....
.
March of the Penguins
Example problem: [March of the Penguins]Solution:
1. For each island t, let’s determine whether we can send allpenguins to it. We can treat t as the sink and let the flow bethe number of penguins we send to t.
2. Suppose we can jump off island i at most mi times. We canmodel this constraint by splitting i into two nodes – iin andiout – and creating an edge between them with capacity mi .(This ‘node-splitting’ technique is a common way oftranslating node constraints into edge constraints.)
3. Lastly, we create a virtual source node which is connected toevery island i via an edge of capacity ni , the number ofpenguins initially on i .
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow Min Cut Theorem
A cut of the graph is a partitioning of the graph into two sets Xand Y . The capacity of this cut is defined to be∑
u∈X
∑v∈Y
cu,v
The max-flow min-cut theorem states that the maximum capacityof any cut where s ∈ X and t ∈ Y is equal to the max flow from sto t. This is actually a manifestation of the duality property oflinear programs, which we’ll investigate more next week.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow Min Cut Theorem
A cut of the graph is a partitioning of the graph into two sets Xand Y . The capacity of this cut is defined to be∑
u∈X
∑v∈Y
cu,v
The max-flow min-cut theorem states that the maximum capacityof any cut where s ∈ X and t ∈ Y is equal to the max flow from sto t.
This is actually a manifestation of the duality property oflinear programs, which we’ll investigate more next week.
..........
.....
.....................................................................
.....
......
.....
.....
.
Max Flow Min Cut Theorem
A cut of the graph is a partitioning of the graph into two sets Xand Y . The capacity of this cut is defined to be∑
u∈X
∑v∈Y
cu,v
The max-flow min-cut theorem states that the maximum capacityof any cut where s ∈ X and t ∈ Y is equal to the max flow from sto t. This is actually a manifestation of the duality property oflinear programs, which we’ll investigate more next week.
..........
.....
.....................................................................
.....
......
.....
.....
.
Bipartite Matching
A common application of max flow is to solve the bipartitematching problem:
Given a bipartite graph G = (V = X ∪ Y ,E ⊆ X × Y ), what isthe maximum number of edges we can choose such that no twoedges share a common vertex?
..........
.....
.....................................................................
.....
......
.....
.....
.
Bipartite Matching
A common application of max flow is to solve the bipartitematching problem:Given a bipartite graph G = (V = X ∪ Y ,E ⊆ X × Y ), what isthe maximum number of edges we can choose such that no twoedges share a common vertex?
..........
.....
.....................................................................
.....
......
.....
.....
.
Bipartite Matching
How do we reduce bipartite matching on G = (X ∪ Y ,E ) to maxflow?
1. Create edges of capacity 1 from s to every x ∈ X , and fromevery y ∈ Y to t.
2. Let edges e = (x , y) ∈ E have capacity 1 from x to y .
The max flow on the modified graph is the maximum bipartitematching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Bipartite Matching
How do we reduce bipartite matching on G = (X ∪ Y ,E ) to maxflow?
1. Create edges of capacity 1 from s to every x ∈ X , and fromevery y ∈ Y to t.
2. Let edges e = (x , y) ∈ E have capacity 1 from x to y .
The max flow on the modified graph is the maximum bipartitematching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Bipartite Matching
How do we reduce bipartite matching on G = (X ∪ Y ,E ) to maxflow?
1. Create edges of capacity 1 from s to every x ∈ X , and fromevery y ∈ Y to t.
2. Let edges e = (x , y) ∈ E have capacity 1 from x to y .
The max flow on the modified graph is the maximum bipartitematching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Bipartite Matching
How do we reduce bipartite matching on G = (X ∪ Y ,E ) to maxflow?
1. Create edges of capacity 1 from s to every x ∈ X , and fromevery y ∈ Y to t.
2. Let edges e = (x , y) ∈ E have capacity 1 from x to y .
The max flow on the modified graph is the maximum bipartitematching.
..........
.....
.....................................................................
.....
......
.....
.....
.
PythTriplets
Bipartite matching example: [PythTriplets]
Solution:
1. Critical observation: if a2 + b2 = c2 for integral a, b, and c ,then a and b cannot both be odd. (Can be proven viaquadratic residues.)
2. The problem statement also mentions that a and b cannotboth be even, so one must be odd and the other must be even.
3. Then the problem reduces to finding a maximum (bipartite)matching between odd and even numbers.
..........
.....
.....................................................................
.....
......
.....
.....
.
PythTriplets
Bipartite matching example: [PythTriplets]Solution:
1. Critical observation: if a2 + b2 = c2 for integral a, b, and c ,then a and b cannot both be odd. (Can be proven viaquadratic residues.)
2. The problem statement also mentions that a and b cannotboth be even, so one must be odd and the other must be even.
3. Then the problem reduces to finding a maximum (bipartite)matching between odd and even numbers.
..........
.....
.....................................................................
.....
......
.....
.....
.
PythTriplets
Bipartite matching example: [PythTriplets]Solution:
1. Critical observation: if a2 + b2 = c2 for integral a, b, and c ,then a and b cannot both be odd. (Can be proven viaquadratic residues.)
2. The problem statement also mentions that a and b cannotboth be even, so one must be odd and the other must be even.
3. Then the problem reduces to finding a maximum (bipartite)matching between odd and even numbers.
..........
.....
.....................................................................
.....
......
.....
.....
.
PythTriplets
Bipartite matching example: [PythTriplets]Solution:
1. Critical observation: if a2 + b2 = c2 for integral a, b, and c ,then a and b cannot both be odd. (Can be proven viaquadratic residues.)
2. The problem statement also mentions that a and b cannotboth be even, so one must be odd and the other must be even.
3. Then the problem reduces to finding a maximum (bipartite)matching between odd and even numbers.
..........
.....
.....................................................................
.....
......
.....
.....
.
PythTriplets
Bipartite matching example: [PythTriplets]Solution:
1. Critical observation: if a2 + b2 = c2 for integral a, b, and c ,then a and b cannot both be odd. (Can be proven viaquadratic residues.)
2. The problem statement also mentions that a and b cannotboth be even, so one must be odd and the other must be even.
3. Then the problem reduces to finding a maximum (bipartite)matching between odd and even numbers.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
One application of bipartite matching is to solve the minimumpath cover problem:
Given a graph G = (V ,E ), what is the minimum number ofdisjoint paths needed to cover all the vertices? When G is a DAG,this can be solved with bipartite matching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
One application of bipartite matching is to solve the minimumpath cover problem:Given a graph G = (V ,E ), what is the minimum number ofdisjoint paths needed to cover all the vertices?
When G is a DAG,this can be solved with bipartite matching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
One application of bipartite matching is to solve the minimumpath cover problem:Given a graph G = (V ,E ), what is the minimum number ofdisjoint paths needed to cover all the vertices? When G is a DAG,this can be solved with bipartite matching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
One application of bipartite matching is to solve the minimumpath cover problem:Given a graph G = (V ,E ), what is the minimum number ofdisjoint paths needed to cover all the vertices? When G is a DAG,this can be solved with bipartite matching.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
How do we reduce minimum path cover on a DAG to bipartitematching?
1. Note that a set of edges E ′ is a disjoint path covering if andonly if each node has indegree and outdegree of at most 1.
2. Each edge in E ′ reduces the number of paths we use. I.e., thesize of the minimum path cover is |V | − |E ′|. This means wewant to maximise |E ′| subject to the above constraint.
3. Split the nodes into vin and vout and connect vout and uin if(v , u) ∈ E . A matching in the split graph corresponds exactlyto a disjoint path covering in the original graph.
It suffices, then, to find a maximum bipartite matching for the splitgraph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
How do we reduce minimum path cover on a DAG to bipartitematching?
1. Note that a set of edges E ′ is a disjoint path covering if andonly if each node has indegree and outdegree of at most 1.
2. Each edge in E ′ reduces the number of paths we use. I.e., thesize of the minimum path cover is |V | − |E ′|. This means wewant to maximise |E ′| subject to the above constraint.
3. Split the nodes into vin and vout and connect vout and uin if(v , u) ∈ E . A matching in the split graph corresponds exactlyto a disjoint path covering in the original graph.
It suffices, then, to find a maximum bipartite matching for the splitgraph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
How do we reduce minimum path cover on a DAG to bipartitematching?
1. Note that a set of edges E ′ is a disjoint path covering if andonly if each node has indegree and outdegree of at most 1.
2. Each edge in E ′ reduces the number of paths we use. I.e., thesize of the minimum path cover is |V | − |E ′|. This means wewant to maximise |E ′| subject to the above constraint.
3. Split the nodes into vin and vout and connect vout and uin if(v , u) ∈ E . A matching in the split graph corresponds exactlyto a disjoint path covering in the original graph.
It suffices, then, to find a maximum bipartite matching for the splitgraph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
How do we reduce minimum path cover on a DAG to bipartitematching?
1. Note that a set of edges E ′ is a disjoint path covering if andonly if each node has indegree and outdegree of at most 1.
2. Each edge in E ′ reduces the number of paths we use. I.e., thesize of the minimum path cover is |V | − |E ′|. This means wewant to maximise |E ′| subject to the above constraint.
3. Split the nodes into vin and vout and connect vout and uin if(v , u) ∈ E . A matching in the split graph corresponds exactlyto a disjoint path covering in the original graph.
It suffices, then, to find a maximum bipartite matching for the splitgraph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Minimum Path Cover
How do we reduce minimum path cover on a DAG to bipartitematching?
1. Note that a set of edges E ′ is a disjoint path covering if andonly if each node has indegree and outdegree of at most 1.
2. Each edge in E ′ reduces the number of paths we use. I.e., thesize of the minimum path cover is |V | − |E ′|. This means wewant to maximise |E ′| subject to the above constraint.
3. Split the nodes into vin and vout and connect vout and uin if(v , u) ∈ E . A matching in the split graph corresponds exactlyto a disjoint path covering in the original graph.
It suffices, then, to find a maximum bipartite matching for the splitgraph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Taxi Cab Scheme
Example problem: [Taxi Cab Scheme]
Solution:
1. Let customers be nodes, and connect customers a and b withan edge if it’s possible to pick up customer b after droppingoff customer a.
2. The minimum number of taxis needed to accommodate allcustomers is then the minimum path cover of the above graph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Taxi Cab Scheme
Example problem: [Taxi Cab Scheme]Solution:
1. Let customers be nodes, and connect customers a and b withan edge if it’s possible to pick up customer b after droppingoff customer a.
2. The minimum number of taxis needed to accommodate allcustomers is then the minimum path cover of the above graph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Taxi Cab Scheme
Example problem: [Taxi Cab Scheme]Solution:
1. Let customers be nodes, and connect customers a and b withan edge if it’s possible to pick up customer b after droppingoff customer a.
2. The minimum number of taxis needed to accommodate allcustomers is then the minimum path cover of the above graph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Taxi Cab Scheme
Example problem: [Taxi Cab Scheme]Solution:
1. Let customers be nodes, and connect customers a and b withan edge if it’s possible to pick up customer b after droppingoff customer a.
2. The minimum number of taxis needed to accommodate allcustomers is then the minimum path cover of the above graph.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
The minimum-cost maximum flow problem uses the same settingas the maximum flow problem, except that each unit of flow has acost associated with it.
I.e., we want to minimise∑u,v
costu,v · fu,v
such that the maximum flow constraints still hold, i.e.∑v
fs,v
is still maximised.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
The minimum-cost maximum flow problem uses the same settingas the maximum flow problem, except that each unit of flow has acost associated with it. I.e., we want to minimise∑
u,v
costu,v · fu,v
such that the maximum flow constraints still hold, i.e.∑v
fs,v
is still maximised.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
The minimum-cost maximum flow problem uses the same settingas the maximum flow problem, except that each unit of flow has acost associated with it. I.e., we want to minimise∑
u,v
costu,v · fu,v
such that the maximum flow constraints still hold, i.e.∑v
fs,v
is still maximised.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
We can solve the min-cost flow problem in a similar manner as wesolve the max flow problem:
1. Find the shortest path p from s to t which isn’t at fullcapacity yet. Here the path length is defined to be the sum ofall the edge flow costs.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more paths.
Again, there are many tutorials and sample implementations online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
We can solve the min-cost flow problem in a similar manner as wesolve the max flow problem:
1. Find the shortest path p from s to t which isn’t at fullcapacity yet. Here the path length is defined to be the sum ofall the edge flow costs.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more paths.
Again, there are many tutorials and sample implementations online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
We can solve the min-cost flow problem in a similar manner as wesolve the max flow problem:
1. Find the shortest path p from s to t which isn’t at fullcapacity yet. Here the path length is defined to be the sum ofall the edge flow costs.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more paths.
Again, there are many tutorials and sample implementations online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
We can solve the min-cost flow problem in a similar manner as wesolve the max flow problem:
1. Find the shortest path p from s to t which isn’t at fullcapacity yet. Here the path length is defined to be the sum ofall the edge flow costs.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more paths.
Again, there are many tutorials and sample implementations online.
..........
.....
.....................................................................
.....
......
.....
.....
.
Min-Cost Flow
We can solve the min-cost flow problem in a similar manner as wesolve the max flow problem:
1. Find the shortest path p from s to t which isn’t at fullcapacity yet. Here the path length is defined to be the sum ofall the edge flow costs.
2. Send flow along p until p is at full capacity.
3. Repeat the previous steps until there are no more paths.
Again, there are many tutorials and sample implementations online.
..........
.....
.....................................................................
.....
......
.....
.....
.
DirectionBoard
Example min-cost flow problem: [DirectionBoard]