CPSC 490: Problem Solving in Computer Science Assignment 2 is due - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science Assignment 2 is due - - PowerPoint PPT Presentation

Lecture 5: Network flow, Maximum flow algorithms, Max-flow min-cut Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-01-17 University of British Columbia CPSC 490: Problem Solving in Computer Science Assignment 2 is due


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

Lecture 5: Network flow, Maximum flow algorithms, Max-flow min-cut

Henry Xia, Brandon Zhang

based on CPSC 490 slides from 2014-2018

2019-01-17

University of British Columbia

slide-2
SLIDE 2

Announcements

  • Solutions to Assignment 1 have been posted on the course website.
  • Assignment 2 is due Tuesday at noon.

1

slide-3
SLIDE 3

Cold War

  • You are given a map of the railroads in Europe.
  • Each railroad has a cost to destroy.
  • What’s the cheapest way of blowing up railroads to disconnect the Soviet Union?

2

slide-4
SLIDE 4

Max flow

Input:

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

3

slide-5
SLIDE 5

Max flow

A flow is an assignment of values fe to each edge e satisfying:

  • Capacity constraints: fe ≤ ce for all e ∈ E
  • Conservation of flow: For all v ∈ V except s and t, total flow entering v = total flow

exiting v Output: A valid flow maximizing the total flow leaving s (= total flow entering t).

4

slide-6
SLIDE 6

Max flow

Formally: given variables fe (e ∈ E), we want to maximize |f| = total flow leaving s subject to          fe ≤ ce for all e ∈ E; fe ≥ 0 for all e ∈ E; ∑

p=u→v

fp = ∑

q=v→w

fq for all v ∈ V, v / ∈ {s, t}.

5

slide-7
SLIDE 7

Max flow

The blue lines represent flow paths.

6

slide-8
SLIDE 8

Max flow

We’ll label edges by x/y, indicating x units of flow on an edge with capacity y.

7

slide-9
SLIDE 9

Max flow – Solution attempt

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

An augmenting path is an s-t path for which all its edges have room for more flow.

8

slide-10
SLIDE 10

Does greedy work?

Greedy fails! A greedy in our example network could produce a flow of 3.

9

slide-11
SLIDE 11

Does greedy work?

Greedy fails! A greedy in our example network could produce a flow of 3. But, we can fix it...

9

slide-12
SLIDE 12

Residual graph

  • We picked a “bad” augmenting path above, which blocked us from pushing more flow.
  • The idea of residual networks lets us “undo” these bad choices.

10

slide-13
SLIDE 13

Residual graph

A residual graph Gf = (V, Ef) is a flow graph (depending on the current flow f) that satisfies the following:

  • V(Gf) = V(G)
  • For each edge e = u → v ∈ E(G), we consider the following edges:
  • Forward edge: edge u → v with capacity cf(u, v) = ce − fe
  • Backward edge: edge v → u with capacity cf(v, u) = fe
  • cf(u, v) is the residual capacity of an edge.
  • The edges of Gf are all the edges described above with residual capacity > 0.

11

slide-14
SLIDE 14

Residual graph

  • Theorem. A flow f in a flow graph G is maximum if and only if there is no augmenting

path in the residual graph Gf.

12

slide-15
SLIDE 15

Residual graph

  • Theorem. A flow f in a flow graph G is maximum if and only if there is no augmenting

path in the residual graph Gf.

12

slide-16
SLIDE 16

Ford-Fulkerson algorithm

Greedy!

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

13

slide-17
SLIDE 17

Ford-Fulkerson example

14

slide-18
SLIDE 18

Ford-Fulkerson example

14

slide-19
SLIDE 19

Ford-Fulkerson example

14

slide-20
SLIDE 20

Ford-Fulkerson example

14

slide-21
SLIDE 21

Ford-Fulkerson example

14

slide-22
SLIDE 22

Runtime

What’s the runtime of Ford-Fulkerson?

  • O(|E|) time to find an augmenting path (say, using DFS).
  • If the capacities are integers, every augmenting path increases the flow by ≥ 1 unit.
  • Thus, need O(|f|) iterations.

Time complexity: O(|E||f|). Bad!

15

slide-23
SLIDE 23

Runtime

What’s the runtime of Ford-Fulkerson?

  • O(|E|) time to find an augmenting path (say, using DFS).
  • If the capacities are integers, every augmenting path increases the flow by ≥ 1 unit.
  • Thus, need O(|f|) iterations.

Time complexity: O(|E||f|). Bad!

15

slide-24
SLIDE 24

Runtime

Note:

  • If the capacities aren’t rational, Ford-Fulkerson is not guaranteed to terminate.
  • Not even guaranteed to converge to the maximum flow.
  • If we use BFS to find augmenting paths, we get O(min(|E||f|, |V||E|2)) runtime

(Edmonds-Karp algorithm).

  • Dinic’s algorithm uses some extra tricks to improve runtime to O(|V|2|E|)
  • Fast in practice, and better bounds on bipartite/unit capacity graphs.
  • Best theoretical complexity to compute max flow: O(|V||E|) (Orlin, 2013).

16

slide-25
SLIDE 25

Edmonds-Karp Algorithm (BFS part)

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]

17

slide-26
SLIDE 26

Edmonds-Karp Algorithm

1

