COMP3010: Algorithm Theory and Design

Daniel Sutantyo, Department of Computing, Macquarie University

8.0 - Maximum Flow

Prelude

8.0 - Maximum Flow

  • We are going to discuss graph-related algorithms this week:
    • Maximum Flow (Ford Fulkerson Method)
    • Single-Source Shortest Path (Bellman-Ford Algorithm)
    • All-Pairs Shortest Path (Matrix Multiplication Method and Floyd-Warshall Algorithm)

Notation and Terminology

8.0 - Maximum Flow

  • A graph \(G\) is characterised by its set of vertices \(V\) and its set of edges \(E\), and we write \(G = \langle V,E \rangle\)
    • if \(u\) and \(v\) are two vertices in \(V\), we use \((u,v)\) to denote the edge connecting them, and the degree of a vertex is the number of edges connected to it
    • if  the graph is weighted, then we use \(w(u,v)\) to denote the weight of the edge connecting \(u\) and \(v\)
    • if the graph is directed (digraph) then \((u,v)\) is not the same as \((v,u)\), and they can have different weights as well 
  • A graph can also be acyclic, i.e. it contains no cycles
  • A graph is connected if there is a path from each vertex in the graph to any other vertex

8.0 - Maximum Flow

  • A subgraph of \(G = \langle V,E\rangle\) is another graph \(G^\prime = \langle V^\prime,E^\prime\rangle\) formed from a subset of the vertices and edges of \(G\) ​i.e \(E^\prime \subseteq E\) and \(V^\prime \subseteq V\)
  • We also use the term component to refer to a (maximal) connected subgraph 
    • weakly connected vs strongly connected

D

A

B

C

E

F

G

Notation and Terminology

Maximum Flow Problem

8.0 - Maximum Flow

a

s

c

t

b

d

13

source

sink

16

12

4

9

7

20

14

4

  • Suppose that you want to move materials from one location to another location
    • each vertex represents a location
    • each edge represents the connections between two locations

Maximum Flow Problem

8.0 - Maximum Flow

a

s

c

t

b

d

13

source

sink

16

12

4

9

7

20

14

4

  • The source vertex produces material and the sink vertex consumes them
    • every other vertex, can only propagate the material (so if they receive \(n\), then they have to send out \(n\) as well)
  • The weight of each edge signifies the amount of material that can flow through it

Maximum Flow Problem

8.0 - Maximum Flow

a

s

c

t

b

d

13

source

sink

16

12

4

9

7

20

14

4

4

4

4

  • The source vertex produces material and the sink vertex consumes them
    • every other vertex, can only propagate the material (so if they receive \(n\), then they have to send out \(n\) as well)
  • The weight of each edge signifies the amount of material that can flow through it

Maximum Flow Problem

8.0 - Maximum Flow

a

s

c

t

b

d

13

source

sink

13

16

12

4

9

7

20

14

4

  • In maximum-flow problem, we want to compute the greatest rate at which we can move materials from the source vertex to the sink vertex
  • It's not that straightforward: in the above example, the flow out of the source is 29, but you cannot propagate this much material in the network

Maximum Flow Problem

8.0 - Maximum Flow

  • It is a relatively common (and complex) problem, but we are going to study a simplified version 
  • Examples of applications:
    • goods distribution
    • road networks (traffic control), rail networks
    • water distribution, water pipes
    • airline scheduling
  • It is also well-studied:
    • Ford-Fulkerson Algorithm
    • Edmonds-Karp Algorithm
    • Dinic's Algorithm
    • KRT Algorithm

Example

8.0 - Maximum Flow

a

s

c

t

b

d

source

sink

4/13

12/16

12/12

0/4

0/9

0/7

12/20

4/14

4/4

Example

8.0 - Maximum Flow

a

s

c

t

b

d

source

sink

8/13

11/16

12/12

1/4

4/9

7/7

15/20

11/14

4/4

Here is another flow:

a

s

c

t

