• Nenhum resultado encontrado

‣ Ford-Fulkerson algorithm

N/A
N/A
Protected

Academic year: 2022

Share "‣ Ford-Fulkerson algorithm "

Copied!
73
0
0

Texto

(1)

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

http://algs4.cs.princeton.edu

Algorithms

ROBERT SEDGEWICK | KEVIN WAYNE

6.4 M AXIMUM F LOW

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

(2)

http://algs4.cs.princeton.edu ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

6.4 M AXIMUM F LOW

(3)

Input. An edge-weighted digraph, source vertex s, and target vertex t.

Mincut problem

s 5 t

15

10 15

9

15

6

8 10

15 4

4 10

10 each edge has a

positive capacity

capacity

(4)

Def. A st-cut (cut) is a partition of the vertices into two disjoint sets, with s in one set A and t in the other set B.

Def. Its capacity is the sum of the capacities of the edges from A to B.

Mincut problem

s 5

15 10

t

(5)

Def. A st-cut (cut) is a partition of the vertices into two disjoint sets, with s in one set A and t in the other set B.

Def. Its capacity is the sum of the capacities of the edges from A to B.

10

Mincut problem

8

don't count edges from B to A

t s

(6)

Def. A st-cut (cut) is a partition of the vertices into two disjoint sets, with s in one set A and t in the other set B.

Def. Its capacity is the sum of the capacities of the edges from A to B.

Minimum st-cut (mincut) problem. Find a cut of minimum capacity.

10

Mincut problem

s

10 8 t

(7)

"Free world" goal. Cut supplies (if cold war turns into real war).

Figure 2

From Harris and Ross [1955]: Schematic diagram of the railway network of the Western So-

Mincut application (RAND 1950s)

7

rail network connecting Soviet Union with Eastern European countries (map declassified by Pentagon in 1999)

(8)

Potential mincut application (2010s)

Government-in-power’s goal. Cut off communication to set of people.

(9)

Input. An edge-weighted digraph, source vertex s, and target vertex t.

Maxflow problem

4

4 15

10

10

s 5 t

6

10 9

8 15

10 15

15

capacity each edge has a

positive capacity

(10)

Def. An st-flow (flow) is an assignment of values to the edges such that:

Capacity constraint: 0 ≤ edge's flow ≤ edge's capacity.

Local equilibrium: inflow = outflow at every vertex (except s and t).

Maxflow problem

0 / 4

0 / 4 0 / 15

10 / 10

10 / 10

5 / 5 v

s t

0 / 6

5 / 10 5 / 9

5 / 8 5 / 15

10 / 10 10 / 15

inflow at v = 5 + 5 + 0 = 10 outflow at v = 10 + 0 = 10

flow capacity

0 / 15

(11)

Maxflow problem

Def. An st-flow (flow) is an assignment of values to the edges such that:

Capacity constraint: 0 ≤ edge's flow ≤ edge's capacity.

Local equilibrium: inflow = outflow at every vertex (except s and t).

Def. The value of a flow is the inflow at t.

0 / 4 10 / 10

10 / 10 5 / 5

s t

5 / 10 5 / 9

5 / 8 5 / 15

10 / 10 10 / 15

0 / 15

value = 5 + 10 + 10 = 25

0 / 4

0 / 6 0 / 15

we assume no edges point to s or from t

(12)

Maxflow problem

Def. An st-flow (flow) is an assignment of values to the edges such that:

Capacity constraint: 0 ≤ edge's flow ≤ edge's capacity.

Local equilibrium: inflow = outflow at every vertex (except s and t).

Def. The value of a flow is the inflow at t.

Maximum st-flow (maxflow) problem. Find a flow of maximum value.

0 / 4 10 / 10

10 / 10 5 / 5

s

8 / 10 8 / 9

8 / 8

10 / 10 13 / 15

0 / 15

value = 8 + 10 + 10 = 28

0 / 4

3 / 6 0 / 15

t

2 / 15

(13)

Soviet Union goal. Maximize flow of supplies to Eastern Europe.

Figure 2

From Harris and Ross [1955]: Schematic diagram of the railway network of the Western So-

Maxflow application (Tolsto ǐ 1930s)

13

flow capacity

rail network connecting Soviet Union with Eastern European countries (map declassified by Pentagon in 1999)

