Title: Network Flow
1Network Flow Linear Programming
Thinking about Algorithms Abstractly
Network Flow Linear Programming
- Jeff Edmonds York University
COSC 3101
Lecture 5
2Optimization Problems
- Ingredients
- Instances The possible inputs to the problem.
- Solutions for Instance Each instance has an
exponentially large set of solutions. - Cost of Solution Each solution has an easy to
compute cost or value. - Specification
- ltpreCondgt The input is one instance.
- ltpostCondgt A valid solution with optimal cost.
(minimum or maximum)
3Network Flow
- Instance
- A Network is a directed graph G
- Edges represent pipes that carry flow
- Each edge ltu,vgt has a maximum capacity cltu,vgt
- A source node s out of which flow leaves
- A sink node t into which flow arrives
Goal Max Flow
4Network Flow
- Instance
- A Network is a directed graph G
- Edges represent pipes that carry flow
- Each edge ltu,vgt has a maximum capacity cltu,vgt
- A source node s out of which flow leaves
- A sink node t into which flow arrives
5Network Flow
- Solution
- The amount of flow Fltu,vgt through each edge.
- Flow cant exceed capacity i.e. Fltu,vgt ? cltu,vgt.
- Unidirectional flow
- Fltu,vgt ? 0 and Fltv,ugt 0
- or
- Fltu,vgt 0 and Fltv,ugt ? 0
Some texts Fltu,vgt -Fltv,ugt
6Network Flow
- Solution
- The amount of flow Fltu,vgt through each edge.
- Flow Fltu,vgt cant exceed capacity cltu,vgt.
- Unidirectional flow
- No leaks, no extra flow.
7Network Flow
- Solution
- The amount of flow Fltu,vgt through each edge.
- Flow Fltu,vgt cant exceed capacity cltu,vgt.
- Unidirectional flow
- No leaks, no extra flow.
- For each node v flow in flow out
- ?u Fltu,vgt ?w Fltv,wgt
8Network Flow
- Value of Solution
- Flow from s into the network
- minus flow from the network back into s.
- rate(F) ?u Flts,ugt
- flow from network into t
- minus flow back in.
- ?u Fltu,tgt - ?v Fltt,vgt
- ?v Fltv,sgt
What about flow back into s?
Goal Max Flow
9Network Flow
A network with its edge capacities
What is the maximum that can flow from s to t?
10Network Flow
A network with its edge capacities
Prove that total can not be higher.
11Network Flow
No more flow can be pushedalong the top path
because theedge ltb,cgt is at capacity.
Similarly, the edge lte,fgt. No flow is pushed
along the bottom path because this would decrease
the total from s to t.
12Network Flow
ltU,Vgt is a minimum cut Its capacity is the sum
of the capacities crossing the cut 12 3.
lti,jgt is not included in because it is going in
the wrong direction.
13Network Flow
The edges crossing forward across the cut are at
capacity those crossing backwards have zero
flow. This is always true.
14Network Flow
The maximum flow is 123 The minimum cut is
123. These are always equal.
15An Application Matching
3 matches Can we do better?
4 matches
Who loves whom.
Who should be matched with whomso as many as
possible matchedand nobody matched twice?
16An Application Matching
s
t
1
1
u
v
- clts,ugt 1
- Total flow out of u flow into u ? 1
- Boy u matched to at most one girl.
- cltv,tgt 1
- Total flow into v flow out of v ? 1
- Girl v matched to at most one boy.
17Network Flow
- Strategy
- Push flow into s.
- Must make decisions.
- Get stuck and must backtrack.
- Difficult and time consuming.
c100
f100
s
f100
18Network Flow
- Strategy
- Find a path for a single drop.
- Push as much flow through as fits.
- w augment Minltu,vgt ? Path cltu,vgt
19Network Flow
- Strategy
- Find a path for a single drop.
- Push as much flow through as fits.
- w augment Minltu,vgt ? Path cltu,vgt
flow/capacity 20/21
20Network Flow
- Given Flow F
- Construct Augmenting Graph GF
- Find path P using BFS, DFS, or generic search
algorithm - Let w be the max amount flowcan increase along
path P. - Increase flow along path P by w.
- i.e newF oldF w P
21Network Flow
- Given Flow F
- Construct Augmenting Graph GF
- Find path P using BFS, DFS, or generic search
algorithm - No path
- Stop.
22Hill Climbing
We have a valid solution.
(not necessarily optimal)
Value of our solution.
Take a step that goes up.
Make small local changes to your solution
toconstruct a slightly better solution.
Initially have the zero
Global Max
Cant take a step that goes up.
Local Max
Problems
Running time?
Can our Network Flow Algorithm get stuck in a
local maximum?
If you take small step,could be exponential time.
23Network Flow
Can our Network Flow Algorithm get stuck in
local max?
Yes!
Need only one example.
24Network Flow
25Network Flow
Yes! Our Network Flow Algorithm can get stuck.
Need only one example.
26Hill Climbing
Avoiding getting stuck in a local maximum
Good Execution
Bad Execution
- Made better choices of direction
- Hard
- Back up an retry
- Exponential time
- Define a bigger step
27Hill Climbing
Different Solutions
Current Solution
28Hill Climbing
Different Solutions
Current Solution
This defines the topography
Perhaps removes some local maxima
29Network Flow
Mistake?
Putting 2 through this edge
30Network Flow
Mistake?
We need to decrease the flow in this edge. But if
we decrease the total flow thenthe algorithm
might run exponentially or forever. We need to
decrease the flow in this edgeAND increase the
total flow.
Putting 2 through this edge
31Network Flow
How much flow can I have in each direction?
Where can I stand in each direction?
32Network Flow
How much flow can I have in each direction?
Where can I stand in each direction?
75 to the right
33Network Flow
How much flow can I have in each direction?
Where can I stand in each direction?
10 to the left
75 to the right
34Network Flow
15
Walking 5
Add 5 flow to right
20
35Network Flow
15
Walking 75-1560
Add 75-1560 flow to right
36Network Flow
15
How much can I change my position to the right?
How much flow can I add to right
75-1560
37Network Flow
f/c
u
v
0/10
15
How much can I change my position to the right?
How much flow can I add to right
f-c
75-1560
38Network Flow
15
Walking 5
Add 5 flow to left
Edge shouldnt have flow in both directions.
10
39Network Flow
15
Walking 5
Add 5 flow to left
10
40Network Flow
15
Walking 15
Add 15 flow to left
41Network Flow
15
Walking 15520
Add 15520 flow to left
-5
Edge shouldnt have negative flow.
42Network Flow
15
Walking 15520
Add 15520 flow to left
-5
43Network Flow
15
Walking 151025
Add 151025 flow to left
44Network Flow
15
How much can I change my position to the left?
How much flow can I add to left
15
151025
45Network Flow
f/75
u
v
0/c
15
How much can I change my position to the left?
How much flow can I add to left
u
v
15
fc
Augmentation Graph
151025
46Network Flow
cltv,ugt
Fltu,vgt
cltu,vgt
Flow Graph
Augmentation Graph
Fltu,vgt/cltu,vgt
cltu,vgt-Fltu,vgt
u
v
u
v
Fltu,vgtcltv,ugt
0/cltv,ugt
47Network Flow
cltu,vgt
Fltu,vgt
cltu,vgt
Flow Graph
Augmentation Graph
Fltu,vgt/cltu,vgt
cltu,vgt-Fltu,vgt
u
v
u
v
Fltu,vgtcltv,ugt
0/cltv.ugt
48Network Flow
Where we got stuck before
Old New
- Given Flow F
- Construct Augmenting Graph GF
- Find path P
49Network Flow
?
-w
- Given Flow F
- Construct Augmenting Graph GF
- Find path P
- Let w be the max amount flowcan increase along
path P. - Increase flow along path P by w.
- i.e newF oldF w P
50Network Flow
- Given Flow F
- Construct Augmenting Graph GF
- Find path P using BFS, DFS, or generic search
algorithm - No path
- Stop.
51Network Flow
Same Input
Previous Input
Previous Output
Worse Output
Same Output
52(No Transcript)
53An Application Matching
3 matches Can we do better?
4 matches
Who loves whom.
Who should be matched with whomso as many as
possible matchedand nobody matched twice?
54An Application Matching
Flow
s
t
- Alternates
- adding edge
- removing edge
- adding edge
- removing edge
- adding edge
- Extra edge added
55An Application Matching
3 matches Can we do better?
4 matches
Who loves whom.
Who should be matched with whomso as many as
possible matchedand nobody matched twice?
56Network Flow
Can our Network Flow Algorithm get stuck in
local max?
No!
Need to prove for every input network for every
choice of augmenting paths Maximum Flow is
found!
How?
57Primal-Dual Hill Climbing
58Primal-Dual Hill Climbing
- Primal Problem
- Exponential of locations to stand.
- Find a highest one.
- Dual problem
- Exponential of roofs.
- Find a lowest one.
59Primal-Dual Hill Climbing
- Prove
- Every roof is above every location to stand.
- ? R ? L height(R) ? height(L)
- ? height(Rmin) ? height(Lmax)
- Is there a gap?
60Primal-Dual Hill Climbing
- Prove
- For every location to stand either
- the alg takes a step up or
- the alg gives a reason that explains why not by
giving a ceiling of equal height. - i.e. ? L ? L height(L) ? height(L) or
- ? R height(R) height(L)
or
But ? R ? L height(R) ? height(L)
61Primal-Dual Hill Climbing
- Prove
- For every location to stand either
- the alg takes a step up or
- the alg gives a reason that explains why not by
giving a ceiling of equal height. - i.e. ? L ? L height(L) ? height(L) or
- ? R height(R) height(L)
or
?
Cant go up from this location and no matching
ceiling.
Cant happen!
62Primal-Dual Hill Climbing
- Prove
- For every location to stand either
- the alg takes a step up or
- the alg gives a reason that explains why not by
giving a ceiling of equal height. - i.e. ? L ? L height(L) ? height(L) or
- ? R height(R) height(L)
or
63Primal-Dual Hill Climbing
Claim Primal and dual have the same optimal
value. height(Rmin)
height(Lmax) Proved ? R ? L, height(R) ?
height(L) Proved Alg runs until it provides
Lalg and Ralg
height(Ralg) height(Lalg)
height(Rmin) ?
height(Ralg)
height(Lalg) ?
height(Lmax)
height(Rmin) ?
height(Lmax)
Lalg witness that height(Lmax) is no smaller.
Ralg witness that height(Lmax) is no bigger.
64Network Flow
A network with its edge capacities
What is the maximum that can flow from s to t?
65Network Flow
A network with its edge capacities
Prove that total can not be higher.
66Network Flow
No more flow can be pushedalong the top path
because theedge ltb,cgt is at capacity.
Similarly, the edge lte,fgt. No flow is pushed
along the bottom path because this would decrease
the total from s to t.
67Network Flow
ltU,Vgt is a minimum cut Its capacity is the sum
of the capacities crossing the cut 12 3.
lti,jgt is not included in because it is going in
the wrong direction.
68Network Flow
The edges crossing forward across the cut are at
capacity those crossing backwards have zero
flow. This is always true.
69Network Flow
The maximum flow is 123 The minimum cut is
123. These are always equal.
70Primal-Dual Network Flow
Primal Problem Max Flow
Dual Problem
What are the roofs to the flows?
Min Cut
71Min Cut
- Instance
- A Network is a directed graph G
- Special nodes s and t.
- Edges represent pipes that carry flow
- Each edge ltu,vgt has a maximum capacity cltu,vgt
72Min Cut
- Instance
- A Network is a directed graph G
- Special nodes s and t.
- Edges represent pipes that carry flow
- Each edge ltu,vgt has a maximum capacity cltu,vgt
73Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
74Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
75Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
76Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
77Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
78Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
79Min Cut
- Solution
- C partition of nodes ltU,Vgt with s?U, t?V.
80Min Cut
- Value Solution CltU,Vgt
- cap(C) how much can flow from U to V
- ?u?U,v?V cltu,vgt
Goal Min Cut
s
t
81Max Flow Min Cut
- Theorem
- For all Networks MaxF rate(F) MinC cap(C)
- Prove ? F,C rate(F) ? cap(C)
- Prove ? flow F, alg either
- finds a better flow F
- or finds cut C such that rate(F) cap(C)
- Alg stops with an F and C for which rate(F)
cap(C) - F witnesses that the optimal flow cant be less
- C witnesses that it cant be more.
82Max Flow Min Cut
- Prove ? flow F, cut C rate(F) ? cap(C)
- cap(C) how much can flow from U to V
?u?U,v?V cltu,vgt - rate(F,C) Flow from U to V minus flow V to U
- ?u?U,v?V Fltu,vgt - Fltv,ugt
- Lemma rate(F) rate(F,C) ? cap(C)
Fltu,vgt ? cltu,vgt Fltv,ugt ? 0
U
Canada
V
USA
83Max Flow Min Cut
- Lemma ?F,C rate(F,C) rate(F)
- Proof By induction on the size of U.
- Base case C lts,G-sgt
- rate(F) Flow from s into the network
minus flow from the network back
into s - ?u Flts,ugt - ?v Fltv,tgt
- rate(F,C) where C lts,G-sgt
84Max Flow Min Cut
Lemma ?F,C rate(F,C) rate(F) Proof By
induction on the size of C. Base case C
lts,G-sgt Inductive step Move nodes across C
one at a time.
Flow into x Flow out.
Hence, flow across cut does not change.
rate(F) rate(F,C) ? cap(C)
85Max Flow Min Cut
- Theorem
- For all Networks MaxF rate(F) MinC cap(C)
- Prove ? F,C rate(F) ? cap(C)
- Prove ? flow F, alg either
- finds a better flow F
- or finds cut C such that rate(F) cap(C)
86Max Flow Min Cut
-w
- Given Flow F
- Construct Augmenting Graph GF
- Find path P
- Let w be the max amount flowcan increase along
path P. - Increase flow along path P by w.
- i.e newF oldF w P
87Max Flow Min Cut
- Given Flow F
- Construct Augmenting Graph GF
- Find path P using BFS, DFS, or generic search
algorithm - No path
- Stop
88Max Flow Min Cut
Bad example because U might contain more than s.
- Let Falg be this final flow.
- Let cut CalgltU,Vgt,
- where U are the nodes reachable from s in the
augmented graph - and V not.
- Claim rate(Falg) cap(Calg)
89Max Flow Min Cut
- Prove rate(Falg) cap(Calg)
90Max Flow Min Cut
- Prove rate(Falg,Calg) cap(Calg)
- cap(C) how much can flow from U to V
?u?U,v?V cltu,vgt - rate(F,C) Flow from U to V minus flow V to U
- ?u?U,v?V Fltu,vgt - Fltv,ugt
91Max Flow Min Cut
- Prove rate(Falg,Calg) cap(Calg)
- cap(C) how much can flow from U to V
?u?U,v?V cltu,vgt - rate(F,C) Flow from U to V minus flow V to U
- ?u?U,v?V Fltu,vgt - Fltv,ugt
92Max Flow Min Cut
- Prove rate(Falg,Calg) cap(Calg)
- cap(C) how much can flow from U to V
?u?U,v?V cltu,vgt - rate(F,C) Flow from U to V minus flow V to U
- ?u?U,v?V Fltu,vgt - Fltv,ugt
Flow Graph
reachable from s
not
not edges
cltu,vgt-Fltu,vgt
Fltu,vgt/cltu,vgt
u
v
Fltu,vgt/cltu,vgt
v
u
Fltu,vgtcltv,ugt
93Max Flow Min Cut
- Theorem
- For all Networks MaxF rate(F) MinC cap(C)
- Prove ? F,C rate(F) ? cap(C)
- Prove ? flow F, alg either
- finds a better flow F
- or finds cut C such that rate(F) cap(C)
- Alg stops with an F and C for which rate(F)
cap(C) - F witnesses that the optimal flow cant be less
- C witnesses that it cant be more.
94Hill Climbing
Problems
Can our Network Flow Algorithm get stuck in a
local maximum?
No!
95Hill Climbing
Problems
Running time?
If you take small step,could be exponential time.
96Network Flow
97Network Flow
Add flow 1
98Network Flow
Add flow 1
99Hill Climbing
Problems
Running time?
- If each iteration you take the biggest step
possible, - Alg is poly time
- in number of nodes
- and number of bits in capacities.
- If each iteration you take path with the
fewest edges - Alg is poly time
- in number of nodes
100Taking the biggest step possible
101Linear Programming
102A Hotdog
A combination of pork, grain, and sawdust,
103- Constraints
- Amount of moisture
- Amount of protein,
104The Hotdog Problem
Given todays prices,what is a fast algorithm
to find the cheapest hotdog?
105(No Transcript)
106Abstract Out Essential Details
Cost 29, 8, 1, 2
107Abstract Out Essential Details
Minimize
29x1 8x2 1x3 2x4
3x1 4x2 7x3 8x4 ³ 12
Subject to
2x1 - 8x2 4x3 - 3x4 ³ 24
-8x1 2x2 3x3 - 9x4 ³ 8
x1 2x2 9x3 - 3x4 ³ 31
108A Fast Algorithm
For decades people thought that there was no fast
algorithm.
Then one was found!
Theoretical Computer Science finds new algorithms
every day.
109Network Flow as a Linear Program
- Given an instance of Network Flow ltG,cltu,vgtgt
express it as a Linear Program - The variables
- Maximize
- Subject to
Flows fltu,vgt for each edge.
rate(F) ?u Fltu,tgt - ?v Fltt,vgt
?ltu,vgt Fltu,vgt ? cltu,vgt. (Flow cant exceed
capacity)
?v ?u Fltu,vgt ?w Fltv,wgt (flow in flow out)
110(No Transcript)
111Dual
Primal
112End
113Network Flow
Locally in each edge,we see how much we can
change the flow. Allowed Flow ? Allowed Change in
Flow People find this hard. This is my first
attempt to explain it.
114Network Flow
115Network Flow
116Network Flow
Not allowed
117Network Flow
Not allowed
118Network Flow
10? 75 Fltu,vgt? cltu,vgt ? cltu,vgt-F
ltu,vgt
119Network Flow
10-? -10 Fltu,vgt-? -cltv,ugt ? F ltu,vgt
cltv,ugt
120Network Flow
121Network Flow
Locally in each edge,we see how much we can
change the flow. Allowed Flow ? Allowed Change in
Flow People find this hard. This is my second
attempt to explain it.
122Network Flow
123Network Flow
75-2154
124Network Flow
75-2154
?
10
125Network Flow
Capacity of 10 not met
75-2154
?
10
126Network Flow
Capacity of 10 exceeded
75-2154
?
10
127Network Flow
Capacity of 10 not met
75-2154
?
10
128Network Flow
Capacity of 10 met
75-2154
211031
129Network Flow
75-2154
211031
130Network Flow
cltv,ugt
Fltu,vgt
cltu,vgt
Flow Graph
Augmentation Graph
Fltu,vgt/cltu,vgt
cltu,vgt-Fltu,vgt
u
v
u
v
Fltu,vgtcltv,ugt
0/cltv,ugt
131Network Flow
cltu,vgt
Fltu,vgt
cltu,vgt
Flow Graph
Augmentation Graph
Fltu,vgt/cltu,vgt
cltu,vgt-Fltu,vgt
u
v
u
v
Fltu,vgtcltv,ugt
0/cltv.ugt