b

d

source

sink

4/13

12/16

12/12

0/4

0/9

0/7

12/20

4/14

4/4

Definitions

8.0 - Maximum Flow

  • A flow network \(G = \langle V,E \rangle\)  is a directed graph where:
    • each edge \((u,v) \in E\) has a non-negative capacity \(c(u,v)\)
    • if \((u,v) \in E\), then \((v,u)\not\in E\), i.e. if you have an edge going in one direction, then you cannot have another edge in the reverse direction
    • there is a source vertex \(s\) and a sink vertex \(t\)
  • Assumptions (to make our life easier):
    • the graph is connected and each node lies in the path between the source and the sink vertices
    • \(|E| \ge |V|-1\) because each vertex has degree at least 1

Definitions

8.0 - Maximum Flow

  • A flow in \(G\) is a function\(f(u,v) : V \times V \rightarrow \mathbb R\) for \(u,v \in V\) that satisfies the following two properties:
    • capacity constraint: for all \(u,v \in V\),
      • \(0\le f(u,v) \le c(u,v)\), i.e. the flow through an edge cannot exceed the capacity of that edge
    • flow conservation: for all \(u \in V \setminus \{s,t\}\)

      • i.e. except for the source and sink vertices, all inflows to a vertex must equal the outflows from that vertex

\[\sum_{v\in V\setminus \{s,t\}}f(v,u) = \sum_{v\in V\setminus\{s,t\}} f(u,v)\]

Definitions

8.0 - Maximum Flow

this is a flow

this is a different flow

a

s

c

t

b

d

source

sink

8/13

11/16

12/12

1/4

4/9

7/7

15/20

11/14

4/4

a

s

c

t

b

d

source

sink

4/13

12/16

12/12

0/4

0/9

0/7

12/20

4/14

4/4

Definitions

8.0 - Maximum Flow

sink

\(f(s,a) = 11\)

\(f(s,c) = 8\)

\(f(a,b) = 11\)

\(f(b,c) = 3\)

\(f(c,d) = 11\)

\(f(d,b) = 7\)

\(f(b,t) = 15\)

\(f(d,t) = 4\)

the value of this flow is \(19\) 

A flow in \(G\) is a function \(f(u,v) : V \times V \rightarrow \mathbb R\) for \(u,v \in V\)

 \(f(c,a) = 0\) 

a

s

c

t

b

d

source

8/13

11/16

12/12

1/4

4/9

7/7

15/20

11/14

4/4

Definitions

8.0 - Maximum Flow

a

s

c

t

b

d

source

sink

1/13

1/16

1/12

0/4

0/9

0/7

1/20

1/14

1/4

A flow in \(G\) is a function \(f(u,v) : V \times V \rightarrow \mathbb R\) for \(u,v \in V\)

\(f(s,a) = 1\)

\(f(s,c) = 1\)

\(f(a,b) = 1\)

\(f(b,c) = 0\)

\(f(c,d) = 1\)

\(f(d,b) = 0\)

\(f(b,t) = 1\)

\(f(d,t) = 1\)

the value of this flow is \(2\)

\(f(c,a) = 0\)

Definitions

8.0 - Maximum Flow

  • A flow network has many flows, and each flow has its own value \(|f|\), defined by:
    • \[|f| = \sum_{v \in V} f(s,v)\]
    • i.e the total flow out of the source vertex (how much can we send out)
    • our goal is to find a flow of maximum value, the maximum flow
  • It might be worthwhile for you to stop now and think on how to do this using brute force or also why greedy algorithm won't work

Ford-Fulkerson Method

8.0 - Maximum Flow

  • Method:
    • start by setting \(f(u,v)\) to \(0\) for all \(u,v \in V\)
    • while there is an augmenting path \(p\) in the residual network \(G_f\):
      • augment flow \(f\) along \(p\)
  • It is not an algorithm, just a method, because it can be implemented in several ways (Edmond-Karp Algorithm is one implementation)
  • Let's begin with a run of the method, then we'll go back to these definitions

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

