26
1 Maximum Flow w s v u t z 3/3 1/9 1/1 3/3 4/7 4/6 3/5 1/1 3/5 2/2 Maximum Flow

Maximum Flow

Embed Size (px)

DESCRIPTION

Maximum Flow. c. v. 3 / 3. 4 / 6. 1 / 1. t. 4 / 7. 3 / 3. s. w. 1 / 9. 3 / 5. 1 / 1. 3 / 5. z. u. 2 / 2. Flow Network. A flow network (or just network) N consists of - PowerPoint PPT Presentation

Citation preview

1

Maximum Flow

ws

v

u

t

z

3/3

1/9

1/1

3/3

4/7

4/6

3/51/13/5

2/2

Maximum Flow

Flow Network

2

A flow network (or just network) N consists of A weighted digraph G with nonnegative integer edge weights, where the

weight of an edge e is called the capacity c(e) of e Two distinguished vertices, s and t of G, called the source and sink,

respectively, such that s has no incoming edges and t has no outgoing edges.

Example:

ws

v

u

t

z

3

9

1

3

7

6

515

2

Maximum Flow

Flow

3

A flow f for a network N is is an assignment of an integer value f(e) to each edge e that satisfies the following properties:Capacity Rule: For each edge e, 0 f (e) c(e)

Conservation Rule: For each vertex v s,t where E(v) and E(v) are the incoming and outgoing edges of v, resp.

The value of a flow f , denoted |f|, is the total flow from the source, which is the same as the total flow into the sink

