fundamental algorithmsfaculty.baruch.cuny.edu/asheffer/courses/algs/08. network flow.pdf · nuclear...

43
4/10/2020 1 Fundamental Algorithms Topic 8: Network Flow By Adam Sheffer The RAND Corporation American think tank composed of scientists. In the 1950’s helped decision concerning the nuclear race, space program, etc. Contributed to the development of many scientific techniques, such as game theory. John Nash John von Neumann Dr. Strangelove 1 2

Upload: others

Post on 19-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

1

Fundamental AlgorithmsTopic 8: Network Flow

By Adam Sheffer

The RAND Corporation

American think tank composed of scientists.

In the 1950’s helped decision concerning the nuclear race, space program, etc.

Contributed to the development of many scientific techniques, such as game theory.

John Nash John von Neumann Dr. Strangelove

1

2

Page 2: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

2

A document that was declassified in 1999 describes how RAND studied the Soviet train system.

They studied the Soviet ability to transport things from place to place (e.g., Asian side to European side).

Flow Networks

flow network – directed graph 𝐺 = 𝑉, 𝐸 , together with a source vertex 𝑠, a sinkvertex 𝑡, and a capacity function 𝑐: 𝐸 → ℕ.

s t

a

b

c

d

eSource

7

3

1

1

1

3

5

3

5

5 Sink

Capacity

3

4

Page 3: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

3

Flow in a Network

Given flow network 𝐺 = (𝑉, 𝐸, 𝑠, 𝑡, 𝑐), a flow in 𝐺 is a function 𝑓: 𝐸 → ℕ that satisfies

◦ Every edge 𝑒 satisfies 0 ≤ 𝑓 𝑒 ≤ 𝑐 𝑒 .

◦ Every vertex 𝑣 except for 𝑠, 𝑡 satisfies

𝑢,𝑣 ∈𝐸

𝑓 𝑢, 𝑣 =

𝑣,𝑤 ∈𝐸

𝑓 𝑣, 𝑤

Total flow entering 𝑣.

Total flow exiting 𝑣.

Example: Flow

s t

a

b

c

d

e

2/7

The capacities are in red.

The flow is in blue.

Intuition: Water flowing in pipes.

2/3

2/5

0/3

0/1

2/3

1/1

1/1

1/5

3/5

5

6

Page 4: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

4

What is it Good For?

Transportation networks

Pipe Network analysis Ecological food webs

Finding maximum matchings

Size of a Flow

In any flow 𝑓, the flow coming out of 𝑠 is equal to the flow getting into 𝑡, since flow cannot stop at other vertices.

◦ We refer to this quantity as the size of 𝑓 or as 𝑓 .

s t

a

b

c

d

e

2/7

2/3

2/5

0/3

2/3

1/1

1/1

1/5

3/5

𝑓 = 4

0/1

7

8

Page 5: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

5

Maximum Flow

We are usually interested in finding the maximum flow of a network.

We wish to have an algorithm that receives a flow network, and finds a maximum flow of it.

s t

a

c

5 36

75

4db

2 4

3

A First Flow

s t

a

c

5 36

75

4db

2 4

Find a path from 𝑠 to 𝑡.

◦ For example, the path 𝑠 → 𝑏 → 𝑑 → 𝑡.

◦ How much flow can pass through this path?

◦ A Beginner’s Mistake: Make sure not to sum up the edge capacities!

3

9

10

Page 6: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

6

A First Flow

s t

a

c

5 36

75

4db

2 4

Find a path from 𝑠 to 𝑡.

◦ For example, the path 𝑠 → 𝑏 → 𝑑 → 𝑡.

◦ The flow that can pass in this path is the minimum edge capacity: 2.

3

Find a Larger Flow

We found a flow of size 2.

s t

a

c

0/5

0/3

0/30/6

0/70/5

db 2/2 2/42/4

What is the largest flow you can find?

11

12

Page 7: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

