37
Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ.

Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Embed Size (px)

Citation preview

Page 1: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Chapter 26 of CLSRMaximum Network

Flowby me Dr. M. Sakalli

Ch26 of CSLR and many other sources.

Marmara Univ.

Page 2: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

A Flow Network, terminology• Modeled as a flow graph which is a directed

graph of G=(V,E).

• Flow – a function f : V V R: Rate passing through the network from the source vertex s to the sinking vertex t. In literature called as s-t network. Net flow, |f(u,v)|.

• Source is vertex from which all the edges are leaving ‘s’, and sink is the vertex, all the edges incident to ‘t’ .

2/5

2/15

5/14

4/19

3/3

s t0/9

a

b

Page 3: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

A Flow Network, ConstraintsA flow is an assignment of real numbers xij to edges (i,j) of a given network

that satisfy the following: flow-conservation requirements and capacity constraints

• Capacity constraint: Each edge has a maximum capacity ‘ce’ allowed to transmit through such that.. Net flow cannot exceed ce… u, v V: f(u, v) c(u, v)

• Skew symmetry constraint: u, v V: f(u,v) = –f(v,u)• Flow conservation Law: The sum of the net flow incident to a vertex is

equal to the net flow leaving the same vertex. u, v V – {s, t}, ΣvVf(u, v)=0. for entire vertices. – What goes in = what leaves out: ΣvVf(u, V)- ΣvVf(V, v) =0, – s and t vertices:

ΣvVf(v, s)=0, ΣvVf(t, v)=0, ΣvVf(s, v) = ΣvVf(v, t)>0 and R+,

• No leakage and no memory is allowed. • Total value of flow, |f| = f(s, V) = f(V, t)

• Among the areas applied are traffic, freight, airflow, hydrodynamic, communications, and stocks, companies.

• The Maximum Flow Problem Definition: is to find a feasible flow through a single-source, single-sink flow network that is maximum.

2/5

2/15

5/14

4/19

3/3

s t0/9

a

b

Page 4: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• Opposite flows bt two vertices (due to skew symmetry) will cancel each other.

• Super source and super sink connected with infinite capacity.

• Find paths to maximize |f|. • f(X, Y) means ΣxXΣyYf(x, y) • f(u, V – {s, t}) = 0

Lucky Puck Distribution Network Max Flow, |f| = 19Or is it?Best we can do?

2/13

3/11

1/51/4

3/15

1/10

1/14

2/19

2/3

9s

a b

c d

t

Page 5: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• For X, Y, Z V with X Y =,• f(X Y, Z) = f(X, Z) + f(Y, Z) flow between any x and y

through Z….• f(Z, X Y) = f(Z, X) + f(Z, Y) • The value of a flow is the total net flow into the sink; that

is, |f| = f(V, t), and from the source s |f| = f(s, V) (by definition)

= f(V-{V-s}, V) = f(V, V) - f(V - s, V) = f(V, V - s)

|f| = f(V, t) = f(V, t) + f(V, V - s - t) = f(V, t) (by flow conservation)

Cancellation of flows

5/5

2/15

5/14

5/19

2/3

s t2/9

a

b

3/5

2/15

5/14

5/19

2/3

s t0/9

a

b

Page 6: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Maximum Flow as LP

..

Page 7: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Minimum Cost Flow

..

Page 8: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Ford-Fulkerson method• Initialize flow value to 0. Start to iterate (dynamic or greedy!).. • Find augmenting paths

– Find a path p from s to t, such that there is some flow value x > 0, and for each edge (u, v) in p, add x units of flow to |f|, such that max{x} min{cf(u, v): (u, v) is on p}.

– Increment flow, f(u, v) = f(u, v) + x • Calculate residual capacities: cf(u,v) = c(u,v) – x

i.e. the actual capacity minus the net flow (x) from u to vFlow may be negative!! That can be interpreted as increasing residual capacity.

• Residual network: Gf =(V, Ef), where Ef = {(u,v) V V : cf(u,v) > 0}

• Continue till no any augmenting path (any forward channel) left through • Observation – edges in Ef are either edges in E or their reversals: |Ef| 2|E|

ba

c d

s

13

11

54

1510

14

19

3

t9

Augmenting Path?

Page 9: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Ford-Fulkerson method• Contains several algorithms:

– Residue networks– Augmenting paths

• Find a path p from s to t (augmenting path), such that there is some value x > 0, and for each edge (u,v) in p we can add x units of flow

– f(u,v) + x c(u,v)

8/13

8/11

5/52/4

10/15

10

6/14

13/19

3/3

s t9

a b

c d

Augmenting Path?

Page 10: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Residual Network

• To find augmenting path we can find any path in the residual network:– Residual capacities: cf(u,v) = c(u,v) – f(u,v)

• i.e. the actual capacity minus the net flow from u to v

