cse 421: introduction to algorithms · cse 421: introduction to algorithms network flow paul beame....

62
1 CSE 421: Introduction to Algorithms Network Flow Paul Beame

Upload: others

Post on 06-Oct-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

1

CSE 421: Introduction to

Algorithms

Network Flow

Paul Beame

Page 2: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

2

Bipartite Matching

� Given: A bipartite graph G=(V,E)

� M⊆⊆⊆⊆E is a matching in G iff no two edges

in M share a vertex

� Goal: Find a matching M in G of maximum possible size

Page 3: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

3

Bipartite Matching

Page 4: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

4

Bipartite Matching

Page 5: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

5

� How much stuff can flow from s to t?

The Network Flow Problem

5

6

7

4

3

4

1

5

3

7

64

s

a

b

c

x

y

z

t

Page 6: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

6

Bipartite matching as a special case of flow

1

1

1

1

1

1

1

1

1

1

1

s

a

b

c

x

y

z

t

Page 7: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

7

Net Flow: Formal Definition

Given:

A digraph G = (V,E)

Two vertices s,t in V(source & sink)

A capacity c(u,v) ≥ 0for each (u,v) ∈ E(and c(u,v) = 0 for all

non-edges (u,v))

Find:A flow function f: E → R s.t., for all u,v:

� 0 ≤≤≤≤ f(u,v) ≤ c(u,v)[Capacity Constraint]

� if u ≠ s,t, i.e. fout(u)=f in(u)[Flow Conservation]

Maximizing total flow ν(f) = fout(s)

Notation:out

e (v,w ) Ef (v) f(v, w)

= ∈= ∑in

e (u,v) Ef (v) f(u,v)

= ∈= ∑

Page 8: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

8

fin(u)=f(s,u)=2=f(u,t)=fout(u)

Example: A Flow Function

s u t2/2 2/3

flow/capacity, not .66...

Page 9: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

9

� Not shown: f(u,v) if = 0

� Note: max flow ≥ 4 sincef is a flow function, with ν(f) = 4

Example: A Flow Function

4/5

6

7

3/4

1/3

4

1

5

3/3

7

1/61/4

s

a

b

c

x

y

z

t

Page 10: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

10

Max Flow via a Greedy Alg?

While there is an s → t path in GPick such a path, pFind c, the min capacity of any edge in pCount c towards the flow value

Subtract c from all capacities on pDelete edges of capacity 0

� This does NOT always find a max flow:

s1 a

t

b

2

1

3

2

If pick s →b →a →t

first, flow stuck at 2.But flow 3 possible.

Page 11: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

11

A Brief History of Flow

n = # of verticesm= # of edgesU = Max capacity

Source: Goldberg & Rao,

FOCS ‘97

bound

2012 Orlin + King et al. O(nm)

Page 12: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

12

Greed Revisited:Residual Graph & Augmenting Path

2/2

1

2/3s

a

b

t

1

2/2

2

1

1s

a

b

t

1

2

2

2/2

1/1

1/3s

a

b

t

1/1

1+1/2

2

1

1s

a

b

t

1

2

2

Residual Graph

Page 13: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

13

Greed Revisited:An Augmenting Path

2

2s

a

b

t

2

1

2/2

1/1

1/3s

a

b

t

1/1

1+1/2

New Residual Graph1

1

Page 14: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

14

Residual Capacity

� The residual capacity (w.r.t. f) of (u,v) is cf(u,v) = c(u,v) - f(u,v) if f(u,v)≤c(u,v) and cf(u,v)=f(v,u) if f(v,u)>0

� e.g. cf(s,b)=7; cf(a,x) = 1; cf(x,a) = 3

4/5

6

7

3/4

1/3

4

1

5

3/3

7

1/61/4

s

a

b

c

x

y

z

t

Page 15: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

15

Residual Graph& Augmenting Paths

� The residual graph (w.r.t. f) is the graph Gf = (V,Ef), where

Ef = { (u,v) | cf(u,v) > 0 }� Two kinds of edges

� Forward edges

� f(u,v)<c(u,v) so cf(u,v)=c(u,v)-f(u,v)>0

� Backward edges

� f(u,v)>0 so cf(v,u) ≥ -f(v,u)=f(u,v)>0

