SlideShare a Scribd company logo
1 of 49
Maximum Flow Computation
Programming Puzzles and Competitions
CIS 4900 / 5920
Spring 2009
Outline
• Flow analysis
• The min-cut and max-flow problems
• Ford-Fulkerson and Edmonds-Karp
max-flow algorithms
• Start of an example problem from
ICPC’07 (“Tunnels”)
Flow Network
• Directed graph G = (V, E) with
– edge capacities c(u,v) ≥ 0
– a designated source node s
– a designated target/sink node t
– flows on edges f(u,v)
s
b
t
a
5
4
3
1
2
Network
c(s,a) = 2
c(s,b) = 5
c(a,b) = 1
c(a,t) = 4
c(b,t) = 3
Flow Constraints
s
b
t
a
5
4
1|3
1|1
1|2
f(s,a) = 1
f(a,s) = -1
f(a,b) = 1
f(b,a) = -1
f(b,t) = 1
f(t,b) = -1
capacity: f(u,v) ≤ c(u,v)
symmetry: f(u,v) = -f(v,u)
conservation: 




V
v
t}
{s,
V
u
v)
f(u,
Applications
• fluid in pipes
• current in an electrical circuit
• traffic on roads
• data flow in a computer network
• money flow in an economy
• etc.
Maximum Flow Problem
Assuming
– source produces the material at a
steady rate
– sink consumes the material at a steady rate
What is the maximum net flow from s to t?
Ford-Fulkerson Algorithm
• Start with zero flow
• Repeat until convergence:
– Find an augmenting path, from s to t
along which we can push more flow
– Augment flow along this path
Residual Capacity
• Given a flow f in network G = (V, E)
• Consider a pair of vertices u, v є V
• Residual capacity =
amount of additional flow we can push directly from u to v
cf (u, v) = c(u, v)  f (u, v)
≥ 0 since f (u, v) ≤ c(u, v)
• Residual network Gf = (V, Ef )
Ef = { (u, v) є V ×V | cf (u, v) > 0 }
• Example:
c(u,v) = 16, f(u,v) = 5  cf (u, v) = 11
s
b
t
a
5
4
3
1
2
Example (1)
s
b
t
a
5
4
1|3
1|1
1|2
graph with flow
original graph
s
b
t
a
s
b
t
a
5
4
1
1
1|2
1|1
1|3
1
2
1
residual graph
graph with flow
5
4
Example (2)
s
b
t
a
5
1|4
1|1
2|2
1|3
s
b
t
a
5
1|4
1
1|1
1
2
1
residual graph, with flow-augmenting path
original graph
with new flow
Example (3)
s
b
t
a
5
1|4
1|1
2|2
1|3
original graph with new flow
new residual graph
s
b
t
a
5
3
1
2
2
1
1
Example (4)
s
b
t
a
1|5
2|4
1
2|2
1|3
s
b
t
a
5
3
1
2
2
1
new residual graph, with augmenting path
1
original graph
with new flow
Example (5)
s
b
t
a
1|5
2|4
1
2|2
1|3
original graph with new flow
new residual graph
s
b`
t
a
4
2
1
2
2
2
1
1
Example (6)
s
b
t
a
2|5
2|4
1
2|2
3|3
new residual graph, with augmenting path
original graph
with new flow
s
b
t
a
4
2
1
2
2
2
1
1
Example (7)
s
b
t
a
2|5
2|4
1
2|2
3|3
original graph, with new flow
residual graph
(maximum flow = 5)
Example (8)
s
b
t
a
2
2
1
2
2
3
2
Ford-Fulkerson Algorithm
for (each edge (u,v) є E[G])
f[u][v] = f[v][u] = 0;
while ( path p from s to t in Gf) {
cf(p) = min {cf(u,v) | (u,v) є p};
for (each edge (u,v) є p) {
f[u][v] = f[u][v] + cf(p)
f[v][u] = -f[u][v]
}
}
O(E)
O(E)
O(E x f*)
f* = maximum flow, assuming integer flows,
since each iteration increases flow by at least one unit
int findMaxFlow (int s, int t) {
int result = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) flow[i][j] = 0;
for (;;) {
int Increment = findAugmentingPath(s, t);
if (Increment == 0) return result;
result += capTo[t];
int v = t, u;
while (v != s) { // augment flow along path
u = prev[v];
flow[u][v] += capTo[t];
flow[v][u] -= capTo[t];
v = u;
}}}
static int findAugmentingPath(int s, int t) {
for (int i = 0; i < n; i++) {
prev[i] = -1;
capTo[i] = Integer.MAX_VALUE;}
int first = 0, last = 0;
queue[last++] = s; prev[s] = -2; // s visited already
while (first != last) {
int u = queue[first++];
for (int v = 0; v < n; v++) {
if (a[u][v] > 0) {
int edgeCap = a[u][v] - flow[u][v];
if ((prev[v] == -1) && (edgeCap > 0)) {
capTo[v] = Math.min(capTo[u], edgeCap);
prev[v] = u;
if (v == t) return capTo[v];
queue[last++] = v;
}}}}
return 0;
}
This uses breadth-first search, which is the basis
of the Edmonds-Karp algorithm.
Example: Finding Augmenting Path
v0
v2
v3
v1
v4
v5
v6
v7
source
queue = { v0 }
target
1|3
1|3 1|4
2/3
1|1
3
2|4
3
2
1
3
1
1
∞
1 = capTo
= prev
Application to Augmenting Path
v0
v2
v3
v1
v4
v5
v6
v7
source
queue = { v1, v2 }
target
1|3
1|3 1|4
2/3
1|1
3
2|4
3
2
1
3
1
1
∞
2
2
Application to Augmenting Path
v0
v2
v3
v1
v4
v5
v6
v7
source
queue = {v2}
target
1|3
1|3 1|4
2/3
1|1
3
2|4
3
2
1
3
3
1
∞
2
2
Application to Augmenting Path
v0
v2
v3
v1
v4
v5
v6
v7
source
queue = {v3}
target
1|3
1|3 1|4
2/3
1|1
3
2|4
3
2
1
3
3
1
∞
2
2
2
Application to Augmenting Path
v0
v2
v3
v1
v4
v5
v6
v7
source
queue = {v4, v5}
target
1|3
1|3 1|4
2/3
1|1
3
2|4
3
2
1
3
1
1
∞
2
1
1
2
1
Application to Augmenting Path
v0
v2
v3
v1
v4
v5
v6
v7
source
queue = { v5, v6 }
target
1|3
1|3 1|4
2/3
1|1
3
2|4
3
2
1
3
1
1
∞
2
1
1
2
1
1
Done
Breadth-first search
• The above is an example
• Depth-first search is an alternative
• The code is nearly the same
• Only the queuing order differs
static int findAugmentingPath(int s, int t) {
for (int i = 0; i < n; i++) {
prev[i] = -1;
capTo[i] = Integer.MAX_VALUE;}
int first = 0, last = 0;
queue[last++] = s; prev[s] = -2; // s visited already
while (first != last) {
int u = queue[last--];
for (int v = 0; v < n; v++) {
if (a[u][v] > 0) {
int edgeCap = a[u][v] - flow[u][v];
if ((prev[v] == -1) && (edgeCap > 0)) {
capTo[v] = Math.min(capTo[u], edgeCap);
prev[v] = u;
if (v == t) return capTo[v];
queue[last++] = v;
}}}}
return 0;
}
This uses depth-first search.
Breadth vs. Depth-first Search
• Let s be the start node
ToVisit.make_empty; ToVisit.insert(s); s.marked =
true;
while not ToVisit.is_empty {
u = ToVisit.extract;
for each edge (u,v) in E
if not u.marked {
u.marked = true; ToVisit.insert(u);
}}
If Bag is a FIFO queue, we get breadth-first search;
if LIFO (stack), we get dept-first.
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v0 }
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v2 }
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v2, v3 }
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v2, v3 }
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v4, v5 }
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v5 , v6}
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = {v6}
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = {v7}
Breadth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = {}
Depth-first Search
• Now see what happens if ToVisit is
implemented as a stack (LIFO).
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v0 }
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v2 }
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v3 }
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v4, v5}
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v4 }
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v6}
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1, v7}
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { v1 }
Depth-first Search
v0
v2
v3
v1
v4
v5
v6
v7
start
ToVisit = { }

More Related Content

Similar to flows.ppt

Session 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptxSession 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptx
SATHWIKCHAKRI
 
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdfreservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
RTEFGDFGJU
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12
Traian Rebedea
 

Similar to flows.ppt (20)

Clustering and Factorization using Apache SystemML by Alexandre V Evfimievski
Clustering and Factorization using Apache SystemML by  Alexandre V EvfimievskiClustering and Factorization using Apache SystemML by  Alexandre V Evfimievski
Clustering and Factorization using Apache SystemML by Alexandre V Evfimievski
 
Session 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptxSession 13 - Single Source Shortest Path Method.pptx
Session 13 - Single Source Shortest Path Method.pptx
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
Max flow min cut
Max flow min cutMax flow min cut
Max flow min cut
 
Graps 2
Graps 2Graps 2
Graps 2
 
MaximumFlow.ppt
MaximumFlow.pptMaximumFlow.ppt
MaximumFlow.ppt
 
Generating and Analyzing Events
Generating and Analyzing EventsGenerating and Analyzing Events
Generating and Analyzing Events
 
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdfreservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
 
ABA Problem
ABA ProblemABA Problem
ABA Problem
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Network flows
Network flowsNetwork flows
Network flows
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Maximum flow
Maximum flowMaximum flow
Maximum flow
 
Maxflow
MaxflowMaxflow
Maxflow
 
All pair shortest path by Sania Nisar
All pair shortest path by Sania NisarAll pair shortest path by Sania Nisar
All pair shortest path by Sania Nisar
 
Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12Algorithm Design and Complexity - Course 12
Algorithm Design and Complexity - Course 12
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
2-Vector.pptx
2-Vector.pptx2-Vector.pptx
2-Vector.pptx
 

More from KrishanPalSingh39

More from KrishanPalSingh39 (7)

Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.ppt
 
L21-MaxFlowPr.ppt
L21-MaxFlowPr.pptL21-MaxFlowPr.ppt
L21-MaxFlowPr.ppt
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
HardwareIODevice.ppt
HardwareIODevice.pptHardwareIODevice.ppt
HardwareIODevice.ppt
 
fdocuments.in_unit-2-foc.ppt
fdocuments.in_unit-2-foc.pptfdocuments.in_unit-2-foc.ppt
fdocuments.in_unit-2-foc.ppt
 
wang.ppt
wang.pptwang.ppt
wang.ppt
 

Recently uploaded

Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 

Recently uploaded (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 

flows.ppt