0/1

0/3

0/2

0/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

1/1

0/3

1/2

0/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

1/2

1/1

0/3

1/2

1/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

1/1

1/3

2/2

1/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

1/1

1/3

2/2

1/1

  • an augmenting path \(p\) is a simple path from \(s\) to \(t\) in the residual network \(G_f\)

Ingredient #1:

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

0/1

0/3

0/2

0/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

  • a residual network of \(G\) is the edges of \(G\) that still have some capacity left, i.e. those that can take additional flow
  • we use \(G_f\) to denote the residual network of \(G\)

Ingredient #2:

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

  • a residual network of \(G\) is the edges of \(G\) that still have some capacity left, i.e. those that can take additional flow
  • we use \(G_f\) to denote the residual network of \(G\)

Ingredient #2:

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

0/1

2/3

2/2

0/1

  • a residual network of \(G\) is the edges of \(G\) that still have some capacity left, i.e. those that can take additional flow
  • we use \(G_f\) to denote the residual network of \(G\)

Ingredient #2:

1/3

0/2

2/2

0/2

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

0/1

2/3

2/2

0/1

  • a residual network of \(G\) is the edges of \(G\) that still have some capacity left, i.e. those that can take additional flow
  • we use \(G_f\) to denote the residual network of \(G\)

Ingredient #2:

1/3

0/2

2/2

0/2

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

1/1

1/3

2/2

1/1

2/3

0/2

2/2

0/2

  • a residual network of \(G\) is the edges of \(G\) that still have some capacity left, i.e. those that can take additional flow
  • we use \(G_f\) to denote the residual network of \(G\)

Ingredient #2:

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

1/1

1/3

2/2

1/1

2/3

0/2

2/2

0/2

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

Ford-Fulkerson Method

8.0 - Maximum Flow

a

s

c

t

source

sink

1/1

1/3

2/2

1/1

2/3

0/2

2/2

0/2

a

s

c

t

source

sink

2/2

1/1

1/3

2/2

1/1

Residual Network

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

0/1

0/3

0/2

0/1

  • Define the residual capacity \(c_f(u,v)\) by

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

Residual Network

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

0/1

0/3

0/2

0/1

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

a

s

c

t

source

sink

2

1

3

2

1

  • Define the residual capacity \(c_f(u,v)\) by

Residual Network

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

0/1

0/3

0/2

0/1

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

a

s

c

t

source

sink

2

1

3

2

1

0

0

0

0

0

  • Define the residual capacity \(c_f(u,v)\) by

Residual Network

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

1/1

0/3

1/2

0/1

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

a

s

c

t

source

sink

2

0

3

1

1

0

1

1

0

0

  • Define the residual capacity \(c_f(u,v)\) by

Residual Network

8.0 - Maximum Flow

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

a

s

c

t

source

sink

1

0

2

0

1

1

1

2

1

0

  • Define the residual capacity \(c_f(u,v)\) by

Residual Network

8.0 - Maximum Flow

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

a

s

c

t

source

sink

1

2

1

1

1

2

1

  • Define the residual capacity \(c_f(u,v)\) by

Residual Network

8.0 - Maximum Flow

\[c_f(u,v)=\begin{cases} c(u,v)-f(u,v)& \text{if $(u,v)\in E$,} \\ f(v,u) & \text{if $(v,u)\in E$,}\\ 0 & \text{otherwise} \end{cases} \]

  • Define the residual network of \(G\) induced by \(f\) as \(G_f = (V,E_f)\) where
    • \(E_f = \{(u,v) \in V \times V : c_f(u,v) > 0\}\)
    • notice that a residual network is basically a flow network that is allowed to have both \((u,v)\) and \((v,u)\)
  • Define the residual capacity \(c_f(u,v)\) by

Augmenting Path