� An augmenting path (w.r.t. f) is a simple s →→→→ t path in Gf.

Page 16: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

16

A Residual Network

4

3

1

1

1

6

7

1

2

4

1

5

3

7

53

s

a

b

c

x

y

z

t

1

4/5

6

7

3/4

1/3

4

1

5

3/3

7

1/61/4

s

a

b

c

x

y

z

t

Page 17: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

17

An Augmenting Path

4

3

1

1

1

6

7

1

2

4

1

5

3

7

53

s

a

b

c

x

y

z

t

1

4/5

6

7

3/4

1/3

4

1

5

3/3

7

1/61/4

s

a

b

c

x

y

z

t

Page 18: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

18

Augmenting A Flow

augment(f,P)

cP←min(u,v)∈∈∈∈P cf(u,v) “bottleneck(P)”

for each e∈∈∈∈P

if e is a forward edge then

increase f(e) by cP

else (e is a backward edge)

decrease f(e) by cP

endif

endfor

return(f)

Page 19: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

19

Augmenting A Flow

4

3

1

1

1

6

7

1

2

4

1

5

3

7

53

s

a

b

c

x

y

z

t

1

4/5

1/6

7

3/4

1/3

4

1

1/5

3/3

1/7

1/64

s

a

b

c

x

y

z

t

4/5

6

7

3/4

1/3

4

1

5

3/3

7

1/61/4

s

a

b

c

x

y

z

t

Page 20: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

20

Claim 7.1

If Gf has an augmenting path P, then the function f’=augment(f,P) is a legal flow.

Proof:

� f’ and f differ only on the edges of P so only need to consider such edges (u,v)

Page 21: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

21

Proof of Claim 7.1

� If (u,v) is a forward edge then f’(u,v)=f(u,v)+cP ≤≤≤≤ f(u,v)+cf(u,v)

= f(u,v)+c(u,v)-f(u,v) =c(u,v)

� If (u,v) is a backward edge then f and f’differ on flow along (v,u) instead of (u,v)f’(v,u)=f(v,u)-cP ≥≥≥≥ f(v,u)-cf(u,v)

= f(v,u)-f(v,u)=0

� Other conditions like flow conservation still met

Page 22: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

22

Ford-Fulkerson Method

Start with f=0 for every edge

While Gf has an augmenting path, augment

� Questions:

� Does it halt?

� Does it find a maximum flow?

� How fast?

Page 23: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

23

Observations about Ford-FulkersonAlgorithm

� At every stage the capacities and flow values are always integers (if they start that way)

� The flow value νννν(f’)=νννν(f)+cP>νννν(f) for f’=augment(f,P)� Since edges of residual capacity 0 do not appear

in the residual graph

� Let C=ΣΣΣΣ(s,u)∈∈∈∈E c(s,u)

� νννν(f)≤C

� F-F does at most C rounds of augmentation since flows are integers and increase by at least 1 per step

Page 24: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

24

Running Time of Ford-Fulkerson

� For f=0, Gf=G

� Finding an augmenting path in Gf is graph search O(n+m)=O(m) time

� Augmenting and updating Gf is O(n)time

� Total O(mC) time

� Does it find a maximum flow?� Need to show that for every flow f that isn’t

maximum Gf contains an s-t-path

Page 25: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

25

Cuts

� A partition (A,B) of V is an s-t-cut if

� s∈∈∈∈A, t∈∈∈∈B

� Capacity of cut (A,B) is∈∈

=∑u Av B

c(A,B) c(u,v)

5

6

7

4

3

4

1

5

3

7

64

s

a

b

c

x

y

z

t

{s}c=18

V-{t}c=16

{s,b,c}c=15

5

6

7

3

s

a

b

c

x

y

z

t

{s,x}c=21

Page 26: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

26

Convenient Definition

� fout(A)=ΣΣΣΣv∈∈∈∈A, w∉∉∉∉A f (v,w)

� fin(A)=ΣΣΣΣv∈∈∈∈A, u∉∉∉∉A f (u,v)

Page 27: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

27

Claims 7.6 and 7.8

� For any flow f and any cut (A,B),

� the net flow across the cut equals the total

flow, i.e., νννν(f) = fout(A)-fin(A), and

� the net flow across the cut cannot exceed

the capacity of the cut,