7

Saturated Edges

An edge 𝑒 is saturated if 𝑓 𝑒 = 𝑐 𝑒 .

We cannot pass any additional flowthrough a saturated edge.

Saturated edges in example: only 𝑏, 𝑑 .

s t

a

c

0/5

0/3

0/30/6

0/70/5

db 2/2 2/42/4

Edges with Some Flow

An edge can have some flow through it but not be saturated.

We can pass 2 additional units of flow through the edge (𝑠, 𝑏).

s t

a

c

0/5

0/3

0/30/6

0/70/5

db 2/2 2/42/4

13

14

Page 8: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

8

Increasing the Flow

We wish to increase an existing flow.

Find another path from 𝑠 to 𝑡, made of non-saturated edges.

◦ For example, the path 𝑠 → 𝑎 → 𝑡.

◦ Additional flow that can pass in this path: 3.

s t

a

c

0/5

0/3

0/30/6

0/70/5

db 2/2 2/42/4

Maximum Flow?

We keep increasing the flow until there are no more paths of unsaturated edges.

Does that mean that we have a maximum flow?

◦ No! This network has a flow of size 10.

s t

a

c

4/5

3/3

3/31/6

1/72/5

db 2/2 4/42/4

15

16

Page 9: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

9

A Different Kind of Path

s t

a

c

4/5

3/3

3/31/6

1/72/5

db 2/2 4/42/4

s t

a

c

5/5

3/3

3/32/6

0/73/5

db 2/2 4/42/4

We can go in the opposite direction of an edge 𝑒, if 𝑓 𝑒 > 0.

𝑠 → 𝑎 → 𝑑→ 𝑐 → 𝑡.

Going in the Wrong Direction

Why can we go in the opposite direction of an edge, cancelling its flow?

◦ First, 4 units enter 𝑑 and 4 units leave 𝑑.

◦ We increase the flow in 𝑎, 𝑑 and decrease the flow in 𝑐, 𝑑 .

◦ One again, 4 units enter 𝑑.

◦ Still a valid flow!

s t

a

c

4/5

3/3

3/31/6

1/72/5

db 2/2 4/42/4

17

18

Page 10: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

10

Residual Network

Given a flow network (𝑉, 𝐸, 𝑠, 𝑡, 𝑐), and a flow 𝑓, the residual network of 𝑓 has

◦ The same vertex set 𝑉, source 𝑠, and sink 𝑡.

◦ Every edge 𝑒 ∈ 𝐸 has the new capacity 𝑐′ 𝑒 = 𝑐 𝑒 − 𝑓 𝑒 .

◦ For every edge (𝑢, 𝑣) ∈ 𝐸, we add the edge 𝑣, 𝑢 with capacity 𝑓 𝑒 .

Example: Residual Network

s t

a

c

2/5

3/3

3/3

2/5

1/1

s t

a

c

3

3

3

31

A networkand a flow

The residualnetwork

2

2

19

20

Page 11: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

11

Build the Residual Network

s t

a

c

4/5

3/3

3/31/6

1/72/5

db 2/2 4/42/4

Residual Network of a Max Flow

Theorem.

◦ Consider a flow network (𝑉, 𝐸, 𝑠, 𝑡, 𝑐) and a flow 𝑓.

◦ 𝑓 is a maximum flow if and only if there is no path from 𝑠 to 𝑡 in the residual network of 𝑓.

◦ (We will not prove this theorem).

s t

a

c

3/5

3/3

3/3

3/5

0/1

21

22

Page 12: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

12

The Ford-Fulkerson Algorithm

The Ford-Fulkerson algorithm receives a flow network and finds a max flow.

Ford and Fulkerson developed it during their time in the Rand Corporation.

FulkersonFord

The Ford-Fulkerson Algorithm

Start with an empty flow 𝑓.

Repeat.

◦ Build the residual network of 𝑓.