def EdmondsKarp(graph, s, t):

2

# add backward edges

3

for edge e in graph from u to v:

4

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

5

add op to graph

6

# keep finding augmenting paths

7

max_flow = 0

8

current_flow = EdmondsKarp_BFS(graph, s, t)

9

while current_flow > 0:

10

max_flow += current_flow

11

current_flow = EdmondsKarp_BFS(graph, s, t)

12

return max_flow

18

slide-27
SLIDE 27

Dinic’s Algorithm (DFS and BFS parts)

1

def Dinic_BFS(s, t): # build the layer graph

2

run BFS from s, but only considering edges e where flow[e] < capacity[e]

3

store dist(s,v) = number of edges on path from s to v, for each vertex v

4

return true if we can reach t from s, else false

5

def Dinic_DFS(u, t, f): # try to push f flow from u to t

6

if u == t: return f # because we can push f flow from t to t for any f

7

pushed = 0

8

for edge e from u to v, (start iteration from current_edge[u]):

9

if pushed >= f: break # check whether we still have flow to push

10

# we only want to try pushing flow if v is in the layer after u

11

if flow[e] < capacity[e] and dist(s,v) == dist(s,u) + 1:

12

current_push = Dinic_DFS(v, t, min(f - pushed, capacity[e] - flow[e]))

13

flow[e] += current_push

14

flow[opposite edge of e] -= current_push

15

pushed += current_push

16

return pushed

19

slide-28
SLIDE 28

Dinic’s Algorithm

1

def Dinic(graph, s, t):

2

# add backward edges

3

for edge e in graph from u to v:

4

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

5

add op to graph

6

# find max flow

7

max_flow = 0

8

while Dinic_BFS(s, t): # while we can find a layer graph from s to t

9

# try to push infinite flow from s to t, and record how much is successfully pushed

10

max_flow += DFS(s, t, inf)

11

return max_flow

20

slide-29
SLIDE 29

Back to the Cold War...

We want the cheapest way to disconnect the Soviet Union from the rest of Europe. To solve this problem, we’ll study cuts.

21

slide-30
SLIDE 30

Cuts

A cut divides the nodes in a graph into partitions (S, T) (i.e. V = S ∪ T and S ∩ T = ∅).

  • In the context of network flow, we usually consider s-t cuts, where s ∈ S and t ∈ T.
  • The capacity of the cut is the sum of capacities of the edges going from S to T. (Edges

from T to S don’t contribute.)

22

slide-31
SLIDE 31

Cuts

23

slide-32
SLIDE 32

Cuts

23

slide-33
SLIDE 33

Max-flow min-cut

  • Theorem. The maximum flow from s to t in a graph is equal to the capacity of the

minimum s-t cut. Proof 1. Linear programming duality! (Not very illuminating...) Proof 2.

  • Run Ford-Fulkerson to find a maximum flow f.
  • Let S

nodes reachable from s in the residual graph Gf, T V S.

  • Claim: the capacity of the cut S T is equal to f .
  • To see this, observe that all edges from S to T are saturated, and all edges from T to S

have zero flow.

  • Easy to see that the size of any flow

the capacity of any cut, so we’re done.

24

slide-34
SLIDE 34

Max-flow min-cut

  • Theorem. The maximum flow from s to t in a graph is equal to the capacity of the

minimum s-t cut. Proof 1. Linear programming duality! (Not very illuminating...) Proof 2.

  • Run Ford-Fulkerson to find a maximum flow f.
  • Let S

nodes reachable from s in the residual graph Gf, T V S.

  • Claim: the capacity of the cut S T is equal to f .
  • To see this, observe that all edges from S to T are saturated, and all edges from T to S

have zero flow.

  • Easy to see that the size of any flow

the capacity of any cut, so we’re done.

24

slide-35
SLIDE 35

Max-flow min-cut

  • Theorem. The maximum flow from s to t in a graph is equal to the capacity of the

minimum s-t cut. Proof 1. Linear programming duality! (Not very illuminating...) Proof 2.

  • Run Ford-Fulkerson to find a maximum flow f.
  • Let S = nodes reachable from s in the residual graph Gf, T = V \ S.
  • Claim: the capacity of the cut (S, T) is equal to |f|.
  • To see this, observe that all edges from S to T are saturated, and all edges from T to S

have zero flow.

  • Easy to see that the size of any flow ≤ the capacity of any cut, so we’re done.

24

slide-36
SLIDE 36

Finding the min cut

How to find the min cut? Using Proof 2, we just need to find the nodes reachable from s in the final residual graph. The edges being cut are all edges exiting S.

25

slide-37
SLIDE 37

Problem 1

Input: a flow network with sources s1, s2, . . . , sn, and sinks t1, t2, . . . , tm. Output: the maximum flow.

26

slide-38
SLIDE 38

Problem 1 – Solution

Add a super source s and super sink t! Add edges s → si with infinite capacity, ti → t with infinite capacity.

27

slide-39
SLIDE 39

Problem 2 – Connectivity

Input: a directed graph G, and nodes s, t ∈ V(G). Output: the edge connectivity of s, t: the maximum number of edge-disjoint paths from s to t.

28

slide-40
SLIDE 40

Problem 2 – Solution

Set the capacity of every edge to 1. Find the maximum s-t flow.

29