Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

31
Lectures on Network Flows 1 Lectures on Network Flows COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski

Transcript of Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Page 1: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 1

Lectures on Network Flows

COMP 523: Advanced Algorithmic Techniques

Lecturer: Dariusz Kowalski

Page 2: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 2

Overview

Previous lectures:

• Dynamic programming– Weighted interval scheduling– Sequence alignment

These lectures:

• Network flows

• Applications: largest matching in bipartite graphs

Page 3: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 3

NetworkA directed graph G = (V,E) such that• each directed edge e has its nonnegative

capacity denoted by ce

• there is a node s (source) with no incoming edges

• there is a node t (target) with no outgoing edges

20

ts

10

u

v20

10

30u,v - internalnodes

Page 4: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 5

Flows-t flow in G = (V,E) is a function f from E to R+

• capacity condition: for each e, 0 f(e) ce

• conservation condition: for each internal node v, ∑e in v f(e) = ∑e out v f(e)

• there is a node t (target) with no outgoing edges

Property: ∑e in t f(e) = ∑e out s f(e)

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

10

Flow:Network:

Page 5: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 7

Useful definitionsGiven s-t flow f in G = (V,E) and any subset of nodes S• f in(S) = ∑e in S f(e)• f out(S) = ∑e out S f(e) Property: f in(t) = f out(s)Example: f in(u,v) = f out(u,v) = 30

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

10

Flow:Network:

Page 6: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 8

Problem(s)• What is the maximum value of f in(t) (flow) for

a given graph G = (V,E) ?• How to compute it efficiently?

Assumption: capacities are positive integers.

Example: f in(t) = f out(s) = 30

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

10

Flow:Network:

Page 7: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 9

Residual graphAssume that we are given a flow f in graph G.

Residual graph Gf • The same nodes, internal and s,t• For each edge e in E with ce > f(e) we put weight

ce - f(e) (residual capacity)• For each edge e = (u,v) in E we put weight f(e)

to the backward edge (v,u) (residual capacity)

20

ts

10

u

v20

10

30

20

ts

0

u

v20

0

20

Flow:20

ts10

v

20

10

ResidualGraph:

u

1020

Network:

Page 8: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 10

Augmenting path & augmentationAssume that we are given a flow f in graph G, and

the corresponding residual graph Gf 1. Find a new flow in residual graph - through a path

with no repeating nodes, and value equal to the minimum capacity on the path (augmenting path)

2. Update residual graph along the path

20

ts

10

u

v20

10

30

Newflow:

20

ts10

v20

10

Newresidualgraph: u

2010

20

ts10

v20

10

1010

u

20

Network:

Page 9: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 12

Ford-Fulkerson Algorithm• Initialize f(e) = 0 for all e• While there is s-t path P in residual graph

– Augment f through path P and get new f and new residual graph

Augment f through path P :• Find minimum capacity on the path• Go through the path and modify weights

20

ts

10

u

v20

10

30

Newflow: 20

ts10

v20

10

Newresidualgraph:

u

2010

20

ts10

v20

10

1010

u

20

Network:

Page 10: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 13

AnalysisCorrectness:maximum flow - proof latertermination - each time the flow is an integer and advances by

at least 1 (assumption about integer capacities)Time: O(mC)• at most C iterations, where C is the value of the maximum

flow, m is the number of edges• each iteration takes O(m+n) steps - use DFS to find path P

20

ts

10

u

v20

10

30

Newflow: 20

ts10

v20

10

Newresidualgraph:

u

2010

20

ts10

v20

10

1010

u

20

Network:

Page 11: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 14

Reminder: Depth-First Search (DFS)Given a directed graph G = (V,E) of diameter D and the root nodeGoal: find a directed rooted spanning tree such that each edge in graph G

corresponds to the ancestor relation in the treeRecursive idea of the algorithm:Repeat until no neighbor of the root is free• Select a free out-neighbor v of the root and add the edge from root to v to

partial DFS• Recursively find a DFS’ in graph G restricted to free nodes and node v as the

root’ and add it to partial DFS

root

root’

root’’

Page 12: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 15

Implementing DFSStructures:

– Adjacency list

– List (stack) S

– Array Discovered[1…n]

Algorithm:

• Set S = {root}

• Consider the top element v in S– For each out-neighbor w of node v, if w is not Discovered then put w into the stack S and start the next

iteration for w as the top element

– Otherwise remove v from the stack, add edge (z,v) to partial DFS, where z is the current top element, and start the next iteration for z as the top element

Remark: after considering the out-neighbor of node v we remove this neighbor from adjacency list to avoid considering it many times!

rootroot’

root’’

Page 13: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 16

Flows vs. Cuts(A,B) - cut in graph G:• A,B is a partition of nodes, s in A, t in B

c(A,B) = ∑e out A c(e) = ∑e in B c(e) is the capacity of this cutProperty:Minimum cut is equal to the maximum flowExample:c(A,B) = 50

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

30

AB

Page 14: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 17

Max Flow vs. Min CutFor any set A containing s we proceed in 3 steps:• value(f) = ∑v in A ∑e out v f(e) - ∑v in A ∑e in v f(e)• value(f) = f out(A) - f in(A) • c(A,B) f out(A) - f in(A) = value(f) Conclusion:Min-cut value(f)Example: c(A,B) = 50 and f out(A) = 30

20

ts

10

u

v20

10

30

20

ts

10/10

u

v20

10/10

30/10

AB

Page 15: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 18

FF-algorithm gives Max-flowSuppose FF-algorithm stopped with flow f : • Directed DFS tree rooted in s does not contain t• It means that cut-capacity between nodes in DFS and

the remaining ones is 0 in residual graph• It follows that each edge in this cut has been reversed