(14)

Potential maxflow application (2010s)

"Free world" goal. Maximize flow of information to specified set of people.

facebook graph

(15)

Input. A weighted digraph, source vertex s, and target vertex t.

Mincut problem. Find a cut of minimum capacity.

Maxflow problem. Find a flow of maximum value.

Remarkable fact. These two problems are dual!

Summary

s

value of flow = 28

t

0 / 4 10 / 10

10 / 10 5 / 5

8 / 10 8 / 9

8 / 8 2 / 15

10 / 10 13 / 15

0 / 4

3 / 6

13 / 16

0 / 15 0 / 15

s

capacity of cut = 28

10 8

10

t

(16)

http://algs4.cs.princeton.edu ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

6.4 M AXIMUM F LOW

(17)

Initialization. Start with 0 flow.

Ford-Fulkerson algorithm

s t

0 / 4

0 / 4 0 / 15

0 / 10

0 / 15

0 / 15

0 / 10

0 / 10 0 / 9

0 / 15

0 / 8

0 / 6

0 / 16

initialization

0 / 5 0

value of flow

0 / 10

flow capacity

(18)

Augmenting path. Find an undirected path from s to t such that:

Can increase flow on forward edges (not full).

Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

0 / 4

0 / 4 0 / 15

0 / 15

0 / 15

0 / 10 0 / 9

0 / 8

0 / 6

0 / 16

s t

0 / 10

0 / 15

0 / 10 0 / 10

0 / 15

0 / 10 10

10

10

0 / 5

1st augmenting path

0 + 10 = 10

0 / 10

bottleneck capacity = 10

(19)

Augmenting path. Find an undirected path from s to t such that:

Can increase flow on forward edges (not full).

Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

0 / 5

0 / 4

0 / 4

0 / 15

0 / 15

0 / 10 0 / 9

0 / 8

0 / 6 10 / 10

10 / 15

10 / 10

s t

0 / 15

0 / 10

0 / 16 0 / 15

0 / 10

0 / 16

10 10

10

2nd augmenting path

10 + 10 = 20

(20)

Augmenting path. Find an undirected path from s to t such that:

Can increase flow on forward edges (not full).

Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

0 / 4

0 / 4 0 / 6 0 / 15

10 / 10

10 / 10

10 / 15

10 / 10

10 / 16

s 0 / 5 t

0 / 10 0 / 9

0 / 8 10 / 15

0 / 5

0 / 10 0 / 9

0 / 8 10 / 15

3rd augmenting path

20 + 5 = 25

5

5

5

5

5

0 / 15

backward edge (not empty)

(21)

Augmenting path. Find an undirected path from s to t such that:

Can increase flow on forward edges (not full).

Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

0 / 4

0 / 4 0 / 15

10 / 10

10 / 10 5 / 5

s t

0 / 6

5 / 10 5 / 9

5 / 8

0 / 6

5 / 10 5 / 9

5 / 8 5 / 15 5 / 15

10 / 10 10 / 1510 / 15

10 / 16 10 / 16

4th augmenting path

25 + 3 = 28

8

2

8

8

13

13

3

0 / 15

backward edge (not empty)

(22)

Termination. All paths from s to t are blocked by either a

Full forward edge.

Empty backward edge.

Idea: increase flow along augmenting paths

no more augmenting paths

0 / 4

0 / 4 0 / 15

10 / 10

10 / 10 5 / 5

s t

3 / 6

8 / 10 8 / 9

8 / 8 2 / 15

10 / 10 13 / 15

28

13 / 16

0 / 15

full forward edge empty backward edge

(23)

Ford-Fulkerson algorithm

Questions.

How to compute a mincut?

How to find an augmenting path?

If FF terminates, does it always compute a maxflow?

Does FF always terminate? If so, after how many augmentations?

Start with 0 flow.

While there exists an augmenting path:

- find an augmenting path

- compute bottleneck capacity

- increase flow on that path by bottleneck capacity Ford-Fulkerson algorithm

(24)

http://algs4.cs.princeton.edu ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

6.4 M AXIMUM F LOW

(25)

Relationship between flows and cuts

Def. The net flow across a cut (A, B) is the sum of the flows on its edges from A to B minus the sum of the flows on its edges from B to A.

