Mini Max-Flow Unit Part 1: Introduction, Ford-Fulkerson Lucca - - PowerPoint PPT Presentation

mini max flow unit
SMART_READER_LITE
LIVE PREVIEW

Mini Max-Flow Unit Part 1: Introduction, Ford-Fulkerson Lucca - - PowerPoint PPT Presentation

Mini Max-Flow Unit Part 1: Introduction, Ford-Fulkerson Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/31 University of British Columbia Announcements A5 is going to be released tonight (probably) It does not contain any flow


slide-1
SLIDE 1

Mini Max-Flow Unit

Part 1: Introduction, Ford-Fulkerson

Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/31

University of British Columbia

slide-2
SLIDE 2

Announcements

  • A5 is going to be released tonight (probably)
  • It does not contain any flow problems.
  • Presentations are due Friday.
  • We will not be marking your projects before we see your presentations, please assume that

we have not been exposed to them beforehand (this way your fellow students can also watch them).

1

slide-3
SLIDE 3

Maximum Flow: Problem Statement (1)

Input:

  • A directed graph G = (V , E)
  • A source node s ∈ V and sink node t ∈ V .
  • For each edge e ∈ E, a capacity ce.

s t A B

3 2 3 2 5

2

slide-4
SLIDE 4

Maximum Flow: Problem Statement (2)

A flow is an assignment of values fe to each edge meeting the following constraints:

  • Capacity constraints: For every e ∈ E, fe ≤ ce
  • Conservation constraints: For every non-terminal node v ∈ V \ {s, t}, the total flow

leaving v = the total flow entering v, i.e.:

  • vu∈E,u∈V

fvu =

  • uv∈E,u∈V

fuv Output: A flow maximizing the (net) flow leaving s, i.e. maximizing  

  • sv∈E,v∈V

fsv   +  

  • vs∈E,v∈V

fvs   Analogy: If edges are (capacitated) pipes, then the flow is the rate of water (or oil, etc.) flow.

3

slide-5
SLIDE 5

Maximum Flow: Flow Example

s t A B

3 2 3 2 5 2 1 3

4

slide-6
SLIDE 6

Maximum Flow: Notation

Instead of labelling both the flow paths and the capacities, we will instead label how much of the capacity is used. The label x/y indicates that x units of flow are going across an edge of capacity y.

s t A B

3/3 2/2 3/3 2/2 1/5

5

slide-7
SLIDE 7

Maximum Flow: Near-Solution

An augmenting path is a path from s to t (an s − t path) for which all the edges have unused capacity. Let’s try the following greedy algorithm:

1

while there is an augmenting path:

2

pick any augmenting path P

3

increase the flow on edges of P

6

slide-8
SLIDE 8

Maximum Flow: Near-Solution - Failure

This greedy algorithm fails. Consider the following graph and flow of 3:

s t A B

3/3 0/2 3/3 0/2 3/5 3

There is no way to augment this. However, a flow of 4 is possible.

7

slide-9
SLIDE 9

Maximum Flow: Residual Graph (1)

If we re-consider this locally maximum graph, where are all the edges across which we can push flow?

s t A B

3/3 0/2 3/3 0/2 3/5

Key observation: We can push flow backwards across an edge. Use this to create a residual graph. Intuition: Water can push back against water. This is similar to finding the net force in physics.

8

slide-10
SLIDE 10

Maximum Flow: Residual Graph (2)

If we also draw all the edges across which we can push, we get the following graph:

s t A B

3/3 0/2 0/2 3/3 3/5 0/3 2/2 0/3 2/2 2/5

9

slide-11
SLIDE 11

Maximum Flow: Residual Graph (3)

This is called the residual graph of G under flow f . It is denoted as G f . It has the following properties:

  • V (G f ) = V (G).
  • For each edge e ∈ G f , cf

e is the residual capacity.

  • For each edge e = uv ∈ E(G), we have the following edges in G f :
  • The forward edge: edge uv with capacity cf

uv = ce − fe.

  • The backward edge: edge vu with capacity cf

vu = fe.

We typically only draw or care about the edges with residual capacity > 0.

10

slide-12
SLIDE 12

Maximum Flow: Ford-Fulkerson Algorithm

We modify the previous algorithm slightly (and give more details):

1

while there is an augmenting path in the residual graph:

