week 11 graphs iii network flow problems a simple maximum-flow algorithm

17
1 WEEK 11 Graphs III Network Flow Problems A Simple Maximum-Flow Algorithm Izmir University of Economics

Upload: hamish-hernandez

Post on 01-Jan-2016

46 views

Category:

Documents


0 download

DESCRIPTION

WEEK 11 Graphs III Network Flow Problems A Simple Maximum-Flow Algorithm. Network Flow Problems. Given a directed graph G=(V, E) with edge capacities c vw for each edge (v, w)  We have 2 special vertices: s , which we call the source t , which is the sink. c vw. v. w. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

1

WEEK 11 Graphs III

Network Flow Problems

A Simple Maximum-Flow Algorithm

Izmir University of Economics

Page 2: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

Network Flow Problems

Given a directed graph G=(V, E) with edge capacities cvw for each edge (v, w)

We have 2 special vertices: s, which we call the source t, which is the sink.

2Izmir University of Economics

cvw wv

Page 3: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

Network Flow Problems

Through any edge (v, w), at most cvw units of flow may pass.

At any vertex, v, that is not either s or t, the total flow coming in must equal the total flow going out.

3Izmir University of Economics

Page 4: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

4

Problem DefinitionMaximum Flow Problem: Given a directed network G with edge capacities given by cvw ’s, determine the maximum amount of flow that can be sent from a source node s to a sink node t.

1

2 4

3 5

6

10

25

35

20

15

35

40

30

20

s t

Page 5: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A Simple Maximum-Flow Algorithm Initial Attempt

Flow graph Gf tells the flow attained at any stage. Initially all edges in Gf have no flow (costs=0).

Residual graph Gr tells, for each edge, how much more flow can be added (capacity - current flow). An edge in Gr is known as a residual edge.

Gr = G-Gf

5Izmir University of Economics

G Gf Gr

Page 6: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A Simple Maximum-Flow Algorithm Initial Attempt

Find a path in Gr from s to t. This path is known as an augmenting path.

The minimum cost edge on this path is the amount of flow that can be added to every edge on the corresponding path in Gf. (Do this by adjusting Gf and recomputing Gr)

If there is no path from s to t in Gr, we terminate.

6Izmir University of Economics

Page 7: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A Simple Maximum-Flow Algorithm Example 1/2

Initial stages of the graph, flow graph and residual graph

G, Gf, Gr after 2 units of flow added along s, b, d, t

7Izmir University of Economics

Page 8: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A Simple Maximum-Flow Algorithm – Example 2/2

G, Gf, Gr after 2 units of flow added along s, a, c, t

G, Gf, Gr after 1 unit of flow added along s, a, d, t – algorithm terminates.

Looks allright?

8

Izmir University of Economics

Page 9: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A Simple Maximum-Flow Algorithm – Example

!!!Not DeterministicIf we were to choose s, a, d, t with 3 units of flow, algorithm would terminate right there.

Izmir University of Economics 9

G, Gf, Gr If initial action is to add 3 units of flow along s, a, d, t – algorithm terminates.

Page 10: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm – Correct Version

In order to make the algorithm work, we need to allow the algorithm to change its mind.

To do this, for every edge (v, w) with flow fv,w in Gf, we will add an edge (w, v) in the residual graph Gr of capacity fv,w. In effect, we are allowing the algorithm to undo its decisions by sending flow back in the opposite direction.

Surprisingly, it can be shown that if the edge capacities are rational numbers, this algorithm always terminates with a maximal flow.

10Izmir University of Economics

Page 11: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

Maximum-Flow Algorithm – Correct Version (Ford-Fulkerson Algorithm)– Example I

Page 12: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm Example II for Correct Version 1/2

G, Gf, Gr after 3 units of flow along s, a, d, t using correct algorithm.

12

Izmir University of Economics

Page 13: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm – Example II for Correct Version 2/2