◦ Find a path 𝑃 from 𝑠 to 𝑡 in the residual network.

If there is no such path, stop and return 𝑓.

◦ Set 𝑑 = min𝑒∈𝑃

𝑐(𝑒).

◦ Increase the flow in 𝑓 of every edge in 𝑃 by 𝑑(for reverse edges, decrease the flow by 𝑑).

23

24

Page 13: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

13

Run Ford-Fulkerson

s t

a

c

5 36

75

4db

2 4

3

State what path you found in each step and how much flow you added through it.

25

26

Page 14: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

14

Not Efficient

Ford-Fulkerson can have a VERY large running time:

s t

a

c

1/1

0/1010 1/1010

0/10101/1010

s t

a

c

0/1

1/1010 1/1010

1/10101/1010

s t

a

c

1/1

1/1010 2/1010

1/10102/1010

Number of Iterations

𝑓 – maximum flow.

Since each iteration increases the flow size by at least 1, the maximum number of iterations is 𝑓 (size of 𝑓).

s t

a

c

0/1

1/1010 1/1010

1/10101/1010

s t

a

c

1/1

1/1010 2/1010

1/10102/1010

27

28

Page 15: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

15

Running Time

Max number of iterations: 𝑓 .

How can we find a path in the residual network? Or conclude that there is no such path?

◦ Building the residual network: 𝑂 𝑉 + 𝐸 .

◦ Run BFS from 𝑠 in the residual network.

◦ Each such step takes 𝑂 𝑉 + 𝐸 .

Total running time: 𝑂 𝑉 + 𝐸 𝑓 .

Irrational Capacities

The missing capacities are all 𝑟 + 2.

Even though the size of the maximum flow is 2, Ford-Fulkerson might run forever!

◦ Not part of the material.

29

30

Page 16: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

16

Improved Running Time

A more careful analysis shows that the number of iterations is 𝑂 𝑉 𝐸 .

◦ Relies on the fact that BFS finds the shortest path in the residual network.

Usually called the Edmonds-Karp alg.

Gives a running time of O 𝑉 𝐸 2 .

◦ Not part of the material.

31

32

Page 17: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

17

Discuss in Groups

Recall. Two directed edges are said to be anti-parallel if they have the same endpoints, but are in opposite directions.

Problem. Consider a flow network (𝑉, 𝐸, 𝑠, 𝑡, 𝑐), and let 𝑒, 𝑒′ be anti-parallel edges. Explain why there exists a max flow in which at least one of 𝑒, 𝑒′ has no flow through it.

a

b

𝑒 𝑒′

Solution

Consider a maximum flow 𝑓.

◦ If either 𝑒 or 𝑒′ has no flow through it in 𝑓, we are done.

◦ Assume that 𝑓 𝑒 ≤ 𝑓(𝑒′).

◦ Decreasing the flow through both edges by 𝑓 𝑒 gives a valid flow of the same size.

Let 𝑒 = 𝑎, 𝑏 . Both the incoming and the outgoing flows of 𝑎 and 𝑏 were decreased by the same amount, so the flow constraints are still satisfied.

33

34

Page 18: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

18

Intuition

𝑎 𝑏 𝑎 𝑏

We can think of the flow as rerouted as illustrated below.

Bounding the Size of the Maximum Flow Given a flow network, what is an easy

upper bound for the size of the maximum flow?

𝑓 ≤ min

𝑠,𝑣 ∈𝐸

𝑐 𝑠, 𝑣 ,

𝑢,𝑡 ∈𝐸

𝑐 𝑢, 𝑡 .

s t

a

b

c

d

e

7

3

1

1

1

3

5

3

5

5

𝑓 ≤ 10

35

36

Page 19: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

19

Cuts

A cut is a partition of the vertices into two sets 𝑆, 𝑇 such that 𝑠 ∈ 𝑆 and 𝑡 ∈ 𝑇.

