6.4 ford-fulkerson demo - depaul...

27
Algorithms, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · February 23, 2012 4:05:54 PM Algorithms F O U R T H E D I T I O N R O B E R T S E D G E W I C K K E V I N W AY N E 6.4 FORD-FULKERSON DEMO click to begin demo

Upload: others

Post on 25-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · February 23, 2012 4:05:54 PM

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

R O B E R T S E D G E W I C K K E V I N W A Y N E

6.4 FORD-FULKERSON DEMO

click to begin demo

Page 2: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

2

‣ Ford-Fulkerson algorithm‣ computing a min cut

Page 3: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

Initialization. Start with 0 flow.

Ford-Fulkerson algorithm

3

s t

0 / 4

0 / 40 / 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

Page 4: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

4

0 / 4

0 / 40 / 15

0 / 15

0 / 15 0 / 10

0 / 9

0 / 8

0 / 6

0 / 16

s t

0 / 10 0 / 15

0 / 100 / 5

1st augmenting path

0

0 / 10

Page 5: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

4

0 / 4

0 / 40 / 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 / 100 / 5

1st augmenting path

0

0 / 10

Page 6: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

4

0 / 4

0 / 40 / 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 / 100 / 5

1st augmenting path

0

0 / 10

bottleneck capacity = 10

Page 7: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

4

0 / 4

0 / 40 / 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

Page 8: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

5

0 / 5

0 / 4

0 / 4

0 / 15

0 / 15

0 / 10

0 / 9

0 / 8

0 / 6

10 / 10 10 / 15

10 / 10s t

0 / 15 0 / 10

0 / 16

2nd augmenting path

10

Page 9: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

5

0 / 5

0 / 4

0 / 4

0 / 15

0 / 15

0 / 10

0 / 9

0 / 8

0 / 6

10 / 10 10 / 15

10 / 10s t

0 / 15 0 / 10

0 / 16

0 / 15 0 / 10

0 / 16

2nd augmenting path

10

Page 10: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

5

0 / 5

0 / 4

0 / 4

0 / 15

0 / 15

0 / 10

0 / 9

0 / 8

0 / 6

10 / 10 10 / 15

10 / 10s t

0 / 15 0 / 10

0 / 16

0 / 15 0 / 10

0 / 16

10

10

10

2nd augmenting path

10 + 10 = 20

Page 11: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

6

0 / 4

0 / 4 0 / 150 / 6

10 / 10

10 / 10

10 / 15 10 / 10

10 / 16

s t0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

3rd augmenting path

20

0 / 15

Page 12: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

6

0 / 4

0 / 4 0 / 150 / 6

10 / 10

10 / 10

10 / 15 10 / 10

10 / 16

s t0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

3rd augmenting path

20

0 / 15

backward edge

(not empty)

Page 13: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

6

0 / 4

0 / 4 0 / 150 / 6

10 / 10

10 / 10

10 / 15 10 / 10

10 / 16

s t0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

3rd augmenting path

20 + 5 = 255

5

5—

5—

5—

0 / 15

backward edge

(not empty)

Page 14: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

7

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5s t

0 / 6

5 / 10

5 / 9

5 / 8

5 / 15

10 / 1010 / 15

10 / 16

4th augmenting path

25

0 / 15

Page 15: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

7

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5s t

0 / 6

5 / 10

5 / 9

5 / 8

0 / 6

5 / 10

5 / 9

5 / 8

5 / 155 / 15

10 / 1010 / 15

10 / 15

10 / 1610 / 16

4th augmenting path

25

0 / 15

backward edge

(not empty)

Page 16: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

7

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5s t

0 / 6

5 / 10

5 / 9

5 / 8

0 / 6

5 / 10

5 / 9

5 / 8

5 / 155 / 15

10 / 1010 / 15

10 / 15

10 / 1610 / 16

4th augmenting path

25 + 3 = 288

2—

8—

8—

13—

13

3—

0 / 15

backward edge

(not empty)

Page 17: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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

• Full forward edge.

• Empty backward edge.

Idea: increase flow along augmenting paths

8

no more augmenting paths

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5s t

3 / 6

8 / 10

8 / 9

8 / 8

2 / 15

10 / 1013 / 15

28

13 / 16

0 / 15

full forward edge

empty backward edge

Page 18: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

9

‣ Ford-Fulkerson algorithm‣ computing a min cut

Page 19: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

s

13 / 156 / 6

3 / 4

A

Page 20: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 156 / 6

3 / 4

A

Page 21: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 6

3 / 4

A

Page 22: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 6

3 / 4

A

Page 23: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

backward edge

(not empty)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 6

3 / 43 / 4

A

Page 24: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

backward edge

(not empty)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 6

3 / 43 / 4

A

Page 25: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

backward edge

(not empty)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 66 / 6

3 / 43 / 4

A

Page 26: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

backward edge

(not empty)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 66 / 6

3 / 43 / 4

A

Page 27: 6.4 FORD-FULKERSON DEMO - DePaul Universityfpl.cs.depaul.edu/jriely/ds1/extras/demos/64DemoFordFulkerson.pdf · Initialization. Start with 0 flow. Ford-Fulkerson algorithm 3 s t 0

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 pathwith no full forward or empty backward edges.

Computing a mincut from a maxflow

10

forward edge

(not full)

backward edge

(not empty)

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 66 / 6

3 / 43 / 4

full forward edge

empty backward edge

A