0 / 4 10 / 10

10 / 10 5 / 5

s t

5 / 10 5 / 9

5 / 8 5 / 15

10 / 10 10 / 15

0 / 15

value of flow = 25

0 / 4

0 / 6 0 / 15

net flow across cut = 5 + 10 + 10 = 25

(26)

Relationship between flows and cuts

Def. The net flow across a cut (A, B) is the sum of the flows on its edges from A to B minus the sum of the flows on its edges from B to A.

0 / 4 10 / 10

10 / 10 5 / 5

s t

5 / 10 5 / 9

5 / 8 5 / 15

10 / 10 10 / 15

0 / 15 0 / 4

0 / 6 0 / 15

value of flow = 25 net flow across cut = 10 + 5 + 10 = 25

(27)

Relationship between flows and cuts

Def. The net flow across a cut (A, B) is the sum of the flows on its edges from A to B minus the sum of the flows on its edges from B to A.

0 / 4 10 / 10

10 / 10 5 / 5

s t

5 / 10 5 / 9

5 / 8 5 / 15

10 / 10 10 / 15

0 / 15

value of flow = 25

0 / 4

0 / 6 0 / 15

net flow across cut = (10 + 10 + 5 + 10 + 0 + 0) – (5 + 5 + 0 + 0) = 25

edges from B to A

(28)

Relationship between flows and cuts

Flow-value lemma. Let f be any flow and let (A, B) be any cut. Then, the net flow across (A, B) equals the value of f.

Intuition. Conservation of flow.

Pf. By induction on the size of B.

Base case: B = { t }.

Induction step: remains true by local equilibrium when moving any vertex from A to B.

Corollary. Outflow from s = inflow to t = value of flow.

(29)

Relationship between flows and cuts

Weak duality. Let f be any flow and let (A, B) be any cut.

Then, the value of the flow ≤ the capacity of the cut.

Pf. Value of flow f = net flow across cut (A, B) ≤ capacity of cut (A, B).

flow-value lemma flow bounded by capacity

s t

0 / 4 10 / 10

9 / 10 5 / 5

8 / 10 8 / 9

7 / 8 2 / 15

10 / 10 12 / 15

0 / 4

2 / 6

12 / 16

0 / 15 0 / 15

s

15 5 10

t

(30)

Maxflow-mincut theorem

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f :

i. There exists a cut whose capacity equals the value of the flow f.

ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

[ i ii ]

Suppose that (A, B) is a cut with capacity equal to the value of f.

Then, the value of any flow f ' ≤ capacity of (A, B) = value of f.

Thus, f is a maxflow.

weak duality by assumption

(31)

Maxflow-mincut theorem

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f :

i. There exists a cut whose capacity equals the value of the flow f.

ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

[ ii iii ] We prove contrapositive: ~iii ~ii.

Suppose that there is an augmenting path with respect to f.

Can improve flow f by sending flow along this path.

Thus, f is not a maxflow.

(32)

Maxflow-mincut theorem

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f :

i. There exists a cut whose capacity equals the value of the flow f.

ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

[ iii i ]

Suppose that there is no augmenting path with respect to f.

Let (A, B) be a cut where A is the set of vertices connected to s by an undirected path with no full forward or empty backward edges.

By definition of cut, s is in A.

Since no augmenting path, t is in B.

Capacity of cut = net flow across cut

 

= value of flow f.

forward edges full; backward edges empty flow-value lemma

(33)

To compute mincut (A, B) from maxflow f :

By augmenting path theorem, no augmenting paths with respect to f.

Compute A = set of vertices connected to s by an undirected path with no full forward or empty backward edges.

Computing a mincut from a maxflow

forward edge (not full)

0 / 4

0 / 15 10 / 10

10 / 10

5 / 5 t

8 / 10 8 / 9

8 / 8 2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15 13 / 15

6 / 6 6 / 6 3 / 43 / 4

full forward edge empty backward edge

A

(34)

http://algs4.cs.princeton.edu ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

6.4 M AXIMUM F LOW

(35)

Ford-Fulkerson algorithm

Questions.

How to compute a mincut? Easy. ✔

How to find an augmenting path? BFS works well.

If FF terminates, does it always compute a maxflow? Yes. ✔

Does FF always terminate? If so, after how many augmentations?