The size of a cut is the sum of the capacities of the edges from 𝑆 to 𝑇.◦ The figure shows a cut of size 1 + 1 + 5 = 7.

s t

a

b

c

d

e

7

3

1

1

1

3

5

3

5

5

Cuts and Maximum Flow

If there exists a cut of size 𝑑, no flow can be of size more than 𝑑.

◦ Each flow unit must pass through the cut.

◦ At most 𝑑 units can pass through the cut.

s t

a

b

c

d

e

7

3

1

1

1

3

5

3

5

5

37

38

Page 20: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

20

Minimum Cuts

A minimum cut is a cut of minimum size in the flow network.

◦ Is the cut in the figure a minimum cut?

◦ No! There is a cut of size 6.

s t

a

b

c

d

e

7

3

1

1

1

3

5

3

5

5

Find a Minimum Cut

s t

a

c

5 46

75

4db

2 4

4

39

40

Page 21: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

21

Max Flow – Min Cut

We saw that the size of maximum flow ≤size of minimum cut.

Max flow – min cut theorem. In every flow network, the size of the minimum cut is equal to the size of the maximum flow.

s t

a

b

c

d

e

7

3

1

1

1

3

5

3

5

5

Proof: Max Flow – Min Cut

Let 𝑓 be a max flow of the network.

In the residual network 𝑅 of 𝑓, there is no path from 𝑠 to 𝑡.

𝑆 – vertices that are accessible from 𝑠 in 𝑅.

𝑇 – The remaining vertices.

𝑆, 𝑇 is a cut.

𝑆 𝑇 s t

a

c

5/5

3/3

3/32/6

0/73/5

db 1/2 3/31/4

41

42

Page 22: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

22

The Cut 𝑆,𝑇 : Claim 1

Claim 1. An edge from 𝑢 ∈ 𝑆 to 𝑣 ∈ 𝑇must be saturated in 𝑓.

◦ Since 𝑢 ∈ 𝑆, there is a path from 𝑠 to 𝑢 in 𝑅.

◦ If 𝑢, 𝑣 is not saturated, there is a path from 𝑠 to 𝑣 in 𝑅. Contradicting that 𝑣 ∈ 𝑇!

𝑆 𝑇 s t

a

c

5/5

3/3

3/32/6

0/73/5

db 1/2 3/31/4

The Cut 𝑆,𝑇 : Claim 2

Claim 2. An edge from 𝑢 ∈ 𝑇 to 𝑣 ∈ 𝑆 has no flow through it in 𝑓.

◦ Since 𝑣 ∈ 𝑆, there is a path from 𝑠 to 𝑣 in 𝑅.

◦ If 𝑓 𝑢, 𝑣 > 0, there is a path from 𝑠 to 𝑢 in 𝑅. Contradiction to 𝑢 ∈ 𝑇.

𝑆 𝑇 s t

a

c

5/5

3/3

3/32/6

0/73/5

db 1/2 3/31/4

43

44

Page 23: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

23

Completing the Proof

𝑓 - maximum flow.

𝑆, 𝑇 – cut defined according to 𝑓.

◦ Every edge from 𝑆 to 𝑇 is saturated in 𝑓.

◦ Every edge 𝑒 from 𝑇 to 𝑆 satisfies 𝑓 𝑒 = 0.

Thus, the size of the cut (𝑆, 𝑇) is 𝑓 .

The minimum cut has size at most 𝑓 .

Size of min cut ≤ size of max flow.

Discuss in Groups

Problem. Design an algorithm that receives a flow network and finds a minimum cut.

◦ Rely on the proof we just saw.

𝑆 𝑇 s t

a

c

5/5

3/3

3/32/6

0/73/5

db 1/2 3/31/4

45

46

Page 24: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

24

Cut Size = Flow Size

Claim. A flow network has a flow 𝑓 and a cut 𝑆, 𝑇 , both of the same size. Then 𝑓is a max flow and 𝑆, 𝑇 is a min cut.