• Net flow may be negative

– Residual network: Gf =(V,Ef), where

Ef = {(u,v) V V : cf(u,v) > 0}

– Observation – edges in Ef are either edges in E or their reversals: |Ef| 2|E|

0/14

5/15

a b

19

10

a b

Sub-graphWithc(u,v) andf(u,v)

ResidualSub-Graph

c

5/6

c

1

5

Page 11: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Residual Graph• Compute the residual graph of the graph with the

following flow:8/13

8/11

5/52/4

10/15

10

6/14

13/19

3/3

s t9

a b

c d

Page 12: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Residual Capacity and Augmenting Path

• Finding an Augmenting Path– Find a path from s to t in the residual graph– The residual capacity of a path p in Gf:

cf(p) = min{cf(u,v): (u,v) is in p}• i.e. find the minimum capacity along p

– Doing augmentation: for all (u,v) in p, we just add this cf(p) to f(u,v) (and subtract it from f(v,u))

– Resulting flow is a valid flow with a larger value.

Page 13: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Residual network and augmenting path

f(p1:(s, v1),…) = 11, as far as cf(s,v1)<16, and as far as sum v1=0, and f(p2:(s, v2),…), calculate final residual graph Gf, and next search augmentation path.

Page 14: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

The Ford-Fulkerson method

Ford-Fulkerson(G,s,t) 1 for each edge (u,v) in G.E do 2 f(u,v) f(v,u) 0 3 while there exists a path p from s to t in residual

network Gf do4 cf = min{cf(u,v): (u,v) is in p} 5 for each edge (u,v) in p do6 f(u,v) f(u,v) + cf

7 f(v,u) -f(u,v)8 return f

The algorithms based on this method differ in how they choose p in step 3.Steps 1, 2, O(E), and 4-7 O(E) times for each unit of |f*|. If chosen poorly the algorithm might not terminate.

Page 15: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Execution of Ford-Fulkerson (1)

Left Side = Residual Graph Right Side = Augmented Flow

Page 16: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Execution of Ford-Fulkerson (2)

Left Side = Residual Graph Right Side = Augmented Flow

Page 17: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Path augmenting scenarios

Fig 27.4 of CLSR: The flow network G with f. The residual network G f with augmenting path p shaded; its residual capacity is cf(p) = c(v2, v3) = 4. (c) The flow in G that results from augmenting along path p by its residual capacity 4. (d) The residual network induced by the flow in (c).

Page 18: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

The Ford-Fulkerson algorithm, Residual Capacity and Augmenting Path

In each iteration find any path augmenting the flow along by the residual capacity câ(p). Following algo computes the max f in G = (V, E) by updating the net â[u, v] between connected vertices; u, v. If not connected, implicit assumption is that â[u, v] = 0. Assumption: the c(u, v) is a constant-time function, which is 0 if (u, v) E.

c(u, v) might be derived from fields stored within vertices and their adjacency lists. The residual capacity: câ(u, v).. Here the câ(p) is a temporary variable for the câ(u, v) of the path p. () refers a function [] refers a mutable identifier.

Ford-Fulkerson(G,s,t) 1 for each edge (u,v) G.E do //Initializing 2 f(u,v) f(v,u) 0 3 while path p from s to t in residual network Gf do ***4 cf = min{cf(u,v): (u,v) p} // determining augmentation5 for each edge (u,v) p do // updating residual capacities, new Gf 6 f(u,v) f(u,v) + cf

7 f(v,u) -f(u,v)8 return fT(n) depends on how line 3 is implemented and the algorithms based on this differ

in how to determine p. If chosen poorly the algorithm might not terminate.http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/Maxflow.shtml

Page 19: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Execution

Residual Graphs Augmented Flow

ba

c d

s

12

14

74

169

13

20

4

t10

Page 20: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Execution of Ford-FulkersonResidual Graph Augmented Flow

Observe that terminates when there is no a path from s to t whose all edges are in forward direction. And observe that the edges whose capacities are depleted form bottleneck, which is the minimum cut and their total capacity of the maximum achivible.

ba

c d

s

12

3/11

75/11

9

13

1/19

4

t3/11

Page 21: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• Does the method find the maximum flow? Yes, if program can get to the point where the residual graph has no path from s to t. Which is the moment when the G(V, E) is partitioned into two disjoint sets, by a definite split, minimum cut, c(S, V-S) = c(S, T), such that s S and t T.

• And the net flow (f(S,T)) through the cut (or the capacity) is the total capacity of the flows f(u,v), where s S and t T which includes negative flows back from T to S as well. Think about it from the graph…

• Minimum cut – a cut with the smallest capacity of all cuts.|f|= f(S,T) i.e. the value of a max flow is equal to the capacity of a min cut.

8/13

8/11

5/52/4

10/15

