Maximum Flow Computation Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.

Slides:



Advertisements
Similar presentations
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
Advertisements

1 Review of some graph algorithms Graph G(V,E) (Chapter 22) –Directed, undirected –Representation Adjacency-list, adjacency-matrix Breadth-first search.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
MAXIMUM FLOW Max-Flow Min-Cut Theorem (Ford Fukerson’s Algorithm)
The Maximum Network Flow Problem. CSE Network Flows.
Chapter 10: Iterative Improvement The Maximum Flow Problem The Design and Analysis of Algorithms.
Advanced Algorithm Design and Analysis (Lecture 8) SW5 fall 2004 Simonas Šaltenis E1-215b
Lectures on Network Flows
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
1 The Max Flow Problem. 2 Flow networks Flow networks are the problem instances of the max flow problem. A flow network is given by 1) a directed graph.
CS138A Network Flows Peter Schröder. CS138A Flow Networks Definitions a flow network G=(V,E) is a directed graph in which each edge (u,v)
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Lecture 4 Tuesday, 2/19/02 Graph Algorithms: Part 2 Network.
1 Maximum flow problems. 2 - Introduction of: network, max-flow problem capacity, flow - Ford-Fulkerson method pseudo code, residual networks, augmenting.
1 Maximum Flow Maximum Flow Problem The Ford-Fulkerson method Maximum bipartite matching.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 4 Tuesday, 10/2/01 Graph Algorithms: Part 2 Network.
Yangjun Chen 1 Network Flow What is a network? Flow network and flows Ford-Fulkerson method - Residual networks - Augmenting paths - Cuts of flow networks.
Maximum Flows Lecture 4: Jan 19. Network transmission Given a directed graph G A source node s A sink node t Goal: To send as much information from s.
Chapter 27 Maximum Flow Maximum Flow Problem The Ford-Fulkerson method m bipartite matching.
Maximum Flow CSC 172 SPRING 2002 LECTURE 27. Flow Networks Digraph Weights, called capacities, on edges Two distinct veticies Source, “s” (no incoming.
Flows sourcesink s t Flows sourcesink edge-weights = capacities.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
1 Ford-Fulkerson method Ford-Fulkerson(G) f = 0 while( 9 simple path p from s to t in G f ) f := f + f p output f Runs in time O(|f max | |E|) where f.
Maximum Flow Maximum Flow Problem The Ford-Fulkerson method
Maximum Flow Chapter 26.
CS774. Markov Random Field : Theory and Application Lecture 13 Kyomin Jung KAIST Oct
Yangjun Chen 1 Network Flow What is a network? Flow network and flows Ford-Fulkerson method - Residual networks - Augmenting paths - Cuts of flow networks.
Single Source Shortest-Path: The General Case (with negative edges) Bellman-Ford algorithm. Iteratively relax all edges |V|-1 times Running time? O(VE).
CS 473Lecture ?1 CS473-Algorithms I Lecture ? Network Flows Finding Max Flow.
Max Flow – Min Cut Problem. Directed Graph Applications Shortest Path Problem (Shortest path from one point to another) Max Flow problems (Maximum material.
CS 473Lecture ?1 CS473-Algorithms I Lecture ? Network Flows Flow Networks & Flows.
CS223 Advanced Data Structures and Algorithms 1 Maximum Flow Neil Tang 3/30/2010.
Chapter 7 April 28 Network Flow.
Network Flow Analysis of Algorithms.
Chapter 7 May 3 Ford-Fulkerson algorithm Step-by-step walk through of an example Worst-case number of augmentations Edmunds-Karp modification Time complexity.
1 EE5900 Advanced Embedded System For Smart Infrastructure Static Scheduling.
Flow Networks Ching-Chen Huang Hsi-Yue Hsiao. CONTENTS Network flows on directed acyclic graphs Ford-fulkerson Algorithms -Residual networks.
Basic Graph Algorithms Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.
1 Network Flow CSC401 – Analysis of Algorithms Chapter 8 Network Flow Objectives: Flow networks –Flow –Cut Maximum flow –Augmenting path –Maximum flow.
Fall 2003Maximum Flow1 w s v u t z 3/33/3 1/91/9 1/11/1 3/33/3 4/74/7 4/64/6 3/53/5 1/11/1 3/53/5 2/22/2 
CSE 421 Algorithms Richard Anderson Lecture 22 Network Flow.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
Iterative Improvement for Domain-Specific Problems Lecturer: Jing Liu Homepage:
A directed graph G consists of a set V of vertices and a set E of arcs where each arc in E is associated with an ordered pair of vertices from V. V={0,
Maximum Flow Problem Definitions and notations The Ford-Fulkerson method.
Instructor Neelima Gupta Edited by Divya Gaur(39, MCS '09) Thanks to: Bhavya(9), Deepika(10), Deepika Bisht(11) (MCS '09)
The Maximum Network Flow Problem
Preflow Push Algorithm M. Amber Hassaan. Preflow Push Algorithm2 Max Flow Problem Given a graph with “Source” and “Sink” nodes we want to compute:  The.
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
TU/e Algorithms (2IL15) – Lecture 8 1 MAXIMUM FLOW (part II)
Network Flow What is a network? Flow network and flows
Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009
The Maximum Network Flow Problem
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Lecture 22 Network Flow, Part 2
Network Flows and Matching (Supplementary)
Lecture 10 Network flow Max-flow and Min-cut Ford-Fulkerson method
Flow Networks Topics Flow Networks Residual networks
Richard Anderson Lecture 23 Network Flow
Richard Anderson Lecture 23 Network Flow
Richard Anderson Lecture 21 Network Flow
Network Flows and Matching (Supplementary)
Flow Networks General Characteristics Applications
Flow Networks and Bipartite Matching
Algorithms (2IL15) – Lecture 7
Network Flow CSE 373 Data Structures.
EE5900 Advanced Embedded System For Smart Infrastructure
Lecture 21 Network Flow, Part 1
Richard Anderson Lecture 22 Network Flow
Lecture 21 Network Flow, Part 1
Presentation transcript:

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”)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 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:

Applications fluid in pipes current in an electrical circuit traffic on roads data flow in a computer network money flow in an economy etc.

Maximu m 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 c f (u, v) = c(u, v)  f (u, v) ≥ 0 since f (u, v) ≤ c(u, v) Residual network G f = (V, E f ) E f = { (u, v) є V ×V | c f (u, v) > 0 } Example: c(u,v) = 16, f(u,v) = 5  c f (u, v) = 11

s b t a 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 |2 1|1 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| 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 Example (4)

s b t a 1|5 2|4 1 2|2 1|3 s b t a 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 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 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

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 G f ) { c f (p) = min {c f (u,v) | (u,v) є p}; for (each edge (u,v) є p) { f[u][v] = f[u][v] + c f (p) f[v][u] = -f[u][v] } 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|4 2/3 1|1 3 2| ∞ 1 = capTo = prev

Application to Augmenting Path v0 v2 v3 v1 v4 v5 v6 v7 source queue = { v1, v2 } target 1|3 1|4 2/3 1|1 3 2| ∞ 2 2

Application to Augmenting Path v0 v2 v3 v1 v4 v5 v6 v7 source queue = {v2} target 1|3 1|4 2/3 1|1 3 2| ∞ 2 2

Application to Augmenting Path v0 v2 v3 v1 v4 v5 v6 v7 source queue = {v3} target 1|3 1|4 2/3 1|1 3 2| ∞ 2 2 2

Application to Augmenting Path v0 v2 v3 v1 v4 v5 v6 v7 source queue = {v4, v5} target 1|3 1|4 2/3 1|1 3 2| ∞

Application to Augmenting Path v0 v2 v3 v1 v4 v5 v6 v7 source queue = { v5, v6 } target 1|3 1|4 2/3 1|1 3 2| ∞ 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 = { }