Proof.

◦ No flow can be larger then the size of 𝑆, 𝑇 . So 𝑓 is a max flow.

◦ No cut can be smaller than 𝑓 . So 𝑆, 𝑇 is a min cut.

An Application

In the Vietnam war, the North used a series of underground tunnels called the Ho Chi Minh trail.

The U.S. was looking for the min cut to efficiently disconnect the south and the north parts of the system.

The capacity of an edge is the difficulty of destroying it.

47

48

Page 25: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

25

Recall: The Rand corporation studied the Soviet train system.

They studied the Soviet ability to transport things from the Asian side to the European side.

They also studied the minimum cut.

Problem. In this map there are several sources and sinks!

The bottleneck

49

50

Page 26: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

26

Discuss in Groups

Problem. We are given a flow network with several sources and several sinks. Explain how to use an algorithm for finding a maximum flow (in a standard flow network) for this case.

1s

a

b

c

2t

1t1

2s

3s

1

5

55

3

33

3

11

Solution

We add a “super source” 𝑆, and add an edge from it to each of the sources. Each of these edges has an infinite capacity.

We symmetrically add a “super sink” 𝑇.

Run the original algorithm from 𝑆 to 𝑇.

1s

a

b

c

2t

1t

2s

3s

1

5

55

3

33

3

11

t

1

51

52

Page 27: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

27

No Infinite Capacities

How can we solve the problem without using infinite capacities?

Replace infinity with a very large number.

◦ For example, the sum of all edge capacities.

1s

a

b

c

2t

1t

2s

3s5

55

3

33

11

t

1

Correctness

A max flow in the new network corresponds to a max flow in the original network.

◦ We just remove the new edges and leave the rest as is.

1s

a

b

c

2t

1t

2s

3s5

55

3

33

11

t

1

53

54

Page 28: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

28

Discuss in Groups

Problem. Given a flow network 𝑉, 𝐸, 𝑠, 𝑡, 𝑐 such that all of the capacities

are even, prove that the size of the maximum flow is even.

The capacities are even.

Every cut has an even size.

The minimum cut has an even size.

The maximum flow has an even size.

Max flow – min cut

55

56

Page 29: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

29

Coming Soon: How to Improve your Chances of Getting a Math Ph.D.

Edge-disjoint Paths

Problem. Given directed 𝐺 = 𝑉, 𝐸 and vertices 𝑠, 𝑡 in 𝑉, describe an algorithm that finds the maximum number of edge-disjoint paths from 𝑠 to 𝑡.

s st t

57

58

Page 30: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

30

0-1 Networks

We give every edge a capacity of 1.

A network with only 1 capacities is called a 0-1 network.

There are algorithms for finding max flow in a 0-1 network with running times

𝑂 𝐸 3/2 and 𝑂 𝑉 2/3 𝐸 .

1\1

1\1 1\1

1\1

1\11\1

1\11\1

1\1

1\1 1\1

1\1

1\0

1\0

s t

Solution: Edge-disjoint Paths

We give every edge a capacity of 1.

Find max flow in resulting 0-1 network.

We then split the flow into edge-disjoint pathsarbitrarily.

1\1

1\1 1\1

1\1

1\11\1

1\11\1

1\1

1\1 1\1

1\1

1\0

1\0

s t

59

60

Page 31: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

31

Correctness of Solution

Given a 0-1 flow network with a max flow 𝑓 and max number of edge-disjoint paths 𝑘, we need to prove 𝑓 = 𝑘.

𝑓 ≥ 𝑘: By having a flow of 1 through every disjoint path, we obtain a flow of size 𝑘.

𝑓 ≤ 𝑘:

◦ Can be proved using induction.

◦ Not in the material of this course.

Running Time

Building flow network: 𝑂 𝑉 + 𝐸 .

Finding max flow: Either 𝑂 𝐸 3/2 or