i.e. fout(A)-fin(A) ≤ c(A,B)

� Corollary :

Max flow ≤ Min cut1

s

t

1

1

1

1

Cut Cap = 3

Net Flow = 1

Cut Cap = 2

Net Flow = 1

Page 28: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

28

Proof of Claim 7.6

� Consider a set A with s∈∈∈∈A, t∉∉∉∉A

� fout(A)-fin(A) =ΣΣΣΣv∈∈∈∈A, w∉∉∉∉A f (v,w)-ΣΣΣΣv∈∈∈∈A, u∉∉∉∉A f (u,v)

� We can add flow values for edges with both endpoints in A to both sums and they would cancel out so

� fout(A)-fin(A)= ΣΣΣΣv∈∈∈∈A, w∈∈∈∈V f (v,w)-ΣΣΣΣv∈∈∈∈A, u∈∈∈∈V f (u,v)

= ΣΣΣΣv∈∈∈∈A (ΣΣΣΣw∈∈∈∈V f (v,w) - ΣΣΣΣu∈∈∈∈V f (u,v))

=ΣΣΣΣv∈∈∈∈A fout (v) - fin(v)

=fout(s)-fin(s)

since all other vertices have fout(v)=fin(v)

� νννν(f) = fout(s) and fin(s)=0

Page 29: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

29

Proof of Claim 7.8

� νννν(f)=fout(A)-fin(A) ≤ fout(A)

= ΣΣΣΣv∈∈∈∈A, w∉∉∉∉A f (v,w)

≤ ΣΣΣΣv∈∈∈∈A, w∉∉∉∉A c(v,w)

≤ ΣΣΣΣv∈∈∈∈A, w∈∈∈∈B c(v,w)

=c(A,B)

Page 30: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

30

Max Flow / Min Cut Theorem

Claim 7.9 For any flow f, if Gf has no augmenting path then there is some s-t-cut (A,B) such that νννν(f)=c(A,B) (proof on next slide)

� We know by Claims 7.6 & 7.8 that any flow f’ satisfies νννν(f’) ≤ c(A,B) and we know that F-F runs for finite time until it finds a flow f satisfying conditions of Claim 7.9

� Therefore by 7.9 for any flow f’, νννν(f’) ≤νννν(f)

� Corollary (1) F-F computes a maximum flow in G

(2) For any graph G, the value νννν(f) of a maximum

flow = minimum capacity c(A,B) of any s-t-cut in G

Page 31: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

31

Claim 7.9

Let A = { u | ∃ an path in Gf from s to u }

B = V - A; s ∈∈∈∈ A, t ∈∈∈∈ B

This is true for every edge crossing the cut, i.e.

and fin(A)=0 so νννν(f)=fout(A)-fin(A)=c(A,B)

s t

A B

u v

∈ ∈∈ ∈

= = =∑ ∑out

u A u Av B v B

f A f u,v c u,v c A,B( ) ( ) ( ) ( )

w

saturatedf(u,v)=c(u,v)

no flowf(w,u)=0x

Page 32: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

32

Flow Integrality Theorem

If all capacities are integers

� The max flow has an integer value

� Ford-Fulkerson method finds a max flow in

which f(u,v) is an integer for all edges (u,v)

ts

0.5/1

0.5/10.5/1

0.5/1

1/1

Page 33: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

33

Corollaries & Facts

� If Ford-Fulkerson terminates, then it’s found a max flow.

� It will terminate if c(e) integer or rational

(but may not if they’re irrational).

� However, may take exponential time, even with integer capacities:

sc a

t

b

c

c

1

c

c = 109, say

Page 34: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

34

Bipartite matching as a special case of flow

1

1

1

1

1

1

1

1

1

1

1

s

a

b

c

x

y

z

t

Integer flows implies each flow is just a subset of the edges

Therefore flow corresponds to a matching

O(mC)=O(nm) running time

Page 35: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

35

Capacity-scaling algorithm

� General idea:

� Choose augmenting paths P with ‘large’

capacity cP

� Can augment flows along a path P by any

amount ∆∆∆∆ ≤≤≤≤cP

� Ford-Fulkerson still works

� Get a flow that is maximum for the high-

order bits first and then add more bits later

Page 36: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

36

Capacity Scaling

5