2

pick an augmenting path P

3

# augment the flow

4

push = min(capacity[e] for e in P)

5

for e in P:

6

if e is a forward edge:

7

increase the flow on e by push

8

else:

9

decrease the flow on the corresponding forward edge by push

10

update the residual graph

11

slide-13
SLIDE 13

Maximum Flow: Ford-Fulkerson Example (1)

s t A B

0/3 0/2 0/2 0/3 0/5 3/3 2/2 3/3 2/2 5/5

12

slide-14
SLIDE 14

Maximum Flow: Ford-Fulkerson Example (2)

s t A B

0/3 0/2 0/2 0/3 0/5 3/3 2/2 3/3 2/2 5/5

Found an augmenting path of 3.

13

slide-15
SLIDE 15

Maximum Flow: Ford-Fulkerson Example (3)

s t A B

3/3 0/2 0/2 3/3 3/5 0/3 2/2 0/3 2/2 2/5

14

slide-16
SLIDE 16

Maximum Flow: Ford-Fulkerson Example (3)

s t A B

3/3 0/2 0/2 3/3 3/5 0/3 2/2 0/3 2/2 2/5

Found an augmenting path of 2.

15

slide-17
SLIDE 17

Maximum Flow: Ford-Fulkerson Example (4)

s t A B

3/3 2/2 2/2 3/3 1/5 0/3 0/2 0/3 2/2 4/5

Total (maximum) flow is 5.

16

slide-18
SLIDE 18

Maximum Flow: Ford-Fulkerson Runtime

Ford-Fulkerson only necessarily terminates if the capacities are all rational. Let’s assume they’re integers:

  • Every augmentation increases the flow by ≥ 1 unit.
  • Overall, need O(|f |) iterations.
  • Each augmentation takes O(|E|) time to find (use DFS).

The overall time complexity is then O(|E||f |) (this is not necessarily polynomial).

17

slide-19
SLIDE 19

Maximum Flow: Complexity

Note:

  • If we use BFS to find the augmenting paths, we get Edmonds-Karp algorithm, with

runtime O

  • min(|E||f |, |V ||E|2)
  • This runtime has been improved several times. Very approachable improvements include

Dinic’s algorithm and Push-Relabel.

  • The best theoretical complexity is O(|V ||E|) (Orlin, 2013). This is also the fastest

possible in the general case.

18

slide-20
SLIDE 20

Maximum Flow: Edmonds-Karp (BFS part)

Here we have some pseudocode for Edmonds-Karp:

1

def EdmondsKarp_BFS(graph, s, t): # find an augmenting path and update the flow

2

initialize previous[v] to null (for all vertices v)

3

initialize flow_to[v] to inf (for all vertices v)

4

q = new queue containing s

5

while q is not empty:

6

u = dequeue(q)

7

for edge e from u to v, where v is a neighbour of u:

8

if flow[e] < capacity[e] and previous[v] is null:

9

previous[v] = u

10

flow_to[v] = min(flow_to[u], capacity[e] - flow[e])

11

enqueue(v)

12

if previous[t] is null: # no more augmenting paths

13

return 0

14

# iterate over edges by following previous[], starting from t and ending at null

15

for edge e in augmenting path:

16

flow[e] += flow_to[t] # increase flow on the edge in the path

17

flow[opposite edge of e] -= flow_to[t] # decrease flow on the opposite edge

18

return flow_to[t]

19

slide-21
SLIDE 21

Maximum Flow: Edmonds-Karp (Main part)

20

def EdmondsKarp(graph, s, t):

21

# add backward edges

22

for edge e in graph from u to v:

23

  • p = edge from v to u, with capacity 0

24

add op to graph

25

# keep finding augmenting paths

26

max_flow = 0

27

current_flow = EdmondsKarp_BFS(graph, s, t)

28

while current_flow > 0:

29

max_flow += current_flow

30

current_flow = EdmondsKarp_BFS(graph, s, t)

31

return max_flow

20

slide-22
SLIDE 22

Minimum Cut: The Cold War

We want to find the cheapest way to disconnect the Soviet Union from the rest of Europe, using the least force. We need study s − t cuts.

21

slide-23
SLIDE 23

Minimum Cut: Problem Statement

