cs 4102: algorithmsjh2jf/courses/fall2019/cs...review: all-pairs shortest path 2 thus...

69
CS 4102: Algorithms Lecture 22: Network Flow David Wu Fall 2019

Upload: others

Post on 23-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

CS 4102: AlgorithmsLecture 22: Network Flow

David WuFall 2019

Page 2: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Review: All-Pairs Shortest Path

2

Thus far: single-source shortest path algorithms (Dijkstra, Bellman-Ford)

All-pairs shortest-paths: find shortest path between every pair of nodes

10

2

11

95

8

3

7

3

1

8

12

9

Page 3: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Review: All-Pairs Shortest Path

NaΓ―vely: Run single-source shortest paths algorithm for each node 𝑠 (to compute shortest path from 𝑠 to every other node in the graph)

β€’ If edge weights are all non-negative, can use Dijkstra (running time 𝑂 𝑉 𝐸 log 𝑉

β€’ If edge weights can be negative, can use Bellman-Ford (running time 𝑂 𝑉 ( 𝐸

When 𝐸 = Ξ© 𝑉 ( , both of these algorithms are 𝑂 𝑉 + log 𝑉 or 𝑂 𝑉 ,

3

Page 4: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Floyd-Warshall All-Pairs Shortest Paths

4

Finds all-pairs shortest paths in Θ( 𝑉 +) using dynamic programmingAlso works if graph has negative-weight edges

Same observation as before: Every subpath of a shortest path is itself a shortest path (optimal substructure)β€’ Namely if shortest path from 𝑖 to 𝑗 goes through π‘˜, then the 𝑖 β†’ π‘˜ and π‘˜ β†’ 𝑗 subpaths must themselves be a shortest path

Two possibilities for node π‘˜:

Shortest path from 𝑖 to 𝑗 includes π‘˜

Shortest path from 𝑖 to 𝑗 excludes π‘˜OR

Short(𝑖, 𝑗, π‘˜ βˆ’ 1)

Short(𝑖, π‘˜, π‘˜ βˆ’ 1) + Short(π‘˜, 𝑗, π‘˜ βˆ’ 1)

𝑖𝑗

π‘˜

𝑖

π‘—π‘˜

Short 𝑖, 𝑗, π‘˜ = weight of shortest path from 𝑖 β†’ 𝑗 using nodes 1,… , π‘˜ as intermediate hops

Page 5: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Floyd-Warshall All-Pairs Shortest Paths

5

Finds all-pairs shortest paths in Θ( 𝑉 +) using dynamic programmingAlso works if graph has negative-weight edges

Same observation as before: Every subpath of a shortest path is itself a shortest path (optimal substructure)β€’ Namely if shortest path from 𝑖 to 𝑗 goes through π‘˜, then the 𝑖 β†’ π‘˜ and π‘˜ β†’ 𝑗 subpaths must themselves be a shortest path

Short(𝑖, 𝑗, π‘˜ βˆ’ 1)Short(𝑖, π‘˜, π‘˜ βˆ’ 1) + Short(π‘˜, 𝑗, π‘˜ βˆ’ 1)Short 𝑖, 𝑗, π‘˜ = min

Short 𝑖, 𝑗, π‘˜ = weight of shortest path from 𝑖 β†’ 𝑗 using nodes 1,… , π‘˜ as intermediate hops

Page 6: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Floyd-Warshall All-Pairs Shortest Paths

6

allocate short[n][n][n] (initialized to ∞)for (i, j) in E:

short[i][j][0] = w[i][j]

for i = 1,...,n:

short[i][i][0] = 0

for k = 1,...,n:

for i = 1,...,n:

for j = 1,...,n:

short[i][j][k] = min(short[i][k][k-1] + short[k][j][k-1], short[i][j][k-1])

π’Œ = 𝟎: shortest path cannot use any intermediate nodes (must be direct path)

π’Œ = 𝟎: shortest path from node to itself is always 0

Short(𝑖, 𝑗, π‘˜ βˆ’ 1)Short(𝑖, π‘˜, π‘˜ βˆ’ 1) + Short(π‘˜, 𝑗, π‘˜ βˆ’ 1)Short 𝑖, 𝑗, π‘˜ = min

Page 7: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Floyd-Warshall All-Pairs Shortest Paths

7

allocate short[n][n] (initialized to ∞)for (i, j) in E:

short[i][j] = w[i][j]

for i = 1,...,n:

short[i][i] = 0

for k = 1,...,n:

for i = 1,...,n:

for j = 1,...,n:

short[i][j] = min(short[i][k] + short[k][j], short[i][j])

π’Œ = 𝟎: shortest path cannot use any intermediate nodes (must be direct path)

π’Œ = 𝟎: shortest path from node to itself is always 0

Observation: short[i][j][k] only depends on values for short[][][k – 1], so we can just use a single two-dimensional array

Short(𝑖, 𝑗, π‘˜ βˆ’ 1)Short(𝑖, π‘˜, π‘˜ βˆ’ 1) + Short(π‘˜, 𝑗, π‘˜ βˆ’ 1)Short 𝑖, 𝑗, π‘˜ = min

In this case, the initialization step is constructing the adjacency matrix of the graph

(this step is not needed if graph already represented in this form!)

Page 8: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Floyd-Warshall All-Pairs Shortest Paths

8

allocate short[n][n] (initialized to ∞)for (i, j) in E:

short[i][j] = w[i][j]

for i = 1,...,n:

short[i][i] = 0

for k = 1,...,n:

for i = 1,...,n:

for j = 1,...,n:

short[i][j] = min(short[i][k] + short[k][j], short[i][j])

π’Œ = 𝟎: shortest path cannot use any intermediate nodes (must be direct path)

π’Œ = 𝟎: shortest path from node to itself is always 0

Very simple implementation!

Running time: 𝑂 𝑛+ = 𝑂 𝑉 +

Short(𝑖, 𝑗, π‘˜ βˆ’ 1)Short(𝑖, π‘˜, π‘˜ βˆ’ 1) + Short(π‘˜, 𝑗, π‘˜ βˆ’ 1)Short 𝑖, 𝑗, π‘˜ = min

Page 9: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Today’s Keywords

GraphsNetwork flowFord-FulkersonEdmonds-KarpMax-Flow Min-Cut Theorem

9

CLRS Readings: Chapter 25, 26

Page 10: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Homework

HW7 due today, 11pmβ€’ Graph algorithmsβ€’ Written (use LaTeX!) – Submit both zip and pdf (two separate attachments)!

HW10B due today, 11pmβ€’ No late submissions allowed (no exceptions)

HW8 out today, due Thursday, November 21, 11pmβ€’ Programming assignment (Python or Java)β€’ Graph algorithms

10

Page 11: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Network Flow

11Railway map of Western USSR, 1955

Question: What is the maximum throughput of the

railroad network?

Page 12: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Flow Networks

Graph 𝐺 = (𝑉, 𝐸)Source node 𝑠 ∈ 𝑉Sink node 𝑑 ∈ 𝑉Edge capacities 𝑐 𝑒 ∈ ℝK

Max flow intuition: If 𝑠 is a faucet, 𝑑 is a drain, and 𝑠 connects to 𝑑 through a network of pipes 𝐸 with capacities 𝑐(𝑒), what is the maximum amount of water which can flow from the faucet to the drain?

12

3

3

3

2

𝑠𝑑

1

2

1 3 2

2

3

Page 13: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Assignment of values 𝑓 𝑒 to edgesβ€’ β€œAmount of water going through that pipe”

Capacity constraintβ€’ 𝑓 𝑒 ≀ 𝑐(𝑒)β€’ β€œFlow cannot exceed capacity”

Flow constraintβ€’ βˆ€π‘£ ∈ 𝑉 βˆ’ {𝑠, 𝑑}, inRlow 𝑣 = outRlow(𝑣)β€’ inRlow 𝑣 = βˆ‘V∈W 𝑓(π‘₯, 𝑣)β€’ outRlow 𝑣 = βˆ‘V∈W 𝑓(𝑣, π‘₯)β€’ Water going in must match water coming out

Flow of 𝐺: |𝑓| = outRlow 𝑠 βˆ’ inRlow(𝑠)β€’ Net outflow of 𝑠

13

flow / capacity

3 in this example

Network Flow

1/3

1/3

2/3

2/2

𝑠𝑑

0/1

2/2

1/1

2/31/2

1/2

2/3

Page 14: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Maximum Flow Problem

Of all valid flows through the graph, find the one that maximizes:

𝑓 = outRlow 𝑠 βˆ’ inRlow(𝑠)

14

3

3

3

2

𝑠𝑑

1

2

1

32

2

30/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/3

Page 15: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Greedy Approach

15

30

20

𝑠 𝑑

10 20

10

Greedy choice: saturate highest capacity path first

Page 16: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Greedy Approach

16

30

20

𝑠 𝑑

10 20

10

Greedy choice: saturate highest capacity path first

Page 17: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Greedy Approach

17

20/30

20/20

𝑠 𝑑

10 20/20

10

Greedy choice: saturate highest capacity path first

Flow: 20

Page 18: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Greedy Approach

18

10/30

20/20

𝑠 𝑑

10/10 20/20

10/10

Greedy choice: saturate highest capacity path first

Maximum Flow: 30

Observe: highest capacity path is not saturated in optimal solution

Page 19: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

19

1/3

1/3

2/3

2/2

𝑠𝑑

0/1

2/2

1/1

2/31/2

1/2

2/3

Flow 𝑓 in 𝐺

Given a flow 𝑓 in graph 𝐺, the residual graph 𝐺Z models additional flow that is possibleβ€’ Forward edge for each edge in 𝐺 with weight set to remaining capacity 𝑐 𝑒 βˆ’ 𝑓(𝑒)

β€’ Models additional flow that can be sent along the edge

Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

Page 20: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

20

1/3

1/3

2/3

2/2

𝑠𝑑

0/1

2/2

1/1

2/31/2

1/2

2/3

Flow 𝑓 in 𝐺 Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

1

21

2

1

2

212

1

Given a flow 𝑓 in graph 𝐺, the residual graph 𝐺Z models additional flow that is possibleβ€’ Forward edge for each edge in 𝐺 with weight set to remaining capacity 𝑐 𝑒 βˆ’ 𝑓(𝑒)

β€’ Models additional flow that can be sent along the edgeβ€’ Backward edge by flipping each edge 𝑒 in 𝐺 with weight set to flow 𝑓(𝑒)

β€’ Models amount of flow that can be removed from the edge

Page 21: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

21

1/3

1/3

2/3

2/2

𝑠𝑑

0/1

2/2

1/1

2/31/2

1/2

2/3

Flow 𝑓 in 𝐺

Consider a path from 𝑠 β†’ 𝑑 in 𝐺Z using only edges with positive (non-zero) weightConsider the minimum-weight edge 𝑒 along the path: we can increase the flow by 𝑀(𝑒)β€’ Send 𝑀(𝑒) flow along all forward edges (these have at least 𝑀(𝑒) capacity)

Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

1

21

2

1

2

212

1

Page 22: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

22

1/3

1/3

2/3

2/2

𝑠𝑑

0/1

2/2

1/1

2/31/2

1/2

2/3

Flow 𝑓 in 𝐺

Consider a path from 𝑠 β†’ 𝑑 in 𝐺Z using only edges with positive (non-zero) weightConsider the minimum-weight edge 𝑒 along the path: we can increase the flow by 𝑀(𝑒)β€’ Send 𝑀(𝑒)flow along all forward edges (these have at least 𝑀(𝑒) capacity)β€’ Remove 𝑀(𝑒)flow along all backward edges (these contain at least 𝑀(𝑒) units of flow)

Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

1

21

2

1

2

212

1

Page 23: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

23

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

2/31/2

1/2

3/3

Flow 𝑓 in 𝐺 Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

1

21

2

1

2

212

1

Observe: Flow has increased by 𝑀(𝑒)

Consider a path from 𝑠 β†’ 𝑑 in 𝐺Z using only edges with positive (non-zero) weightConsider the minimum-weight edge 𝑒 along the path: we can increase the flow by 𝑀(𝑒)β€’ Send 𝑀(𝑒)flow along all forward edges (these have at least 𝑀(𝑒) capacity)β€’ Remove 𝑀(𝑒)flow along all backward edges (these contain at least 𝑀(𝑒) units of flow)

Page 24: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

24

Consider a path from 𝑠 β†’ 𝑑 in 𝐺Z using only edges with positive (non-zero) weightConsider the minimum-weight edge 𝑒 along the path: we can increase the flow by 𝑀(𝑒)β€’ Send 𝑀(𝑒) flow along all forward edges (these have at least 𝑀(𝑒) capacity)β€’ Remove 𝑀(𝑒) flow along all backward edges (these contain at least 𝑀(𝑒) units of flow)

Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

1

21

2

1

2

212

1

Observe: Flow has increased by 𝑀(𝑒)

Why does this respect flow constraints?β€’ Incoming edge to a node always

corresponds to increased flow to the node (more incoming flow from forward edge or less outgoing flow from backward edge)

β€’ Outgoing edge to a node always corresponds to decreased flow to the node

Page 25: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Residual Graphs

25

Consider a path from 𝑠 β†’ 𝑑 in 𝐺Z using only edges with positive (non-zero) weightConsider the minimum-weight edge 𝑒 along the path: we can increase the flow by 𝑀(𝑒)β€’ Send 𝑀(𝑒) flow along all forward edges (these have at least 𝑀(𝑒) capacity)β€’ Remove 𝑀(𝑒) flow along all backward edges (these contain at least 𝑀(𝑒) units of flow)

Residual graph 𝐺Z

𝑠𝑑

21

1

112

11

1

21

2

1

2

212

1

Observe: Flow has increased by 𝑀(𝑒)

Why does this respect flow constraints?β€’ Incoming edge to a node always

corresponds to increased flow to the node (more incoming flow from forward edge or less outgoing flow from backward edge)

β€’ Outgoing edge to a node always corresponds to decreased flow to the node

Capacity constraints satisfied by construction of the residual network

Page 26: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Algorithm

Define an augmenting path to be an 𝑠 β†’ 𝑑 path in the residual graph 𝐺Z (using edges of non-zero weight)

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path 𝑝 in 𝐺Z:

β€’ Let 𝑐 = min]∈^

𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

26

Ford-Fulkerson approach: take any augmenting path(will revisit this later)

Page 27: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

27

0/3

0/3

0/3

0/2

𝑠𝑑

0/1

0/2

0/1

0/30/2

0/2

0/3

Initially: 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸

3

3

3

𝑠𝑑

1

2

1

32

2

Residual graph 𝐺Z

3

2

Page 28: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

28

0/3

0/3

0/3

0/2

𝑠𝑑

0/1

0/2

0/1

0/30/2

0/2

0/33

3

𝑠𝑑

1

2

1

32

2

Residual graph 𝐺Z

Increase flow by 1 unit

33

2

Page 29: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

29

0/3

1/3

1/3

0/2

𝑠𝑑

0/1

1/2

1/1

0/30/2

1/2

0/33

3

𝑠𝑑

1

2

1

32

2

Residual graph 𝐺Z

Increase flow by 1 unit

33

2

Page 30: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

30

0/3

1/3

1/3

0/2

𝑠𝑑

0/1

1/2

1/1

0/30/2

1/2

0/32

2

𝑠𝑑

1

1

32

1

11

1

1

1

Residual graph 𝐺Z

33

2

Page 31: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

31

0/3

1/3

1/3

0/2

𝑠𝑑

0/1

1/2

1/1

0/30/2

1/2

0/32

2

𝑠𝑑

1

1

32

1

11

1

1

1

Residual graph 𝐺Z

Increase flow by 1 unit

33

2

Page 32: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

32

0/3

2/3

1/3

0/2

𝑠𝑑

0/1

2/2

1/1

0/30/2

1/2

1/32

2

𝑠𝑑

1

1

32

1

11

1

1

1

Residual graph 𝐺Z

Increase flow by 1 unit

33

2

Page 33: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

33

0/3

2/3

1/3

0/2

𝑠𝑑

0/1

2/2

1/1

0/30/2

1/2

1/31

2

𝑠𝑑

1

32

1

221

1

1

2 1

Residual graph 𝐺Z

Increase flow by 1 unit

3

2

Page 34: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

34

0/3

2/3

1/3

0/2

𝑠𝑑

0/1

2/2

1/1

0/30/2

1/2

1/3

Residual graph 𝐺Z

1

2

𝑠𝑑

1

32

1

221

1

1

2 13

2

Page 35: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

35

0/3

2/3

1/3

1/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

1/2

2/3

Residual graph 𝐺Z

1

2

𝑠𝑑

1

32

1

221

1

1

2 1

Increase flow by 1 unit

3

2

Page 36: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

36

0/3

2/3

1/3

1/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

1/2

2/3

Residual graph 𝐺Z

1

2

𝑠𝑑

1

32

1

121

1

1

2 2

Increase flow by 1 unit

3

11

Page 37: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

37

0/3

2/3

1/3

1/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

1/2

2/3

Residual graph 𝐺Z

1

2

𝑠𝑑

1

32

1

121

1

1

2 23

11

Page 38: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

38

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/3

Residual graph 𝐺Z

1

2

𝑠𝑑

1

32

1

121

1

1

2 2

Increase flow by 1 unit

3

11

Page 39: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Example

39

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/3

Residual graph 𝐺Z

No more augmenting paths

1

1

𝑠𝑑

1

3212

1

2

2

2 23

2

Maximum flow: 4

Page 40: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Running Time

Define an augmenting path to be an 𝑠 β†’ 𝑑 path in the residual graph 𝐺Z (using edges of non-zero weight)

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path 𝑝 in 𝐺Z:

β€’ Let 𝑐 = min]∈^

𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

40

Initialization: 𝑂 𝐸

Page 41: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Running Time

Define an augmenting path to be an 𝑠 β†’ 𝑑 path in the residual graph 𝐺Z (using edges of non-zero weight)

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path 𝑝 in 𝐺Z:

β€’ Let 𝑐 = min]∈^

𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

41

Initialization: 𝑂 𝐸Construct residual network: 𝑂 𝐸

Page 42: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Running Time

Define an augmenting path to be an 𝑠 β†’ 𝑑 path in the residual graph 𝐺Z (using edges of non-zero weight)

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path 𝑝 in 𝐺Z:

β€’ Let 𝑐 = min]∈^

𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

42

Initialization: 𝑂 𝐸Construct residual network: 𝑂 𝐸Finding augmenting path in residual network: 𝑂 𝐸 using BFS/DFS

We only care about nodes reachable from the source 𝑠 (so the number of nodes

that are β€œrelevant” is at most 𝐸 )

Page 43: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Running Time

Define an augmenting path to be an 𝑠 β†’ 𝑑 path in the residual graph 𝐺Z (using edges of non-zero weight)

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path 𝑝 in 𝐺Z:

β€’ Let 𝑐 = min]∈^

𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

43

Initialization: 𝑂 𝐸Construct residual network: 𝑂 𝐸Finding augmenting path in residual network: 𝑂 𝐸 using BFS/DFS

How many iterations are needed?β€’ For integer-valued capacities, min-weight of each augmenting path is 1, so

number of iterations is bounded by π‘“βˆ— , where π‘“βˆ— is max-flow in 𝐺‒ For rational-valued capacities, can scale to make capacities integerβ€’ For irrational-valued capacities, algorithm may never terminate!

β€’ Extra credit: Construct a graph where this happens

Page 44: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Ford-Fulkerson Running Time

Define an augmenting path to be an 𝑠 β†’ 𝑑 path in the residual graph 𝐺Z (using edges of non-zero weight)

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path 𝑝 in 𝐺Z:

β€’ Let 𝑐 = min]∈^

𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

44

Initialization: 𝑂 𝐸Construct residual network: 𝑂 𝐸Finding augmenting path in residual network: 𝑂 𝐸 using BFS/DFS

For graphs with integer capacities, running time of Ford-Fulkerson is

𝑂 π‘“βˆ— β‹… 𝐸Highly undesirable if π‘“βˆ— ≫ |𝐸| (e.g., graph is small, but capacities are β‰ˆ 2+()

As described, algorithm is not polynomial-time!

Page 45: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

45

0/1

0/100

𝑠 𝑑

0/100 0/100

0/100

1

100

𝑠 𝑑

100 100

100

Page 46: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

46

0/1

0/100

𝑠 𝑑

0/100 0/100

0/100

1

100

𝑠 𝑑

100 100

100

Increase flow by 1 unit

Page 47: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

47

1/1

1/100

𝑠 𝑑

0/100 1/100

0/100

1

100

𝑠 𝑑

100 100

100

Increase flow by 1 unit

Page 48: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

48

1/1

1/100

𝑠 𝑑

0/100 1/100

0/100

1

99

𝑠 𝑑

100 99

100

11

Page 49: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

49

1/1

1/100

𝑠 𝑑

0/100 1/100

0/100

1

99

𝑠 𝑑

100 99

100

11

Increase flow by 1 unit

Page 50: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

50

0/1

1/100

𝑠 𝑑

1/100 1/100

1/100

1

99

𝑠 𝑑

100 99

100

11

Increase flow by 1 unit

Page 51: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

51

0/1

1/100

𝑠 𝑑

1/100 1/100

1/100

1

99

𝑠 𝑑

99 99

99

11

1

1

Page 52: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Worst-Case Ford-Fulkerson

52

0/1

1/100

𝑠 𝑑

1/100 1/100

1/100

1

99

𝑠 𝑑

99 99

99

11

1

1

Observation: each iteration increases flow by 1 unitTotal number of iterations: π‘“βˆ— = 200

Page 53: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Can We Avoid this?

Edmonds-Karp Algorithm: choose augmenting path with fewest hopsRunning time: Θ min 𝐸 π‘“βˆ— , 𝑉 𝐸 ( = 𝑂 𝑉 𝐸 (

53

Ford-Fulkerson max-flow algorithm:β€’ Initialize 𝑓 𝑒 = 0 for all 𝑒 ∈ 𝐸‒ Construct the residual network 𝐺Zβ€’ While there is an augmenting path in 𝐺Z, let 𝑝 be the path with fewest hops:β€’ Let 𝑐 = min

]∈^𝑐Z(𝑒) (𝑐Z(𝑒) is the weight of edge 𝑒 in the residual network 𝐺Z)

β€’ Add 𝑐 units of flow to 𝐺 based on the augmenting path 𝑝‒ Update the residual network 𝐺Z for the updated flow

How to find this? Use breadth-first search (BFS)!

Edmonds-Karp = Ford-Fulkerson using BFS to find augmenting path

Proof: See CLRS (Chapter 26.2)

Page 54: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Correctness of Ford-Fulkerson

Consider cuts which separate 𝑠 and 𝑑‒ Let 𝑠 ∈ 𝑆, 𝑑 ∈ 𝑇, such that 𝑉 = 𝑆 βˆͺ 𝑇

Cost 𝑆, 𝑇 of cut 𝑆, 𝑇 : sum of the capacities of edges from 𝑆 to 𝑇

54

3

3

3

2

𝑠𝑑

1

2

1

32

2

3

𝑆 𝑇𝑆, 𝑇 = 5

Page 55: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow / Min-CutClaim: Maximum flow in a flow network 𝐺 always upper-bounded by the cost any cut that separates 𝑠 and 𝑑Proof: β€œConservation of flow”

β€’ All flow from 𝑠 must eventually get to 𝑑‒ To get from 𝑠 to 𝑑, all flow must cross the cut somewhere

Conclusion: Max-flow in 𝐺 is at most the cost of the min-cut in 𝐺‒ max

Z𝑓 ≀ min

j,k𝑆, 𝑇

55

3

3

3

2

𝑠𝑑

1

2

1

32

2

3

𝑆 𝑇

Page 56: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem

Let 𝑓 be a flow in a graph 𝐺

56

𝑓 is a maximum flow in 𝐺

there are no augmenting paths in the

residual graph 𝐺Z

there exists a cut (𝑆, 𝑇) of 𝐺 where

𝑓 = ‖𝑆, 𝑇‖

Statements are equivalent!

Implications:β€’ Correctness of Ford-Fulkerson: Ford-Fulkerson terminates when there are no more

augmenting paths in the residual graph 𝐺Z, which means that 𝑓 is a maximum flowβ€’ Max-flow min-cut duality: the maximum flow in a network coincides with the minimum cut

of the graph (maxZ

𝑓 = minj,k

𝑆, 𝑇 )

β€’ Finding either the minimum cut or the maximum flow yields solution to the otherβ€’ Special case of more general principle (duality in linear programming)

Page 57: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Duality Example

57

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/31

1

𝑠𝑑

1

3212

1

2

2

2 23

2

Flow graph Residual graph

no more augmenting paths

Max flow: 4

𝑆

𝑇

Page 58: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Duality Example

58

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/31

1

𝑠𝑑

1

3212

1

2

2

2 23

2

Flow graph Residual graph

no more augmenting paths𝑆

𝑇

Max flow: 4Min cut: 4

When there are no more augmenting paths in the graph, there is a cut whose cost matches the flow

Page 59: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

Let 𝑓 be a flow in a graph 𝐺

59

𝑓 is a maximum flow in 𝐺

there are no augmenting paths in the

residual graph 𝐺Z

there exists a cut (𝑆, 𝑇) of 𝐺 where

𝑓 = ‖𝑆, 𝑇‖

Proof:β€’ Suppose 𝑓 is a max flow in 𝐺 and there is an augmenting path in 𝐺Zβ€’ If there is an augmenting path in 𝐺Z, then we can send additional units of flow though

the network along the augmenting pathβ€’ This contradicts optimality of 𝑓

Page 60: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

Let 𝑓 be a flow in a graph 𝐺

60

𝑓 is a maximum flow in 𝐺

there are no augmenting paths in the

residual graph 𝐺Z

there exists a cut (𝑆, 𝑇) of 𝐺 where

𝑓 = ‖𝑆, 𝑇‖

Proof:β€’ Take any flow 𝑓mβ€’ Consider the cut 𝑆, 𝑇 of 𝐺; then, 𝑓m ≀ 𝑆, 𝑇 = 𝑓‒ Thus, 𝑓m ≀ 𝑓 , so 𝑓 must be a maximum flow

Page 61: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

Let 𝑓 be a flow in a graph 𝐺

61

𝑓 is a maximum flow in 𝐺

there are no augmenting paths in the

residual graph 𝐺Z

there exists a cut (𝑆, 𝑇) of 𝐺 where

𝑓 = ‖𝑆, 𝑇‖

Page 62: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

62

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/31

1

𝑠𝑑

1

3212

1

2

2

2 23

2

no more augmenting paths𝑆

𝑇Flow graph Residual graph

No augmenting paths means there is no path from 𝑠 to 𝑑 in 𝐺Zβ€’ Let 𝑆 be set of nodes reachable from 𝑠 in 𝐺Zβ€’ Let 𝑇 = 𝑉 βˆ’ 𝑆

Page 63: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

63

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/31

1

𝑠𝑑

1

3212

1

2

2

2 23

2

no more augmenting paths𝑆

𝑇Claim: 𝑆, 𝑇 = 𝑓‒ Total flow 𝑓 is amount of outgoing flow from 𝑆 to 𝑇 minus the amount of incoming flow from 𝑇 to 𝑆

Page 64: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

64

0/3

2/3

2/3

2/2

𝑠

𝑒𝑣

𝑑

0/1

2/2

0/1

0/30/2

2/2

2/31

1

𝑠

𝑒𝑣

𝑑

1

3212

1

2

2

2 23

2

no more augmenting paths𝑆

𝑇Claim: 𝑆, 𝑇 = 𝑓‒ Total flow 𝑓 is amount of outgoing flow from 𝑆 to 𝑇 minus the amount of incoming flow from 𝑇 to 𝑆‒ Outgoing flow: Consider edge 𝑒, 𝑣 where 𝑒 ∈ 𝑆 and 𝑣 ∈ 𝑇

β€’ Then, 𝑓 𝑒, 𝑣 = 𝑐 𝑒, 𝑣 . Otherwise, there is a forward edge 𝑒, 𝑣 with positive weight in 𝐺Z and 𝑣 ∈ 𝑆

Page 65: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

65

0/3

2/3

2/3

2/2

𝑠

π‘₯

𝑦

𝑑

0/1

2/2

0/1

0/30/2

2/2

2/31

1

𝑠

π‘₯

𝑦

𝑑

1

3212

1

2

2

2 23

2

no more augmenting paths𝑆

𝑇Claim: 𝑆, 𝑇 = 𝑓‒ Total flow 𝑓 is amount of outgoing flow from 𝑆 to 𝑇 minus the amount of incoming flow from 𝑇 to 𝑆‒ Outgoing flow: Consider edge 𝑒, 𝑣 where 𝑒 ∈ 𝑆 and 𝑣 ∈ 𝑇

β€’ Then, 𝑓 𝑒, 𝑣 = 𝑐 𝑒, 𝑣 . Otherwise, there is a forward edge 𝑒, 𝑣 with positive weight in 𝐺Z and 𝑣 ∈ 𝑆‒ Incoming flow: Consider edge 𝑦, π‘₯ where 𝑦 ∈ 𝑇 and π‘₯ ∈ 𝑆

β€’ Then, 𝑓 𝑦, π‘₯ = 0. Otherwise, there is a backward edge π‘₯, 𝑦 with positive weight in 𝐺Z and 𝑦 ∈ 𝑆

Page 66: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem Proof

66

0/3

2/3

2/3

2/2

𝑠𝑑

0/1

2/2

0/1

0/30/2

2/2

2/3𝑆

𝑇Claim: 𝑆, 𝑇 = 𝑓‒ Total flow 𝑓 is amount of outgoing flow from 𝑆 to 𝑇 minus the amount of incoming flow from 𝑇 to 𝑆‒ Outgoing flow: Consider edge 𝑒, 𝑣 where 𝑒 ∈ 𝑆 and 𝑣 ∈ 𝑇

β€’ Then, 𝑓 𝑒, 𝑣 = 𝑐 𝑒, 𝑣 . Otherwise, there is a forward edge 𝑒, 𝑣 with positive weight in 𝐺Z and 𝑣 ∈ 𝑆‒ Incoming flow: Consider edge 𝑦, π‘₯ where 𝑦 ∈ 𝑇 and π‘₯ ∈ 𝑆

β€’ Then, 𝑓 𝑦, π‘₯ = 0. Otherwise, there is a backward edge π‘₯, 𝑦 with positive weight in 𝐺Z and 𝑦 ∈ 𝑆

𝑓 = pq∈j,r∈k

𝑓 𝑒, 𝑣 βˆ’ ps∈k,V∈j

𝑓 𝑦, π‘₯

= 𝑆, 𝑇

= pq∈j,r∈k

𝑐 𝑒, 𝑣

Page 67: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Max-Flow Min-Cut Theorem

Let 𝑓 be a flow in a graph 𝐺

67

𝑓 is a maximum flow in 𝐺

there are no augmenting paths in the

residual graph 𝐺Z

there exists a cut (𝑆, 𝑇) of 𝐺 where

𝑓 = ‖𝑆, 𝑇‖

Statements are equivalent!

Implications:β€’ Correctness of Ford-Fulkerson: Ford-Fulkerson terminates when there are no more

augmenting paths in the residual graph 𝐺Z, which means that 𝑓 is a maximum flowβ€’ Max-flow min-cut duality: the maximum flow in a network coincides with the minimum cut

of the graph (maxZ

𝑓 = minj,k

𝑆, 𝑇 )

Page 68: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Other Max Flow Algorithms

Ford-Fulkersonβ€’ Θ 𝐸 π‘“βˆ—

Edmonds-Karp (Ford-Fulkerson using BFS to choose augmenting path)β€’ Θ( 𝐸 ( 𝑉 )

Push-Relabel (Tarjan)β€’ Θ( 𝐸 𝑉 ()

Faster Push-Relabel (also Tarjan)β€’ Θ( 𝑉 +)

68

Page 69: CS 4102: Algorithmsjh2jf/courses/fall2019/cs...Review: All-Pairs Shortest Path 2 Thus far:single-sourceshortest path algorithms (Dijkstra, Bellman-Ford) All-pairs shortest-paths: find

Minimum-Cost Maximum-Flow Problem

Not all paths are created equal!

69

3

3

3

2

𝑠𝑑

1

2

1

32

2

3

2 5

211

3 4

7

1 2

1

A cost is associated with each unit of flow sent along an edgeGoal: Maximize flow while minimizing cost

Much harder problem!Can solve using linear programming