07 Network Flow Algorithms
07 Network Flow Algorithms
Problem
Network Flows
CSE 3101 2
Types of Networks
• Internet
• Telephone
• Cell
• Highways
• Rail
• Electrical Power
• Water
• Sewer
• Gas
• …
CSE 3101 3
Maximum Flow Problem
CSE 3101 4
•Instance:
Network Flow
•A Network is a directed graph G
•Edges represent pipes that carry flow
•Each edge (u,v) has a maximum capacity c(u,v)
•A source node s in which flow arrives
•A sink node t out which flow leaves
Goal:
Max Flow
CSE 3101 5
The Problem
• Use a graph to model material that flows through conduits.
• Each edge represents one conduit, and has a capacity, which is
an upper bound on the flow rate, in units/time.
• Can think of edges as pipes of different sizes.
• Want to compute max rate that we can ship material from a
designated source to a designated sink.
CSE 3101 6
What is a Flow Network?
CSE 3101 7
What is a Flow in a Network?
Skew symmetry: u ,v V , f (u ,v ) f (v , u )
Flow conservation: u V {s ,t }, f (u ,v ) 0
v V
Notes:
The skew symmetry condition implies that f(u,u)=0.
CSE 3101 8
Example of a Flow:
capacity
flow
capacity
| f | f ( s, v) f (v, t )
vV vV
This is the total flow leaving s = the total flow arriving in t.
CSE 3101 10
Example:
|f| = f(s, v1) + f(s, v2) + f(s, v3) + f(s, v4) + f(s, t) =
11 + 8 + 0 + 0 + 0 = 19
CSE 3101 11
A flow in a network
CSE 3101 12
Multiple Sources Network
• We have several sources and several targets.
• Want to maximize the total flow from all sources to all targets.
• Reduce to max-flow by creating a supersource and a supersink:
CSE 3101 13
Residual Networks
c f (u, v) c(u , v) f (u , v)
• The residual network of a graph G induced by a flow f is the graph including only the edges with positive
residual capacity, i.e.,
CSE 3101 14
Example of Residual Network
Flow Network:
Residual Network:
CSE 3101 15
Augmenting Paths
CSE 3101 16
End of Lecture 14
Network:
Residual
Network:
Augmenting
path The residual capacity of this augmenting path is 4.
CSE 3101 18
Computing Max Flow
• Classic Method:
– Identify augmenting path
– Increase flow along that path
– Repeat
CSE 3101 19
Ford-Fulkerson Method
CSE 3101 20
Example
Flow(1) Residual(1)
No more augmenting paths max flow attained.
CSE 3101 22
The Net Flow through a Cut (S,T)
f (S , T ) f (u, v)
uS ,vT
• f(S,T) = 12 – 4 + 11 = 19
CSE 3101 23
The Capacity of a Cut (S,T)
c( S , T ) c(u, v)
uS ,vT
• c(S,T)= 12+ 0 + 14 = 26
CSE 3101 24
Augmenting Paths – example
• Capacity of the cut
= maximum possible flow through the cut
= 12 + 7 + 4 = 23
Flow(2)
cut
• The network has a capacity of at most 23.
• In this case, the network does have a capacity of 23, because this
is a minimum cut.
CSE 3101 25
Net Flow of a Network
• The net flow across any cut is the same and equal to
the flow of the network |f|.
CSE 3101 26
Bounding the Network Flow
CSE 3101 27
Max-Flow Min-Cut Theorem
CSE 3101 28
The Basic Ford-Fulkerson Algorithm
CSE 3101 29
Example
augmenting path
Original Network
Flow Network
Resulting Flow = 4
CSE 3101 30
Example
Resulting Flow = 4
Flow Network
augmenting path
Residual Network
CSE 3101 31
Example
Resulting Flow = 11
Flow Network
augmenting path
Residual Network
CSE 3101 32
Example
Resulting Flow = 19
Flow Network
augmenting path
Residual Network
CSE 3101 33
Example
Resulting
Flow = 23
No augmenting path:
Maxflow=23
Residual Network
CSE 3101 34
Analysis
O(E)
?
O(E)
CSE 3101 35
Analysis
• If capacities are all integer, then each augmenting path
raises |f| by ≥ 1.
• If max flow is f*, then need ≤ |f*| iterations time is
O(E|f*|).
• Note that this running time is not polynomial in input
size. It depends on |f*|, which is not a function of |V| or |
E|.
• If capacities are rational, can scale them to integers.
• If irrational, FORD-FULKERSON might never terminate!
CSE 3101 36
The Basic Ford-Fulkerson Algorithm
|f*|=2,000,000
CSE 3101 37
Run Ford-Fulkerson on this example
Augmenting Path
Residual Network
CSE 3101 38
Run Ford-Fulkerson on this example
Augmenting Path
Residual Network
CSE 3101 39
Run Ford-Fulkerson on this example
CSE 3101 40
The Edmonds-Karp Algorithm
CSE 3101 41
The Edmonds-Karp Algorithm - example
CSE 3101 42
Further Improvements
CSE 3101 43
An Application of Max Flow:
CSE 3101 45
• A matching in a graph is a subset M of E, such that for all
vertices v in V, at most one edge of M is incident on v.
CSE 3101 46
• A maximum matching is a matching of maximum cardinality
(maximum number of edges).
CSE 3101 47
A Maximum Matching
• No matching of cardinality 4,
because only one of v and u
can be matched.
v
• In the workers-jobs example
a max-matching provides
work for as many people as
possible.
CSE 3101 48
Solving the Maximum Bipartite Matching Problem
CSE 3101 49
Corresponding Flow Network
• To form the corresponding flow network G' of the bipartite graph G:
– Add a source vertex s and edges from s to L.
– Direct the edges in E from L to R.
– Add a sink vertex t and edges from R to t.
– Assign a capacity of 1 to all edges.
• Claim: max-flow in G’ corresponds to a max-bipartite-matching on G.
GG’
1
1
1 1
1
1 1
1
1
1 1
s 1 1 t
1 1
L R
CSE 3101 50
Solving Bipartite Matching as Max Flow
Let G (V , E ) be a bipartite graph with vertex partition V L R .
If M is a matching in G ,
CSE 3101 51
Does this mean that max |f| = max |M|?
CSE 3101 52
Integrality Theorem
CSE 3101 53
Example
min cut
CSE 3101 55
Example
CSE 3101 56