An s − t cut partitions the nodes of a graph into sets S, T (i.e. V = S ∪ T and S ∩ T = ∅), such that s ∈ S and t ∈ T.

  • The capacity of a cut is the sum of the edge capacities in E ∩ (S × T), i.e. the sum of the

edges fromS to T.

s t A B

3 2 3 2 5

Figure 1: A cut of capacity 6.

22

slide-24
SLIDE 24

Minimum Cut: Example

What’s the minimum cut in this graph?

s t A B

3 2 3 2 5

23

slide-25
SLIDE 25

Minimum Cut: Example

What’s the minimum cut in this graph?

s t A B

3 2 3 2 5

It’s 5!

23

slide-26
SLIDE 26

Maximum Flow and Minimum Cut (1)

Theorem 1: Any cut has capacity ≥ the value of any flow. Proof: If we remove all the edges in a cut, the maximum flow across the resulting graph is 0. Note: This is called weak duality. Theorem 2: The maximum s − t flow is equal to the capacity of the minimum s − t cut.

  • Proof 1: By linear programming duality.
  • Proof 2:
  • Run Ford-Fulkerson (or similar) to find the maximum flow f .
  • Let S be the set of nodes still reachable from s in G f , and T = V \ S.
  • Claim: the capacity of the cut (S, T) is |f |.
  • All the edges from S to T are saturated (otherwise there would have been a path along a

forward edge to the node).

  • All of the edges from T to S have zero flow (otherwise there would have been a path along a

backward edge to the node).

24

slide-27
SLIDE 27

Reductions - Warm-up

Input: A capacitated network with sources s1, s2, . . . , sp and sinks t1, t2, . . . , tq. Output: The maximum total flow from all sources to all sinks.

s1 s2 s3 t1 t2

4 5 2 1 3 45 7 3 5 2 3 4

25

slide-28
SLIDE 28

Reductions - Warm-up Solution

Create a super source s and super sink t. Add edges s → si and ti → t with infinite capacity. Instead of infinite capacity, we could also use a capacity that is the sum of all capacities in the

  • riginal graph.

s1 s2 s3 t1 t2

4 5 2 1 3 45 7 3 5 2 3 4

s t

∞ ∞ ∞ ∞ ∞

26

slide-29
SLIDE 29

Reductions

Why is max-flow useful?

27

slide-30
SLIDE 30

Reductions

Why is max-flow useful? Lots of problems can be reduce to max-flow.

27

slide-31
SLIDE 31

Reductions - Problem 1

Input: A directed graph G and notes s, t ∈ V (G). Output: The edge connectivity of s, t, i.e. the maximum number of edge-disjoint paths from s to t.

28

slide-32
SLIDE 32

Reductions - Problem 1 Solution

Set the capacity of every edge to 1. Find the maximum s − t flow. To recover the paths, you can greedily choose them in O(|E|) time.

29

slide-33
SLIDE 33

Reductions - Problem 2

Input: A capacitated network G, and notes s and t. In addition to edge capacities, also include vertex capacities. Output: The maximum flow through this network, also obeying vertex capacities.

30

slide-34
SLIDE 34

Reductions - Problem 2 Solution

Take the directed graph G, and split each vertex into an “out-vertex” and an “in-vertex”. For a vertex v, create one new vertex vin that has all the edges that were entering v, and a new vertex vout that has all the edges that were exiting v. Connect these two new vertices with an edge with capacity equal to the capacity of v.

v 5

vout vin

5

Solve the maximum flow problem on this new graph.

31

slide-35
SLIDE 35

Reductions - Problem 3

Input: A directed graph G and notes s, t ∈ V (G). Output: The vertex-connectivity of s, t, i.e. the maximum number of vertex-disjoint paths from s to t.

32

slide-36
SLIDE 36

Reductions - Problem 3 Solution

Set the capacity of every edge to 1, and set the capacity of each vertex to 1. Find the maximum s − t flow using the solution to the previous problem.

33

slide-37
SLIDE 37

Note on Max-Flow/Min-Cut

  • You will probably not need to tweak an algorithm that solves Max-Flow
  • This means that, in theory, you don’t even need to understand how it works, as long as you

know how to use it

  • Almost every problem consists of figuring out how to build the correct graph to run

max-flow on

  • The UBC Code Archive has a few different implementation for network flow

34