yes, provided edge capacities are integers requires clever analysis

Start with 0 flow.

While there exists an augmenting path:

- find an augmenting path

- compute bottleneck capacity

- increase flow on that path by bottleneck capacity Ford-Fulkerson algorithm

(36)

Ford-Fulkerson algorithm with integer capacities

Important special case. Edge capacities are integers between 1 and U.

Invariant. The flow is integer-valued throughout Ford-Fulkerson.

Pf. [by induction]

Bottleneck capacity is an integer.

Flow on an edge increases/decreases by bottleneck capacity.

Proposition. Number of augmentations ≤ the value of the maxflow.

Pf. Each augmentation increases the value by at least 1.

Integrality theorem. There exists an integer-valued maxflow.

Pf. Ford-Fulkerson terminates and maxflow that it finds is integer-valued.

flow on each edge is an integer

critical for some applications (stay tuned)

(37)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

s

0

1

0 0

0

capacity flow

100

100

100 100

0

initialize with 0 flow

(38)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

0

0

s

0

1

0 0

1st iteration

1

1 1

100

100

100 100

(39)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

1

1 1

2nd iteration

0

0

s

1

1

1

0

100

100

100 100

(40)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

1

1

s

1

1

1 0

3rd iteration

2

2 1

100

100

100 100

(41)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

2

2 1

4th iteration

1

1

s

1

2

2

0

100

100

100 100

(42)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

. . .

(43)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

s

99

1

99 0

199th iteration

100

100 1

99

99

100

100

100 100

(44)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

100

100 1

200th iteration

99

99

s

1

100

100

0

100

100

100 100

(45)

Bad news. Even when edge capacities are integers, number of augmenting paths could be equal to the value of the maxflow.

Good news. This case is easily avoided. [use shortest/fattest path]

Bad case for Ford-Fulkerson

0

100

100

s

1

100

100

100 100

100

100

can be exponential in input size

(46)

How to choose augmenting paths?

Use care when selecting augmenting paths.

Some choices lead to exponential algorithms.

Clever choices lead to polynomial algorithms.

augmenting path number of paths implementation

random path ≤ E U randomized queue

DFS path ≤ E U stack (DFS)

shortest path ≤ ½ E V queue (BFS)

fattest path ≤ E ln(E U) priority queue

digraph with V vertices, E edges, and integer capacities between 1 and U

(47)

How to choose augmenting paths?

Choose augmenting paths with:

Shortest path: fewest number of edges.

Fattest path: max bottleneck capacity.

47

Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems

J A C K E D M O N D S

University of Waterloo, Waterloo, Ontario, Canada A N D

R I C H A R D M. K A R P

University of California, Berkeley, California

ABSTRACT. This paper presents new algorithms for t h e m a x i m u m flow problem, the Hitchcock t r a n s p o r t a t i o n problem, and t h e general m i n i m u m - c o s t flow problem. U p p e r bounds on the numbers of steps in these algorithms are derived, and are shown to compale favorably with upper bounds on t h e numbers of steps required by earlier algorithms.

First, the paper states the m a x i m u m flow problem, gives the F o r d - F u l k e r s o n labeling method for its solution, and points out t h a t an improper choice of flow a u g m e n t i n g p a t h s can lead to severe c o m p u t a t i o n a l difficulties. T h e n rules of choice t h a t avoid these difficulties are given.

We show t h a t , if each flow a u g m e n t a t i o n is made along an a u g m e n t i n g p a t h h a v i n g a minimum n u m b e r of arcs, t h e n a m a x i m u m flow in an n-node network will be o b t a i n e d a f t e r no more t h a n

~(n a - n) a u g m e n t a t i o n s ; and t h e n we show t h a t if each flow change is chosen to produce a m a x i m u m increase in the flow value then, provided the capacities are integral, a m a x i m u m flow will be d e t e r m i n e d within at most 1 + logM/(M--1) if(t, S) a u g m e n t a t i o n s , wheref*(t, s) is the value of the maximum flow and M is the m a x i m u m n u m b e r of arcs across a cut.

Next a new algorithm is given for the m i n i m u m - c o s t flow problem, in which all s h o r t e s t - p a t h c o m p u t a t i o n s are performed on networks with all weights nonnegative. In particular, this a l g o r i t h m solves the n X n assigmnent problem in O(n 3) steps. Following t h a t we explore a