If the capacities are all integers and maximum flow is f, then, since each augmenting path increases the flow by at least 1, f stages suffice, and the total running time is O(f*|E|), since an augmenting path can be found in O(|V|+|E|) time by an unweighted shortest path algorithm. This is BAD!!!

G, Gf, Gr after 2 units of flow along s, b, d, a, c, t using correct algorithm.

13Izmir University of Economics

Page 14: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm Time Complexity – 1/2

The classic example of why this is bad. Max-flow is seen to be 2,000,000. Continually augmenting along a path that includes the edge connected by a and b were to occur repeatedly, 2,000,000 augmentations would be required, when we could get by with only 2.

14Izmir University of Economics

Page 15: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm Time Complexity – 2/2

A simple method to get around this problem is to always choose the augmenting path that allows the largest increase in flow.

If capmax is the maximum edge capacity, O(|E|*log (capmax)) augmentations will suffice.

Since O(|E|*log|V|) time is used for finding augmenting paths(dijkstra) O(|E|2*log|V|*logcapmax)

If capacities are small integers, this reduces to O(|E|2*log|V|).

15Izmir University of Economics

Page 16: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm – Time Complexity – III (Extra Slide)

(by Edmonds-Karp #1) Idea: don't just choose an arbitrary path. Instead, pick a path you can push a lot of flow along. Two versions: (1.1) use maximum bottleneck path (path with largest residual capacity). (1.2) Define a "capacity threshold" c and then just try to find an s-t path of residual capacity >= c. Advantage of this: it takes linear time. Just do a DFS over edges of residual capacity >= c . How to set c? Set initial value by trying 1,2,4,8,... until we can no longer find such a path. So, c is guaranteed to be within a factor 2 of max bottleneck F. Then, as we run the algorithm (finding paths and pushing flow along them), once there is no longer a path in which all edges have residual capacity >= c, we just let c = c/2. This ensures that c is within a factor 2 of F . Claim: Either way, this causes algorithm to make at most O(|E|*log F) iterations. So, the total time using version 1.2 is O(|E|2 log F).Proof of Claim: First, if the current residual graph has max flow f, then if we drop all edges of residual capacity < f/|E|, there must still exist a path from s to t (why? because let A be the component containing s. If doesn't have t, then since there at most |E| edges out of A, this would be a cut with size < f. Contradiction). So, max bottleneck path has capacity at least f/|E|, and (if using version 1.2) this means c is at least f/(2|E|).

16Izmir University of Economics

Page 17: WEEK 11  Graphs III  Network Flow Problems A Simple Maximum-Flow Algorithm

A simple Maximum-Flow Algorithm – Time Complexity – IV (Extra Slide)

Let's just focus on version 1.2 (since then 1.1 follows directly). We have c is at least f/(2|E|). So can have at most 2*|E| iterations before c gets cut down by a factor of 2. Since c <= F at the start, we can cut down c at most log(F) times, so total number of iterations is at most 2|E|*log(F). Can we remove dependence on F completely? (by Edmonds-Karp #2) Idea: always pick the shortest path (rather than the max-capacity path) Claim: this makes at most O(|E|*|V|) iterations. So, run time is O(E|2*|V|) since we use BFS in each iteration. Proof is pretty neat. Proof of Claim: Let d(t) be the distance from s to t in the current residual graph. We'll prove the theorem by showing that every |E| iterations, d(t) has to go up by at least 1. Let's lay out the graph in levels according to a BFS from s. Now, notice that so long as d(t) doesn't change, the paths found will only use forward edges. Each iteration saturates (and removes) at least 1 forward edge, and adds only backward edges (so no distance ever drops). This can happen at most |E|- d(t)+1 <= |E| times since after that many time, t would become disconnected from s. So, after |E| iterations, either t is disconnected (and d(t) = infinity) or else we must have used a non-forward edge, implying that d(t) has gone up by 1. Since d(t) can increase at most |V| times, there are at most O(|E|*|V|) iterations.

17Izmir University of Economics