10

6/14

13/19

3/3

s t9

a b

c dCut capacity = 24 Min Cut capacity = 21

Page 22: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Cut properties.. • A cut (S, T) of flow network G = (V, E) is a partition (or a

disjoint set) of V into S and T = V - S such that s S and t T. MST cut was for undirected graphs, here a directed graph is cut such that (tails) s S and (heads) t T.) If f is a flow, then the net flow across the cut (S, T) is defined to be â(S, T). The capacity of the cut (S, T) is c(S, T).

• f(S, T) = ΣuSΣvTf(u, v) ΣuSΣvTc(u, v) = c(S, T).

• The value of any flow â in a flow network G is bounded from above by the capacity of any cut of G.

• Proof Let (S, T) be any cut of G and let â be any flow. By |â| = â(S, T) (Lemma 27.5 clrs) and the capacity constraints,

• = ΣuSΣvTf(u, v) ΣuSΣvTc(u, v) = c(S, T)

Page 23: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Max-flow min-cut theoremIf f is a flow in a flow network G=(V, E) with source s

and sink t, then the following conditions are equivalent:

1. f is a maximum flow in G.

2. The residual network Gf contains no augmenting paths.

3. Flow value of f = c(S, T) for some cut (S,T) of G. (This cut is called min-cut. Bottleneck. Among all the cuts of G(S, T), it is the cut that returns the minimum capacity.)

Page 24: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Proof1→2 Use contradiction. If it does not hold, then the

residual network Gf must have some augmenting paths. So the net flow |f| could be further increased which contradicts to that |f| is a maximum flow in G.

2→3 Suppose that Gf has no augmenting path, that is, that Gf contains no path from s to t. Define S={vV: there exists a path from s to v in Gf} and T=V-S. The partition (S,T) is a cut: we have s S trivially and t T because there is no path from s to t in Gf. For each pair of vertices u and v such that uS and vT, we have f(u,v)=c(u,v) since otherwise (u,v)Ef, which would place v in set S. Thus, flow value of f=c(S,T). (Lemma: flow value f=f(S,T))

Page 25: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

The end of Proof

3→1 The value of any flow f in a flow network G is bounded from above by the capacity of any cut of G.

flow value f(S, T)=ΣuSΣvTf(u, v) ΣuSΣvTc(u, v) = c(S, T).

so the cut capacity c(S,T) is the upper bound of flow f. Now the flow value f=c(S,T) thus implies that f is a maximum flow.

Page 26: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Worst case analysis• In the worst case, suppose that at

each step Ford-Fulkerson’s algorithm performs some small incremental (integer) augmentations of ∆f, then the number of iterations to reach to the maximum flow, |f*|/∆f, where f* is maximum flow.

• In the given example (clrs) programs starts to traverse through path 1, suvt and alternates to the path 2, svut. instead of completing the algorithm in two steps.

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

ts

v

u

1/1

10000001000000

1000000 1000000

1

ts

v

u

1

1000000-1/11000000-1/1

1/1000000-1 1/1000000-1

1

ts

v

u

11/1000000-1

1000000-1/1

1000000

1000000

2

Page 27: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• If searching path p in line 3, is implemented with a BFS in the residual network, and if the augmentation path is a shortest path from s to t, regardless of the weights of edges,

• Edmonds-Karp algorithm. O(VE2), take shortest augmentation path.• How do we find such a shortest path? DFS or BFS..

• The number of augmenting paths needed by the shortest-augmenting-path algorithm never exceeds nm/2, where n and m are the number of vertices and edges, respectively

• Since the time required to find shortest augmenting path by breadth-first search is in O(n+m)=O(m) for networks represented by their adjacency lists, the time efficiency of the shortest-augmenting-path algorithm is in O(VE2) or O(nm2) for this representation.

• Running time O(VE2), because the number of augmentations is O(VE)

• More efficient algorithms have been found that can run in close to O(nm) time• BFS and AP and Maximum Capacity Path (MCP) - find an A.P. that maximizes

incremental flow

• How many augmenting paths for each method?• Edmonds-Karp - A.P.s to give overall time.• Critical edge on A.P. No capacity will remain on a critical edge after A.P. is recorded • Observations: Edge may become critical several times. A vertex cannot get closer to source

in later rounds of BFS.

Edmonds-Karp algorithm

Page 28: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• Each augmentation has complexity O(E)• Numbers of augmentations is O(VE)

– Each edge can be a critical edge at most |V|/2-1 times

– Once edge is critical, its flow is saturated– Must appear in path the other way next– Since previous path is shortest, this reversing

path must be two edges longerdf’(s,v)=df(s,u)+1df’(s,u)=df’(s,v)+1

≥df(s,v)+1 (paths increase monotonically) =df(s,u)+2

s a b t

Dist i Dist i+1

s a b t