𝑂 𝑉 2/3 𝐸 .

Split the flow into edge-disjoint paths arbitrarily.

◦ Repeat. Start in 𝑠 and travel across saturated edges to 𝑡.

◦ We cross every edge at most once: 𝑂 𝐸 .

Total time: 𝑂 𝐸 3/2 or 𝑂 𝑉 2/3 𝐸 .

61

62

Page 32: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

32

Want a Ph.D. in Math? Eat More Cheese!

Correlation: 0.910557

* Taken from http://tylervigen.com/

Perhaps RAND were also studying the minimum number of train tracks needed to be destroyed to prevent any transportation from the Asian side to the European side?

63

64

Page 33: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

33

Disconnecting Edges

Problem. Given directed 𝐺 = 𝑉, 𝐸 and vertices 𝑠, 𝑡 in 𝑉, describe an algorithm that finds the minimum number of edges to be removed so that no path from 𝑠 to 𝑡remains.

s t

Solution: Disconnecting Edges

Give every edge a capacity of 1, to obtain a 0-1 network.

Find max flow.

But that’s exactly what we did in the previous problem!

◦ Is there a difference between the problems?

1\1

1\1 1\1

1\1

1\11\1

1\11\1

1\1

1\1 1\1

1\1

1\0

1\0

s t

65

66

Page 34: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

34

Solution: Disconnecting Edges

Give every edge a capacity of 1, to obtain a 0-1 network.

Find max flow.

Already know: The max flow equals the max number of edge-disjoint paths.

It remains to show that the max number of edge-disjoint paths equals the min number of edges needed to disconnect 𝑠from 𝑡.

Proof

𝑘 – max number of edge disjoint paths.

ℓ – min number of disconnecting edges.

ℓ ≥ 𝑘: There are 𝑘 edge-disjoint paths, and we need to remove at least one edge from each.

ℓ ≤ 𝑘: The min cut is a set of disconnecting edges. By the max flow –min cut theorem, there are 𝑘 edges in themin cut.

67

68

Page 35: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

35

Menger’s Theorem

The idea that the min number of disconnecting edges is equal to the max number of edge-disjoint paths is called Menger’s Theorem, and is from 1927.

Ich heisse Karl und ich mag Flüsse in

Netzwerken

Want a Ph.D. in Math? Spend more Money on Pets!

Correlation: 0.939826

* Taken from http://tylervigen.com/

69

70

Page 36: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

36

Discuss in Groups

Problem. Given a flow network 𝑉, 𝐸, 𝑠, 𝑡, 𝑐 and an edge 𝑒 in 𝐸, describe

an algorithm for checking whether 𝑒 is in at least one min cut of the network.

s t

a

b

c

d

e

7

3

1

1

1

3

5

3

5

5

Reminder: Matchings

A matching of an undirected graph 𝐺 is a set of vertex-disjoint edges of 𝐺.

The size of a matching is the number of edges in it.

A maximum matching of 𝐺 is a matching of maximum size.

71

72

Page 37: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

37

Discuss in Groups

Problem. Given a bipartite graph 𝐺 = 𝑉1, 𝑉2, 𝐸 , use a max flow algorithm to find a maximum matching of 𝐺.

𝑉2𝑉1

Flows and Bipartite Matchings

Add a source 𝑠 and edges from it to every vertex of 𝑉1. Similarly, add a sink 𝑡.

Direct edges to the right and set capacities to 1.

𝑉2𝑉1

𝑠 𝑡1 1 1

73

74

Page 38: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

38

Flows and Bipartite Matchings

There is a one-to-one correspondence between the matchings of 𝐺 and the flows.

A max matching corresponds to a max flow.

𝑉2𝑉1

𝑠 𝑡1 1 1

Maximum Matching Running Time

In the flow network, there are 𝑉 + 2vertices and 𝐸 + 𝑉 edges.

A specialized algorithm for this network