8.0 - Maximum Flow

  • The residual network tells us how to add flow to the original flow network 
  • The idea is to find an augmenting path in the residual network \(G_f\) and use this augment the flow \(f\) in \(G\)
    • An augmenting path \(p\) is a simple path from \(s\) (source) to \(t\) (sink) in the residual network \(G_f\)

8.0 - Maximum Flow

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

  • The residual network tells us how to add flow to the original flow network 
  • The idea is to find an augmenting path in the residual network \(G_f\) and use this augment the flow \(f\) in \(G\)
    • remember, don't think of a flow as just one path, a flow is a function that tells us how much capacity we are using on each edge 

Augmenting Path

8.0 - Maximum Flow

sink

\(f(s,a) = 1\)

\(f(s,c) = 1\)

 \(f(a,t) = 2\) 

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

 \(f(c,a) = 1\) 

 \(f(c,t) = 0\) 

  • The residual network tells us how to add flow to the original flow network 
  • The idea is to find an augmenting path in the residual network \(G_f\) and use this augment the flow \(f\) in \(G\)
    • remember, don't think of a flow as just one path, a flow is a function that tells us how much capacity we are using on each edge 

Augmenting Path

8.0 - Maximum Flow

sink

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

a

s

c

t

source

sink

1

2

1

1

1

2

1

  • The residual network tells us how to add flow to the original flow network 
  • The idea is to find an augmenting path in the residual network \(G_f\) and use this augment the flow \(f\) in \(G\)
    • remember, don't think of a flow as just one path, a flow is a function that tells us how much capacity we are using on each edge 

Augmenting Path

8.0 - Maximum Flow

sink

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

  • An augmenting path \(p\) is a simple path from \(s\) (source) to \(t\) (sink) in the residual network \(G_f\)

a

s

c

t

source

sink

1

2

1

1

1

2

1

Augmenting Path

8.0 - Maximum Flow

sink

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

  • An augmenting path \(p\) is a simple path from \(s\) (source) to \(t\) (sink) in the residual network \(G_f\)

a

s

c

t

source

sink

1

2

1

1

1

2

1

Augmenting Path

8.0 - Maximum Flow

sink

a

s

c

t

source

sink

1/2

1/1

1/3

2/2

0/1

a

s

c

t

source

sink

1

2

1

1

1

2

1

  • Once we found an augmenting path, we simply use it to augment the flow we have so far in \(G\)
  • Note that an augmenting path is basically a flow in \(G_f\)

Augmenting Path

8.0 - Maximum Flow

  • If \(f\) is a flow in \(G\) and \(f_p\) is a flow in \(G_f\), then define the function \((f \uparrow f_p) : V \times V \rightarrow \mathbb R\) as

\[(f \uparrow f_p)(u,v) = \begin{cases} f(u,v) + f_p(u,v) - f_p(v,u) &\text{if $(u,v) \in E$,} \\ 0 & \text{otherwise.}\end{cases}\]

  • The augmenting path is a flow \(f_p\) where the value \(|f_p|\) of the flow is the minimum residual capacity in the path

a

s

c

t

source

sink

1

2

1

1

1

2

1

Augmenting Path

8.0 - Maximum Flow

  • we call the function \((f \uparrow f_p)\) as the augmentation of flow \(f\) by \(f_p\)
  • basically we increase the flow on \((u,v)\) by \(f_p(u,v)\), but we decrease it by \(f_p(v,u)\) (we call this a cancellation)
  • the function \((f\uparrow f_p)(u,v)\) is a flow in \(G\) with value
     

\(|f\uparrow f_p| = |f| + |f_p| > |f|\)

(Lemma 26.1, Corollary 26.3 of CLRS, page 717, 720)

  • If \(f\) is a flow in \(G\) and \(f_p\) is a flow in \(G_f\), then define the function \((f \uparrow f_p) : V \times V \rightarrow \mathbb R\) as