" s c a l i n g " technique for solving a minimum-cost flow problem by t r e a t i n g a sequence of derived problems w i t h "scaled d o w n " capacities. It is shown t h a t , using this technique, the solution of a I i i t c h c o c k t r a n s p o r t a t i o n problem w i t h m sources and n sinks, m ~ n, and m a x i m u m flow B, requires at most (n + 2) log2 (B/n) flow a u g m e n t a t i o n s . Similar results are also given for the general minimum-cost flow problem.

An a b s t r a c t s t a t i n g the main results of the present paper was presented at the Calgary I n t e r n a t i o n a l Conference on C o m b i n a t o r i a l Structures and T h e i r Applications, J u n e 1969.

In a paper b y l)inic (1970) a result closely related to the main result of Section 1.2 is obtained.

Dinic shows t h a t , in a network with n nodes and p arcs, a m a x i m u m flow can be computed in 0 (n2p) primitive operations b y an algorithm which a u g m e n t s along s h o r t e s t augmenting paths.

KEY WOl¢l)S AND PHP~ASES: network flows, t r a n s p o r t a t i o n problem, analysis of algorithms CR CATEGOI{.IES: 5.3, 5.4, 8.3

Copyright © 1972, Association for C o m p u t i n g Machinery, Inc.

General permission to republish, b u t not for profit, all or p a r t of this m a t e r i a l is granted, provided t h a t reference is made to this publication, to its date of issue, and to the fact t h a t r e p r i n t i n g privileges were granted by permission of the Association for C o m p u t i n g Machinery.

Authors' addresses : J. Edmonds, D e p a r t m e n t of Combinatorics and Optimization, University of Waterloo, Waterloo, Ontario, C a n a d a ; R. M. Karp, College of Engineering, Operations Research Center, U n i v e r s i t y of California, Berkeley, CA 94720; the l a t t e r a u t h o r ' s research has been partially s u p p o r t e d by the N a t i o n a l Science F o u n d a t i o n raider G r a n t GP-15473 with the

Edmonds-Karp 1972 (USA) Dinic 1970 (Soviet Union)

(48)

http://algs4.cs.princeton.edu ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

6.4 M AXIMUM F LOW

(49)

Flow edge data type. Associate flow fe and capacity ce with edge e = v→w.

Flow network data type. Must be able to process edge e = v→w in either direction: include e in adjacency lists of both v and w.

Residual (spare) capacity.

Forward edge: residual capacity = ce fe.

Backward edge: residual capacity = fe.

Augment flow.

Forward edge: add ∆ .

Backward edge: subtract ∆ .

Flow network representation

w

7 / 9

v

flow fe capacity ce

v w

2 residual capacity

forward edge

(50)

Residual network. A useful view of a flow network.

Key point. Augmenting paths in original network are in 1-1 correspondence with directed paths in residual network.

t

Flow network representation

9 / 10

4 / 10 4 / 5

9 / 10 9 / 9

0 / 8 4 / 4

4 / 4 0 / 15

original network

s t

1

1 6

1 9

8

4 4 15

residual network

4 4

9 9

backward edge (not empty)

s

forward edge (not full) includes all edges with positive residual capacity

(51)

Flow edge API

public class FlowEdge public class FlowEdge public class FlowEdge

FlowEdge(int v, int w, double capacity) create a flow edge v→w

int from() vertex this edge points from

int to() vertex this edge points to

int other(int v) other endpoint

double capacity() capacity of this edge

double flow() flow in this edge

double residualCapacityTo(int v) residual capacity toward v void addResidualFlowTo(int v, double delta) add delta flow toward v

w

7 / 9

v

flow fe capacity ce

v w

2 residual capacity

forward edge

(52)

Flow edge: Java implementation

public class FlowEdge {

private final int v, w; // from and to private final double capacity; // capacity private double flow; // flow

public FlowEdge(int v, int w, double capacity) {

this.v = v;

this.w = w;

this.capacity = capacity;

}

public int from() { return v; } public int to() { return w; } public double capacity() { return capacity; } public double flow() { return flow; } public int other(int vertex)

{

if (vertex == v) return w;

else if (vertex == w) return v;

else throw new IllegalArgumentException();

}

public double residualCapacityTo(int vertex) {...}

public void addResidualFlowTo(int vertex, double delta) {...}

flow variable (mutable)

next slide

(53)

Flow edge: Java implementation (continued)

public double residualCapacityTo(int vertex) {

if (vertex == v) return flow;

else if (vertex == w) return capacity - flow;

else throw new IllegalArgumentException();

}

public void addResidualFlowTo(int vertex, double delta) {

if (vertex == v) flow -= delta;

else if (vertex == w) flow += delta;

else throw new IllegalArgumentException();

}

forward edge backward edge

forward edge backward edge

w

7 / 9

v

flow fe capacity ce

v w

2 residual capacity

forward edge

(54)

Conventions. Allow self-loops and parallel edges.

Flow network API

public class FlowNetwork public class FlowNetwork

FlowNetwork(int V)

FlowNetwork(int V) create an empty flow network with V vertices FlowNetwork(In in)

FlowNetwork(In in) construct flow network input stream

void addEdge(FlowEdge e)addEdge(FlowEdge e) add flow edge e to this flow network Iterable<FlowEdge> adj(int v)adj(int v) forward and backward edges incident to v Iterable<FlowEdge> edges()edges() all edges in this flow network

int V()V() number of vertices

int E()E() number of edges

String toString()toString() string representation

(55)

Flow network: Java implementation

public class FlowNetwork {

private final int V;

private Bag<FlowEdge>[] adj;

public FlowNetwork(int V) {

this.V = V;

adj = (Bag<FlowEdge>[]) new Bag[V];

for (int v = 0; v < V; v++)

adj[v] = new Bag<FlowEdge>();

}

public void addEdge(FlowEdge e) {

int v = e.from();

int w = e.to();

adj[v].add(e);

adj[w].add(e);

}

public Iterable<FlowEdge> adj(int v) { return adj[v]; }

}

same as EdgeWeightedGraph, but adjacency lists of

FlowEdges instead of Edges

add forward edge add backward edge

(56)

Maintain vertex-indexed array of FlowEdge lists (use Bag abstraction).

Note. Adjacency list includes edges with 0 residual capacity.

(residual network is represented implicitly)

Flow network: adjacency-lists representation

Flow network representation

adj[]

0 1 2 3 4 5

0 2 3.0 1.0 0 1 2.0 2.0

objectsBag

4 5 3.0 1.0 3 5 2.0 2.0

4 5 3.0 1.0 2 4 1.0 1.0 1 4 1.0 0.0 3 5 2.0 2.0 2 3 1.0 0.0 1 3 3.0 2.0 2 4 1.0 1.0 2 3 1.0 0.0 0 2 3.0 1.0 1 4 1.0 0.0 1 3 3.0 2.0 0 1 2.0 2.0

references to the same

FlowEdge object

6 8

0 1 2.0 0 2 3.0 1 3 3.0 1 4 1.0 2 3 1.0 2 4 1.0 3 5 2.0 4 5 3.0

tinyFN.txt

V

E

(57)

Finding a shortest augmenting path (cf. breadth-first search)

private boolean hasAugmentingPath(FlowNetwork G, int s, int t) {

edgeTo = new FlowEdge[G.V()];

marked = new boolean[G.V()];

Queue<Integer> queue = new Queue<Integer>();

queue.enqueue(s);

marked[s] = true;

while (!queue.isEmpty()) {

int v = queue.dequeue();

for (FlowEdge e : G.adj(v)) {

int w = e.other(v);

if (!marked[w] && (e.residualCapacityTo(w) > 0) ) {

edgeTo[w] = e;

marked[w] = true;

queue.enqueue(w);

} }

}

return marked[t];

}

save last edge on path to w;

mark w;

add w to the queue

found path from s to w in the residual network?

is t reachable from s in residual network?

(58)

Ford-Fulkerson: Java implementation

public class FordFulkerson {

private boolean[] marked; // true if s->v path in residual network private FlowEdge[] edgeTo; // last edge on s->v path

private double value; // value of flow

public FordFulkerson(FlowNetwork G, int s, int t) {

value = 0.0;

while (hasAugmentingPath(G, s, t)) {

double bottle = Double.POSITIVE_INFINITY;

for (int v = t; v != s; v = edgeTo[v].other(v))

bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v));

for (int v = t; v != s; v = edgeTo[v].other(v)) edgeTo[v].addResidualFlowTo(v, bottle);

value += bottle;

} }

private boolean hasAugmentingPath(FlowNetwork G, int s, int t) { /* See previous slide. */ }

public double value() { return value; }

public boolean inCut(int v) { return marked[v]; }

compute

bottleneck capacity

augment flow

is v reachable from s in residual network?

compute edgeTo[]

(59)

http://algs4.cs.princeton.edu ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

introduction

Ford-Fulkerson algorithm

maxflow-mincut theorem

analysis of running time

Java implementation

applications

6.4 M AXIMUM F LOW

(60)

Maxflow/mincut is a widely applicable problem-solving model.

Data mining.

Open-pit mining.

Bipartite matching.

Network reliability.

Baseball elimination.

Image segmentation.

Network connectivity.

Distributed computing.

Security of statistical data.

Egalitarian stable matching.

Multi-camera scene reconstruction.

Sensor placement for homeland security.

Many, many, more.

Maxflow and mincut applications

liver and hepatic vascularization segmentation

(61)

Bipartite matching problem

N students apply for N jobs.

Each gets several offers.

Is there a way to match all students to jobs?

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

Adobe Amazon Google Adobe Amazon Adobe Facebook Google Amazon Yahoo Amazon Yahoo

6

7

8 9

10

Adobe

Amazon

Facebook Google

Yahoo Alice Bob Carol Alice Bob Dave Eliza Carol Alice Carol Dave Eliza bipartite matching problem

(62)

Given a bipartite graph, find a perfect matching.

Bipartite matching problem

bipartite graph

N students N companies

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

Adobe Amazon Google Adobe Amazon Adobe Facebook Google Amazon Yahoo Amazon Yahoo

6

7

8 9

10

Adobe

Amazon

Facebook Google

Yahoo Alice Bob Carol Alice Bob Dave Eliza Carol Alice Carol Dave Eliza bipartite matching problem perfect matching (solution)

Alice Bob Carol Dave Eliza

—— Google

—— Adobe

—— Facebook

—— Yahoo

—— Amazon 3 1

5 2

4

6

8

9 7

10

(63)

Network flow formulation of bipartite matching

Create s, t, one vertex for each student, and one vertex for each job.

Add edge from s to each student (capacity 1).

Add edge from each job to t (capacity 1).

Add edge from student to each job offered (infinite capacity).

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

Adobe Amazon Google Adobe Amazon Adobe Facebook Google Amazon Yahoo Amazon Yahoo

6

7

8 9

10

Adobe

Amazon

Facebook Google

Yahoo Alice Bob Carol Alice Bob Dave Eliza Carol Alice Carol Dave Eliza

3 1

t

6

8

9

5 5 2

4

7

10

s

bipartite matching problem

N students N companies

flow network

(64)

1-1 correspondence between perfect matchings in bipartite graph and integer-valued maxflows of value N.

Network flow formulation of bipartite matching

3 1

t

6

8

9

5 5 2

4

7

10

s

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

Adobe Amazon Google Adobe Amazon Adobe Facebook Google Amazon Yahoo Amazon Yahoo

6

7

8 9

10

Adobe

Amazon

Facebook Google

Yahoo Alice Bob Carol Alice Bob Dave Eliza Carol Alice Carol Dave Eliza bipartite matching problem flow network

N students N companies

(65)

Goal. When no perfect matching, explain why.

What the mincut tells us

5 3 2

4

10 6

8 7

9

5 2

4

10 7 1

S = { 2, 4, 5 } T = { 7, 10 }

student in S can be matched

only to companies in T

|S| > | T |

no perfect matching exists

(66)

Mincut. Consider mincut (A, B).

Let S = students on s side of cut.

Let T = companies on s side of cut.

Fact: | S | > | T |; students in S can be matched only to companies in T.

Bottom line. When no perfect matching, mincut explains why.

What the mincut tells us

3 1

t

6

8

9

5 5 2

4

7

10

s

2

4

7

10

s

5

no perfect matching exists

S = { 2, 4, 5 } T = { 7, 10 }

student in S can be matched

only to companies in T

|S| > | T |

(67)

Q. Which teams have a chance of finishing the season with the most wins?

Montreal is mathematically eliminated.

Montreal finishes with ≤ 80 wins.

Atlanta already has 83 wins.

Baseball elimination problem

i teamteam wins losses to play ATL PHI NYM MON

0 Atlanta 83 71 8 1 6 1

1 Philly 80 79 3 1 0 2

2 New York 78 78 6 6 0 0

3 Montreal 77 82 3 1 2 0

(68)

Q. Which teams have a chance of finishing the season with the most wins?

Philadelphia is mathematically eliminated.

Philadelphia finishes with ≤ 83 wins.

Either New York or Atlanta will finish with ≥ 84 wins.

Observation. Answer depends not only on how many games already won and left to play, but on whom they're against.

Baseball elimination problem

i teamteam wins losses to play ATL PHI NYM MON

0 Atlanta 83 71 8 1 6 1

1 Philly 80 79 3 1 0 2

2 New York 78 78 6 6 0 0

3 Montreal 77 82 3 1 2 0

(69)

Q. Which teams have a chance of finishing the season with the most wins?

Detroit is mathematically eliminated.

Detroit finishes with ≤ 76 wins.

Wins for R = { NYY, BAL, BOS, TOR } = 278.

Remaining games among { NYY, BAL, BOS, TOR } = 3 + 8 + 7 + 2 + 7 = 27.

Average team in R wins 305/4 = 76.25 games.

Baseball elimination problem

i teamteam wins losses to play NYY BAL BOS TOR DET

0 New York 75 59 28 3 8 7 3

1 Baltimore 71 63 28 3 2 7 4

2 Boston 69 66 27 8 2 0 0

3 Toronto 63 72 27 7 7 0 0

4 Detroit 49 86 27 3 4 0 0

AL East (August 30, 1996)

(70)

Intuition. Remaining games flow from s to t.

Fact. Team 4 not eliminated iff all edges pointing from s are full in maxflow.

Baseball elimination problem: maxflow formulation

s g12 t

game vertices

(each pair of teams other than 4)

team vertices

(each team other than 4)

w4 + r4 – w2

1 0

3 2

0–2

0–3

1–3 0–1

2–3

1–2

games left between 1 and 2

team 2 can still win this many more games

(71)

Maximum flow algorithms: theory

(Yet another) holy grail for theoretical computer scientists.

year method worst case discovered by

1951 simplex E3 U Dantzig

1955 augmenting path E2 U Ford-Fulkerson

1970 shortest augmenting path E3 Dinitz, Edmonds-Karp 1970 fattest augmenting path E2 log E log(EU) Dinitz, Edmonds-Karp

1977 blocking flow E 5/2 Cherkasky

1978 blocking flow E 7/3 Galil

1983 dynamic trees E2 log E Sleator-Tarjan

1985 capacity scaling E2 log U Gabow

1997 length function E3/2 log E log U Goldberg-Rao

2012 compact network E2 / log E Orlin

? ? E ?

Referências

Documentos relacionados

O presente trabalho tem como objetivo caracterizar obras de Engenharia em uma cidade de pequeno porte (Palmeira, PR), verificando-se as etapas dos Projetos Arquitetônicos,

Em especial, a implementação de Políticas Públicas para a Agricultura Familiar veio conquistando uma efetividade no Brasil, nas duas últimas décadas, pela atuação em

Os materiais a serem desenvolvidos encontram aplicações em sistemas acumuladores de calor latente de fusão para aquecimento solar de água, utilizando os materiais de baixo ponto

A estratégia aqui proposta, para uma normalização robusta dos dados de expressão pelo volume, é colocar todas as amostras, de volumes diferentes, num mesmo domínio do volume

Efeitos dos teores de sódio aos 21 dias do período experimental sobre a Unidade Haugh UH, Espessura de casca EC, Peso específico PE, Porcentagem de casca PC e peso de casca por

Tabela 5 resume as principais características do facho principal (Technip, 1974). Tabela 5 - Principais características facho do sistema de flare 7200. Os efluentes de composição

In fact, there are several reasons for the scarcity of this type of document: firstly, they were not written at a specific administrative office (for example, the Chancery), but

All of the studied protocols are then introduced to a new omission fault model where temporary node failures occur. This model helps extend simulation scenarios to represent not