by augmenting flow, which means that c(DFS,DFS’) = value(f)

• Using Min-cut value(f) we get that f is maximum

20

ts

10

u

v20

10

30

20

ts

10v

20

10Residualgraph:

u

2010DFS

Min-cut

Page 16: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 19

Conclusions

Network flow algorithms:– Ford-Fulkerson algorithm in time O(mC)– Correspondence between max-flows and min-cuts

Page 17: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 20

Exercises

READING:

• Chapter 7, Sections 7.1, 7.2, and 7.3

EXERCISES:

• Modify FF-algorithm to work in time

O(m2 log C)

• Find an augmentation scheme to guarantee time O(mn) in FF-algorithm independently from integer C

Page 18: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 21

OverviewPrevious lecture:

• Network flows

• Ford-Fulkerson algorithm

• Max-flows versus Min-cuts

This lecture:

• Rational and real capacities in network

• Applications: largest matching in bipartite graphs

• Application to disjoint paths problem

Page 19: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 22

NetworkGiven a directed graph G = (V,E) such that• each directed edge e has its nonnegative

capacity denoted by ce

• there is a node s (source) with no incoming edges

• there is a node t (target) with no outgoing edges

20

ts

10

u

v20

10

30u,v - internalnodes

Page 20: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 23

Flows-t flow in G = (V,E) is a function f from E to R+

• capacity condition: for each e, 0 f(e) ce

• conservation condition: for each internal node v, ∑e in v f(e) = ∑e out v f(e)

• there is a node t (target) with no outgoing edges

Property: ∑e in t f(e) = ∑e out s f(e)

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

10

Flow:Network:

Page 21: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 24

ProblemWhat is a maximum value f in(t) = f out(s) (flow) for a given graph G = (V,E) ?How to compute it efficiently?

Assumption: capacities are positive integers.

Example: f in(t) = f out(s) = 30

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

10

Flow:Network:

Page 22: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 25

Ford-Fulkerson Algorithm• Initialize f(e) = 0 for all e• While there is s-t path P in residual graph

– Augment f through path P and get new f and new residual graph

Augment f through path P :• Find minimum capacity on the path• Go through the path and modify weights

20

ts

10

u

v20

10

30

Newflow: 20

ts10

v20

10

Newresidualgraph:

u

2010

20

ts10

v20

10

1010

u

20

Network:

Page 23: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 26

Non-integer capacities• Rational capacities - rescale them to integers by

multiplying by the common multiply • Real capacities - difficult to handle:

– Min-cut = Max-flow

– FF-algorithm may work very slowly

20ts

10

u

v20

10

30

Newflow: 20

ts

10

v20

10

Newresidualgraph:

u

2010

20

ts10

v20

10

1010

u

20

Graph:

Page 24: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 27

Flows vs. Cuts(A,B) - cut in graph G:• A,B is a partition of nodes, s in A, t in B

c(A,B) = ∑e out A c(e) = ∑e in B c(e) is a capacity of the cutProperty:Minimum cut is equal to the maximum flowExample:c(A,B) = 50

20

ts

10

u

v20

10

30

20

ts

10

u

v20

10

30

AB

Page 25: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 28

Applications - largest matchingInput: bipartite graph G=(V,W,E)Goal: largest set of non-incident edges (with different

ends)Solution using flow algorithms:• Lets direct edges from V to W, introduce s connected

to all nodes in V, t connected from all nodes in W• Capacities are 1 for all edges• Max-flow is the largest matching

ts

Page 26: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 30

Applications - disjoint paths

Input: network graph G=(V,E)

Goal: largest set of edge-disjoint paths from s to t

Solution: Using flow algorithms, where each edge has capacity 1

ts

Page 27: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 31

Disjoint paths cont.Suppose that k is the largest number of edge-disjoint paths

from s to t. • It is also a flow:

– capacity condition is clear since we push flow with value 1 through each path, and

– conservation is satisfied since if a path comes into a node it also goes out

Conclusion: the largest number of edge-disjoint paths from s to t is not bigger than Max-flow

ts

Page 28: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 32

Disjoint paths cont.Suppose that x is the value of Max-flow produced by FF-algorithm. How to construct x edge-disjoint paths from s to t ? By induction on the number of edges in the flow.For 0 edges trivial (nothing to do, no even a path)Assume j edges in the flow. Take one of them (s,v) and continue going using

edges in the flow:• We go to t - done, since we have path, remove it from the graph and

continue by induction• We make a cycle - reduce the flow along the cycle to zero, and the obtained

is a flow having the same value but smaller number of edges - next continue by induction

ts

Page 29: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 33

ComplexityTime of FF-algorithm: O(mn) ( since C = O(n) )Time of extracting paths: each edge is considered

once while extracting one path, hence total time O(mn)

Total time: O(mn) Additional memory: O(m+n) for keeping paths

ts

Page 30: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 34

Conclusions

Network flow algorithms:– Rational capacities are easy to deal with– Real capacities are difficult to compute -

although we can alternatively check Min-cut– Application to the largest matching problem in

time O(mn) (n = C since capacities are 1)– Application to the edge-disjoint paths problem in

time O(mn) (n = C since capacities are 1)

Page 31: Lectures on Network Flows1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.

Lectures on Network Flows 35

Textbook and ExercisesREADING:

• Chapter 7, Sections 7.5, 7.6 and 7.7

EXERCISES:

• Find a network with real capacities for which FF-algorithm works very slowly

• Prove formally that FF-algorithm gives the largest matching in the last application

• Design the algorithm for finding the largest number of edge-disjoint paths from s to t in undirected network

• Design the algorithm for finding the largest number of node-disjoint paths from s to t in both directed and undirected networks