6

7

4

3

4

1

5

3

7

64

s

a

b

c

x

y

z

t

Page 37: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

37

Capacity Scaling

101

110

111

100

011

100

001

101

011

111

110100

s

a

b

c

x

y

z

t

Page 38: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

38

Capacity Scaling Bit 1

101

110

111

100

011

100

001

101

011

111

110100

s

a

b

c

x

y

z

t

Capacity on each edge is at most 1(either 0 or 1 times ∆∆∆∆=4)

Page 39: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

39

1/111

Capacity Scaling Bit 1

101

1/110

100

011

1/100

001

1/101

011

1/111

1/110100

s

a

b

c

x

y

z

t

O(nm) time

Page 40: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

40

Capacity Scaling Bit 2

101

10/110

10/111

100

011

10/100

001

10/101

011

10/111

10/110100

s

a

b

c

x

y

z

t

Residual capacity across min cut is at most m(either 0 or 1 times ∆∆∆∆=2)

Page 41: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

41

Capacity Scaling Bit 2

10/101

10/110

10/111

01/100

01/011

10/100

001

10/101

01/011

11/111

10/110100

s

a

b

c

x

y

z

t

Residual capacity across min cut is at most m

⇒⇒⇒⇒ ≤≤≤≤ m augmentations

Page 42: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

42

Capacity Scaling Bit 3

100/101

100/110

010/100

010/011

100/100

001

100/101

010/011

110/111

100/110100

s

a

b

c

x

y

z

t

Residual capacity across min cut is at most m(either 0 or 1 times ∆∆∆∆=1)

100/111

Page 43: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

43

Capacity Scaling Bit 3

101/101

101/110

010/100

011/011

100/100

001/001

101/101

010/011

111/111

110/110100

s

a

b

c

x

y

z

t101/111

After ≤ m augmentations

Page 44: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

44

Capacity Scaling Final

5/5

5/6

2/4

3/3

4/4

1/1

5/5

2/3

7/7

6/64

s

a

b

c

x

y

z

t5/7

Page 45: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

45

Capacity Scaling Min Cut

5/5

5/6

2/4

3/3

4/4

1/1

5/5

2/3

7/7

6/64

s

a

b

c

x

y

z

t5/7

Page 46: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

46

Total time for capacity scaling

� log2 U rounds where U is largest capacity

� At most m augmentations per round� Let ci be the capacities used in the ith round and fi

be the maxflow found in the ith round

� For any edge (u,v), ci+1(u,v) ≤ 2ci(u,v)+1� i+1st round starts with flow f = 2 fi

� Let (A,B) be a min cut from the ith round

� νννν(fi)=ci(A,B) so νννν(f)=2ci(A,B)

� νννν(fi+1) ≤ ci+1(A,B) ≤ 2ci(A,B)+m =νννν(f)+m

� O(m) time per augmentation

� Total time O(m2 log U)

Page 47: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

47

Edmonds-Karp Algorithm

� Use a shortest augmenting path (via Breadth First Search in residual graph)

� Time: O(n m2)

Page 48: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

48

BFS/Shortest Path Lemmas

Distance from s in Gf is never reduced by:

• Deleting an edgeProof: no new (hence no shorter) path created

• Adding an edge (u,v), provided v is nearer

than uProof: BFS is unchanged, since v visited before

(u,v) examined

s

v

u

a back edge

Page 49: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

49

Key Lemma

Let f be a flow, Gf the residual graph, and P a shortest augmenting path. Then no vertex is closer to s after augmentation along P.

Proof: Augmentation along P only deletes forward edges, or adds back edges that go to previous vertices along P

Page 50: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

50

Augmentation vs BFS

t

v

u

x

s

5/9

3/100/5

3/3

2/5

t

v

u

x

s

t

v

u

x

sG: Gf Gf’

Page 51: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

51

Theorem

The Edmonds-Karp Algorithm performs O(mn) flow augmentations

Proof:

Call (u,v) critical for augmenting path P if it’s closest to s having min residual capacity

It will disappear from Gf after augmenting along P

In order for (u,v) to be critical again the (u,v) edge must re-appear in Gf but that will only happen when the distance to u has increased by 2 (next slide)

It won’t be critical again until farther from sso each edge critical at most n/2 times