finds max flow in 𝑂 𝑉 1/2|𝐸| time.

◦ In a graph created as in the previous slide, it

suffices to have 𝑂 𝑉 1/2 iterations of

increasing the flow.

We can find max matching in a bipartite

graph in 𝑂 𝑉 1/2|𝐸| time.

Doing better is an important open problem.

75

76

Page 39: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

39

Machines and Tasks

Problem. We are given 𝑛 machines and 𝑚tasks that the machines need to do.

◦ The 𝑖’th machine performs at most 𝑛𝑖 tasks.

◦ Every machine has a subset of the tasks that it is able to do.

◦ Describe an algorithm for assigning all of the tasks (or stating that no assignment exists).

Solution

s t

MachinesTasks

1 1

1n

2n

3n

4n

We wish to find a flow of size 𝑚.

77

78

Page 40: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

40

Want a Ph.D. in Math? Get more Uranium!

Correlation: 0.952257

* Taken from http://tylervigen.com/

Indegree and Outdegree

Directed 𝐺 = 𝑉, 𝐸 .

◦ The indegree of a vertex 𝑣 ∈ 𝑉 is the number of edges that are directed into 𝑣.

◦ The outdegree of a vertex 𝑣 ∈ 𝑉 is the number of edges that are directed out of 𝑣.

Indegree: 2Outdegree: 1

79

80

Page 41: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

41

Subgraphs with Bounded Degrees

Problem. Given directed 𝐺 = 𝑉, 𝐸 , describe an algorithm for finding a subset of the edges 𝐸′ ⊂ 𝐸 so that every vertex of 𝑉 has an indegree and an outdegree of exactly 1 in 𝑉, 𝐸′ (or announce that such a set does not exist).

Solution

Build a bipartite graph 𝐺∗ = 𝑈,𝑊, 𝐸∗ so that 𝑉 = 𝑈 = 𝑊.

◦ Write 𝑉 = 𝑣1, … , 𝑣𝑛 , 𝑈 = 𝑢1, … , 𝑢𝑛 , and 𝑊 = 𝑤1, … , 𝑤𝑛 .

◦ For every edge 𝑣𝑖 , 𝑣𝑗 ∈ 𝐸, we add

𝑢𝑖 , 𝑤𝑗 ∈ 𝐸∗.

81

82

Page 42: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

42

Example: Building 𝐺∗

1v

2v3v

4v 5v

1u

2u

3u

4u

5u

1w

2w

3w

4w

5w

What should we do with the bipartite graph?

Solution

Build a bipartite graph 𝐺∗ = 𝑈,𝑊, 𝐸∗ so that 𝑉 = 𝑈 = 𝑊.

◦ Write 𝑉 = 𝑣1, … , 𝑣𝑛 , 𝑈 = 𝑢1, … , 𝑢𝑛 , and 𝑊 = 𝑤1, … , 𝑤𝑛 .

◦ For every edge 𝑣𝑖 , 𝑣𝑗 ∈ 𝐸, we add

𝑢𝑖 , 𝑤𝑗 ∈ 𝐸∗.

Check whether there exists a perfect matching in 𝐺∗.

◦ If so, return the edges of the matching.

◦ Otherwise, no valid subset exists.

83

84

Page 43: Fundamental Algorithmsfaculty.baruch.cuny.edu/ASheffer/courses/algs/08. Network Flow.pdf · nuclear race, space program, etc. Contributed to the development of many scientific techniques,

4/10/2020

43

Intuition

The out-degree of 𝑣𝑖 is the degree of 𝑢𝑖.

The in-degree of 𝑣𝑖 is the degree of 𝑤𝑖.

A perfect matching means that the degrees of all 𝑢𝑖’s and 𝑤𝑖’s are 1.

1v

2v3v

4v 5v

1u

2u

3u

4u

5u

1w

2w

3w

4w

5w

Recap

Eat cheese Spend money onpets

MoreUranium

85

86