Example:

)()(

)()(vEevEe

efef

ws

v

u

t

z

3/3

2/9

1/1

1/3

3/7

2/6

4/51/13/5

2/2Maximum Flow

Maximum Flow

4

A flow for a network N is said to be maximum if its value is the largest of all flows for N

The maximum flow problem consists of finding a maximum flow for a given network N

Applications Hydraulic systems Electrical circuits Traffic movements Freight transportation

ws

v

u

t

z

3/3

2/9

1/1

1/3

3/7

2/6

4/51/13/5

2/2

ws

v

u

t

z

3/3

2/9

1/1

3/3

3/7

4/6

4/51/13/5

2/2

Flow of value 8 = 2 + 3 + 3 = 1 + 3 + 4

Maximum flow of value 10 = 4 + 3 + 3 = 3 + 3 + 4Maximum Flow

Greedy may not work

5

If we just keep picking paths and adding them to the flow, we may make a mistake.

In this case, sending 2 units along s-v-w-t is a mistake

ws

v

u

t

z

3/6

1/1

2/2

0/3

5/5

2/2

5/51/15/5

4/4Flow of value 10 = 2 + 3 + 5 = 1 + 5 + 4

Maximum flow of value 12 = 2+5+5 = 2+0+5+1+4

ws

u

t

z

5/6

1/1

0/25/5

2/2

5/51/15/5

4/4

v 2/3

Maximum Flow

Cut

6

A cut of a network N with source s and sink t is a partition (Vs,Vt) of the vertices of N such that s Vs and t Vt Forward edge of cut : origin in Vs and

destination in Vt Backward edge of cut : origin in Vt and

destination in Vs

Flow f() across a cut : total flow of forward edges minus total flow of backward edges

Capacity c() of a cut : total capacity of forward edges

Example: c() 24 f() 8

ws

v

u

t

z

3

9

1

3

7

6

515

2

ws

v

u

t

z

3/3

2/9

1/1

1/3

3/7

2/6

4/51/13/5

2/2

Maximum Flow

Flow and Cut

7

Lemma:The flow f() across any cut is equal to the flow value |f|

Lemma:The flow f() across a cut is less than or equal to the capacity c() of the cut

Theorem:The value of any flow is less than or equal to the capacity of any cut, i.e., for any flow f and any cut , we have

|f| c()

ws

v

u

t

z

3/3

2/9

1/1

1/3

3/7

2/6

4/51/13/5

2/2

1 2

c(1) 12 6 3 1 2c(2) 21 3 7 9 2

|f| 8Maximum Flow

Augmenting Path

8

Consider a flow f for a network N

Let e be an edge from u to v: Residual capacity of e from u to

v: f(u, v) = c(e) f (e) Residual capacity of e from v to

u: f(v, u) = f (e) (we can go backwards along

edge) Let be a path from s to t

The residual capacity f() of is the smallest of the residual capacities of the edges of in the direction from s to t

A path from s to t is an augmenting path if f() 0

ws

v

u

t

z

3/3

2/9

1/1

1/3

2/7

2/6

4/50/12/5

2/2

f(s,u) 3f(u,w) 1f(w,v) 1f(v,t) 2f() 1

|f| 7Maximum Flow

Flow Augmentation

9

Lemma:Let be an augmenting path for flow f in network N. There exists a flow f for N of value

| f | = |f | f()

Proof: We compute flow f by modifying the flow on the edges of Forward edge:

f (e) = f(e) f()

Backward edge:f (e) = f(e)

f()

ws

v

u

t

z

3/3

2/9

1/1

1/3

2/7

2/6

4/50/12/5

2/2

f() = 1

ws

v

u

t

z

3/3

2/9

0/1

2/3

2/7

2/6

4/51/13/5

2/2

| f | = 7

| f | = 8Maximum Flow

Ford-Fulkerson’s Algorithm

10

Initially, f(e) 0 for each edge e

Repeatedly Search for an augmenting

path Augment by f() the flow

along the edges of A specialization of DFS (or

BFS) searches for an augmenting path An edge e is traversed

from u to v provided f(u, v) 0

Algorithm FordFulkersonMaxFlow(N)for all e G.edges()

setFlow(e, 0)while G has an augmenting path { compute residual capacity of } for all edges e { compute residual capacity of e }

if e is a forward edge of getCapacity(e) getFlow(e)

else { e is a backward edge } getFlow(e)

if <

{ augment flow along }for all edges e

if e is a forward edge of setFlow(e, getFlow(e) )

else { e is a backward edge }setFlow(e, getFlow(e) )

Maximum Flow

Example - Try a greedy algorithm, but be unlucky

11

ws

v

u

t

z

0/2

0/2

0/3

0/2

0/3

0/3

0/50/40/5

0/4

Example

12

ws

v

u

t

z

0/2

0/2

0/3

0/2

3/3

0/3

2/53/45/5

2/4

ws

v

u

t

z

2/2

2/2

0/3

2/2

3/3

2/3

4/53/45/5

2/4

ws

v

u

t

z

0/2

2/2

2/3

0/2

3/3

2/3

4/53/45/5

2/4

ws

v

u

t

z

0/2

0/2

0/3

0/2

3/3

0/3

0/53/43/5

0/4 No more forward flow

without pushing back

Example (cont)

13

ws

v

u

t

z

2/2

2/2

0/3

2/2

3/3

2/3

4/53/45/5

2/4

ws

v

u

t

z

2/2

2/2

2/3

2/2

3/3

3/3

5/52/45/5

3/4

Notice:This is clearly optimal, as the arcs into t (and leaving

s) are filled. If ANY cut is maxed out, we can’t do better.

Notice the effects of pushing backwards along an arc.We don’t REALLY require the flow goes forward and

then backward. It is like we could change our mind whilestill making progress.

Analysis

14

In the worst case, Ford-Fulkerson’s algorithm performs |f*| flow augmentations, where f* is a maximum flow

Example The augmenting paths found

alternate between 1 and 2

The algorithm performs 100 augmentations

Finding an augmenting path and augmenting the flow takes O(n m) time

The running time of Ford-Fulkerson’s algorithm is O(|f*|(n m))

ts

v

u

1/1

1/500/50

1/50 0/50

ts

v

u

0/1

1/501/50

1/50 1/50

1

2

Maximum Flow

Ford-Fulkerson Algorithm Details

15

Traversal can DFS or BFS, modify graph to include out-edges whose capacity is unused capacity and reversed flow edges who capacity is the flow they carry

Might only increase flow by 1 each timeO(|f*|m), pseudo-polynomial time (depends

on the magnitude of a parameter not its encoding size)

Maximum Flow

Edmonds-Karp Algorithm

16

Find the best augmenting paths, those with the fewest of edges. Greedy algorithm, like Prim’s.

Each time we choose a path, the number of edges along an augmenting path to any vertex can’t decrease.

But in turn, this implies that no edge can be a bottleneck (the smallest capacity) more than n times.

m edges can be a bottleneck n timesO(nm2)

Maximum Flow

Matchings

17

Kindergarten Teacher needs partners for a field trip

Maximum Flow

Maximum Bipartite Matching

18

Bipartite graphs – edges only between elements of two separate sets of vertices

Example:Jobs and Students. WorkAt edges link Students to Job. Can’t have edges from Students to Students.

Dancing, room scheduling (maximum number of classes simultaneously)

What if company has two jobs to fill?

Maximum Flow

Bipartite matching

19

In a bipartite match, all arcs go between

verticies of different sets. You want to

maximize the number of matches

Sometimes a bipartite matching problem is modeled as a network flow problem. Can you see how?

Maximum Flow

Maximum Bipartite Matching, solved by maximum flow

20

Direct edges, assign capacity 1Add source, sink, add edges to/from other nodes with

capacity m.Find maximal flow for maximal matchingConservation => no sharing of verticesUse regular FordFulkerson as flow is quite small.

O(f*m) but our F* is <n/2O(nm)

Maximum Flow

Maximum Bipartite Matching (transform into network flow) x: students y:jobs arc: “will work/will hire”

21 Maximum Flow

Maximum Bipartite Matching (transform into network flow) x: students y:jobs arc: “will work/will hire”

22 Maximum Flow

Minimum Cost flow(another version of network flow)

23

Add another “weight”, called cost, to edges to denote the cost to send a unit of flow along that leg.

Now, the cost of a flow is the sum of products of flow and costs on all edges

the cost of an augmenting path is the sum of forward edge costs minus the sum of backward costs

We want minimal cost for a given |f|An augmenting cycle for a flow has same |f| (since

it’s a cycle) but lower flow cost

Maximum Flow

Augmenting Cycles

24

Given an augmenting cycle, there is a new flow whose value is the same but whose cost is augmented by w(γ)fΔ(γ)

A flow is minimum cost there is no negative-cost augmenting cycle for it

Maximum Flow

Bellman-Ford Algorithm(recall from chapter 7)

25

A better idea is just to continually add the cheapest augmenting paths

Works even with negative-weight edges

Initially all nodes are Must assume directed edges

(for otherwise we would have negative-weight cycles)

Iteration i finds all shortest paths that use i edges.

Running time: O(nm).

Algorithm BellmanFord(G, s)for all v G.vertices()

if v ssetDistance(v, 0)

else setDistance(v, )

for i 1 to n-1 dofor each e =(u,z)

G.edges(){ relax edge e }r getDistance(u)

weight(e)if r getDistance(z)

setDistance(z,r)

Maximum Flow

26 Maximum Flow

Residual graphis shown on right

Find cheapest path

by looking at edge costs.

Send maximal flow along that

path