\[(f \uparrow f_p)(u,v) = \begin{cases} f(u,v) + f_p(u,v) - f_p(v,u) &\text{if $(u,v) \in E$,} \\ 0 & \text{otherwise.}\end{cases}\]

Augmenting Path

8.0 - Maximum Flow

  • we call the function \((f \uparrow f_p)\) as the augmentation of flow \(f\) by \(f_p\)
  • basically we increase the flow on \((u,v)\) by \(f_p(u,v)\), but we decrease it by \(f_p(v,u)\) (we call this a cancellation)
  • the function \((f\uparrow f_p)(u,v)\) is a flow in \(G\) with value
     

\(|f\uparrow f_p| = |f| + |f_p| > |f|\)

(Lemma 26.1, Corollary 26.3 of CLRS, page 717, 720)

  • If \(f\) is a flow in \(G\) and \(f_p\) is a flow in \(G_f\), then define the function \((f \uparrow f_p) : V \times V \rightarrow \mathbb R\) as

\[(f \uparrow f_p)(u,v) = \begin{cases} f(u,v) + f_p(u,v) - f_p(v,u) &\text{if $(u,v) \in E$,} \\ 0 & \text{otherwise.}\end{cases}\]

Augmenting Path

8.0 - Maximum Flow

a

s

c

t

source

sink

0/2

0/1

0/3

0/2

0/1

Augmenting Path

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

Augmenting Path

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

a

s

c

t

source

sink

1

1

2

1

2

2

\(G\)

\(G_f\)

Augmenting Path

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

0/1

2/3

2/2

0/1

a

s

c

t

source

sink

1

1

2

1

2

2

\(G\)

\(G_f\)

Augmenting Path

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

1/1

1/3

2/2

1/1

a

s

c

t

source

sink

1

1

2

1

2

2

\(G\)

\(G_f\)

Augmenting Path

8.0 - Maximum Flow

a

s

c

t

source

sink

2/2

1/1

1/3

2/2

1/1

a

s

c

t

source

sink

2

1

1

1

2

2

\(G\)

\(G_f\)

Augmenting Path

Ford-Fulkerson Method

8.0 - Maximum Flow

  • You don't have to worry about the proof of correctness of the algorithm (it is definitely not simple)
    • It is called the Max-Flow Min-Cut Theorem (Theorem 26.6 in CLRS, pg 723)
  • What you need to understand is how the method works
  • Ford-Fulkerson Method:
    • start by setting \(f(u,v)\) to \(0\) for all \(u,v \in V\)
    • while there is an augmenting path \(p\) in the residual network \(G_f\):
      • augment flow \(f\) along \(p\)

Ford-Fulkerson Method

8.0 - Maximum Flow

  • Ford-Fulkerson Method for a flow network \(G = \langle V,E\rangle\):
    • set \(f(u,v) = 0\) for all \((u,v)\) in \(E\)
    • create the residual network \(G_f\)
    • while there exists a simple path \(p\) from  source to sink in \(G_f\):
      • set \(c_f(p) = \min\{c_f(u,v) : (u,v) \in p\}\)
      • for each edge \((u,v)\) in \(p\):
        • if \((u,v) \in E\):
          • \((u,v) = (u,v) + c_f(p)\) 
        • else
          • \((v,u) = (v,u) - c_f(p)\)

Ford-Fulkerson Method

8.0 - Maximum Flow

  • One more time, this is just a method, not an algorithm, because we never discussed how to actually find the augmenting path
  • Edmond-Karp Algorithm is an implementation that uses BFS to find the augmenting path, and it can be proven that the runtime is \(O(VE^2)\)
    • proof of correctness is in CLRS, pg. 727 onwards, but again, this is beyond the scope of this unit
  • Dinic's blocking flow algorithm is \(O(V^2E)\) (it also uses BFS)

Example

8.0 - Maximum Flow

a

s

c

t

b

d

source

sink

0/13

0/16

0/12

0/4

0/9

0/7

0/20

0/14

0/4

  • We'll do this in the workshop, but you can also give it a try first