Page 52: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

52

Critical Edges in Gf

Shortest s-t path P in Gf

vus x twcP cP>cP >cP

critical edge df(s,v)=df(s,u)+1 since this is a shortest path

After augmenting along P

>0 >0vus x tw

For (u,v) to be critical later for some flow f’ it must be in Gf’

so must have augmented along a shortest path containing (v,u)

vus x tw

Then we must have df’(s,u)=df’(s,v)+1 ≥≥≥≥ df(s,v)+1=df(s,u)+2

Page 53: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

53

Corollary

� Edmonds-Karp runs in O(nm2) time

Page 54: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

54

Project Selectiona.k.a. The Strip Mining Problem

� Given� a directed acyclic graph G=(V,E)

representing precedence constraints on tasks (a task points to its predecessors)

� a profit value p(v) associated with each task v∈∈∈∈V (may be positive or negative)

� Find� a set A⊆⊆⊆⊆V of tasks that is closed under

predecessors, i.e. if (u,v)∈E and u∈∈∈∈A then

v∈∈∈∈A, that maximizes Profit(A)=ΣΣΣΣv∈∈∈∈A p(v)

Page 55: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

55

Project Selection Graph

-1

43

12

10

8

-3

11

-13

14

-5-6

4

2

Each task points to its predecessor tasks

Page 56: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

56

Extended Graph

-1

43

12

10

8

-3

11

-13

14

-5-6

4

2

s

t

Page 57: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

57

Extended Graph G’

-1

43

12

10

8

-3

11

-13

14

-5-6

4

2

s

t

5

10For each vertex vIf p(v)≥0 add (s,v) edge

with capacity p(v)If p(v)<0 add (v,t) edge

with capacity –p(v)

Page 58: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

58

Extended Graph G’

� Want to arrange capacities on edges of G so that for minimum s-t-cut (S,T) in G’, the set A=S-{s}� satisfies precedence constraints

� has maximum possible profit in G

� Cut capacity with S={s} is just C=ΣΣΣΣv: p(v)≥≥≥≥0 p(v)� Profit(A) ≤ C for any set A

� To satisfy precedence constraints don’t want any original edges of G going forward across the minimum cut� That would correspond to a task in A=S-{s} that had a

predecessor not in A=S-{s}

� Set capacity of each of the edges of G to C+1� The minimum cut has size at most C

Page 59: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

59

Extended Graph G’

-1

43

12

10

8

-3

11

-13

14

-5-6

4

2

s

t

5

10

Capacity C

Page 60: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

60

Extended Graph G’

-1

43

12

10

8

-3

11

-13

14

-5-6

4

2

s

t

5

10Cut value =13+3+2+3+4=13+3

+C-4-8-10-11-12-14

Page 61: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

61

Project Selection

� Claim Any s-t-cut (S,T) in G’ such that A=S-{s} satisfies precedence constraints has capacity

c(S,T)=C - Σv∈∈∈∈A p(v) = C - Profit(A)

� Corollary A minimum cut (S,T) in G’ yields an optimal solution A=S-{s} to the profit selection problem

� Algorithm Compute maximum flow f in G’, find the set S of nodes reachable from s in G’fand return S-{s}

Page 62: CSE 421: Introduction to Algorithms · CSE 421: Introduction to Algorithms Network Flow Paul Beame. 2 Bipartite Matching Given: A bipartite graph G=(V,E) ... Note: max flow ≥ 4

62

Proof of Claim

� A=S-{s} satisfies precedence constraints � No edge of G crosses forward out of A since those

edges have capacity C+1

� Only forward edges cut are of the form (v,t) for v∈∈∈∈A or (s,v) for v∉∉∉∉A

� The (v,t) edges for v∈∈∈∈A contribute

Σv∈∈∈∈A:p(v)<<<<0 -p(v) = - Σv∈∈∈∈A:p(v)<<<<0 p(v)

� The (s,v) edges for v∉∉∉∉A contribute

ΣΣΣΣv∉∉∉∉A: p(v)≥≥≥≥0 p(v)=C-ΣΣΣΣv∈∈∈∈A: p(v)≥≥≥≥0 p(v)

� Therefore the total capacity of the cut is

c(S,T) = C - Σv∈∈∈∈A p(v) =C-Profit(A)