Dist > i+2 Dist > i+1

First time critical

Later

s a b t

Dist > i+2 Dist > i+3

Second time critical

The number of distances available for the tail of a repeated critical edge is (V-2)/2. The number of edges that become critical is < E. Thus, A.P.s overall.Flow must be sent in opposite direction by another A.P. before an edge can become critical again.

Page 29: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Shortest-Augmenting-Path Algorithm

Generate augmenting path with the least number of edges by BFS.

Start at s, perform BFS traversal by marking new (unlabeled) vertices with two labels and a sign mark:

• 1st label – indicates the amount of additional flow that can be brought from the previous to the current vertex being labeled.• 2nd label – indicates the vertex from which the vertex being labeled was reached, with “+” or “–” in forward or in backward, respectively.

• Vertex labeling: The start is always labeled with ∞,-

• All other vertices are labeled as follows: If unlabeled vertex j is connected to the front vertex i with the edge

– from i to j with unused capacity rij = uij –f(uij, vij) (forward edge), vertex j is labeled with lj, i+, where lj = min{li, rij}.

– from j to i with positive flow xji (backward edge), vertex j is labeled lj,i-, where lj = min{li, xji}

Page 30: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• If the sink ends up being labeled, then there is an augmentation path by the amount min{cp} which indicated is with the sink’s first label.

• The augmentation path is traced along the path from sink to source; the current flow quantities are increased on the forward edges and decreased on the backward edges of this path.

• If the sink remains unlabeled after the traversal queue becomes empty, the algorithm returns the current flow as maximum and terminates.

s

4

32

5

t0/2 0/2

0/3 0/1

0/5

0/3 0/4

s

4

32

5

t0/2 0/2

0/30/1

0/5

0/30/4

∞,- 2,1+

2,2+

2,3+2,2+

3,1+

Queue: 1 2 4 3 5 6 ↑ ↑ ↑ ↑

Augment the flow by 2 (the sink’s first label) along the path 1→2→3→6

Page 31: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

s

4

32

5

t2/2 2/2

0/3 0/1

2/5

0/3 0/4

s

4

32

5

t2/2 2/2

0/3 0/1

2/5

0/3 0/4

∞,- 1,3-

1,2+

1,5+

1,4+

3,1+4

Augment the flow by 1 (the sink’s first label) along the path 1→4→3←2→5→6

Queue: 1 4 3 2 5 6 ↑ ↑ ↑ ↑ ↑

s

4

32

5

t2/2 2/2

1/3 1/1

1/5

1/3 1/4

s

4

32

5

t2/2 2/2

1/3 1/1

1/5

1/3 1/4∞,-

2,1+

No augmenting path (the sink is unlabeled) the current flow is maximum

Queue: 1 4 ↑ ↑

Page 32: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

• S : (∞, 1, +)• B : (S, 8, +)• E : (S, 28, +)• F : (S, 15, +)• A : (B, 8, +)• C : (B, 8, +)• D : (B, 6, -)• T : (A, 8, +)• f updated by 8.P = (S,B, A, T)

Page 33: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Multiple Sources or Sinks

• What if you have a problem with more than one source and more than one sink?

• Modify the graph to create a single supersource and supersink

13

11

54

15

10

14

13

3

s t9

a b

c d13

11

54

15

10

14

13

3

x y9

e f

g h

4

13

11

54

15

10

14

13

3

9

a b

c d13

11

54

15

10

14

13

3

9

e f

g h

4s

i

j

k

l

t

Page 34: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Application – Bipartite Matching

• Example – given a community with n men and m women

• Assume we have a way to determine which couples (man/woman) are compatible for marriage– E.g. (Joe, Susan) or (Fred, Susan) but not (Frank,

Susan)

• Problem: Maximize the number of marriages– No polygamy allowed– solve this problem by creating a flow network out of a

bipartite graph

Page 35: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Bipartite Graph

• A bipartite graph is an undirected graph G=(V,E) in which V can be partitioned into two sets V1 and V2 such that (u,v) E implies either u V1 and v V2 or vice versa.

• That is, all edges go between the two sets V1 and V2 and not within V1 and V2.

Page 36: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Model for Matching Problem

• Men on leftmost set, women on rightmost set, edges if they are compatible

A

B

C

D

X

Y

Z

Men Women

A

B

C

D

X

Y

Z

A matching

A

B

C

D

X

Y

Z

Optimal matching

Page 37: Chapter 26 of CLSR Maximum Network Flow by me Dr. M. Sakalli Ch26 of CSLR and many other sources. Marmara Univ

Solution Using Max Flow

• Add a supersouce, supersink, make each undirected edge directed with a flow of 1

A

B

C

D

X

Y

Z

A

B

C

D

X

Y

Z

st

Since the input is 1, flow conservation prevents multiple matchings