umass lowell computer science 91.503 analysis of algorithms prof. karen daniels fall, 2006

29
UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 5 Lecture 5 Wednesday, 10/4/06 Wednesday, 10/4/06 Graph Algorithms: Part 2 Graph Algorithms: Part 2

Upload: mariam-david

Post on 02-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006. Lecture 5 Wednesday, 10/4/06 Graph Algorithms: Part 2. Network Flow. Chapter 26. Basic Network Flow Concepts. 2. 3. 1. 1. 1. 3. 2. 3. Network Flow. edge weights. source. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

UMass Lowell Computer Science 91.503

Analysis of Algorithms Prof. Karen Daniels

Fall, 2006

UMass Lowell Computer Science 91.503

Analysis of Algorithms Prof. Karen Daniels

Fall, 2006

Lecture 5Lecture 5Wednesday, 10/4/06Wednesday, 10/4/06

Graph Algorithms: Part 2Graph Algorithms: Part 2

Page 2: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Network Flow Network Flow

Chapter 26Chapter 26

Page 3: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Basic Network Flow Concepts

Basic Network Flow Concepts

Page 4: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Network FlowNetwork Flow

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

weighted network

edge weights

Goal:Goal: compute second set of compute second set of edge weights <= capacities.edge weights <= capacities.

flow

direction

source

sink

flow in = flow out flow in = flow out

(except at source, sink) (except at source, sink)

2 3

2

3

3

1

1 1

Page 5: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Network Flow ApplicationsNetwork Flow Applications

• Distribution ProblemsDistribution Problems• move objects from place to place within networkmove objects from place to place within network• examples:examples:

• merchandisemerchandise• communicationscommunications• traffic flowtraffic flow

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Page 6: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Network Flow Applications (continued)Network Flow Applications (continued)

• Matching ProblemsMatching Problems• network represents ways to connect verticesnetwork represents ways to connect vertices• goal: choose connections to goal: choose connections to

• cover vertex setcover vertex set• only touch each vertex onceonly touch each vertex once

• examples:examples:• job placementjob placement• minimum-distance point matching (2 point sets)minimum-distance point matching (2 point sets)

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Page 7: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Network Flow Applications(continued)Network Flow Applications(continued)

• Cut ProblemsCut Problems• remove edges to cut network into >= 2 piecesremove edges to cut network into >= 2 pieces• examples:examples:

• network reliabilitynetwork reliability• cutting supply linescutting supply lines

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Page 8: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Flow DefinitionsFlow Definitions

• Flow network Flow network GG = ( = (VV, , EE))• directed graphdirected graph• each edge (each edge (u,vu,v) in ) in EE has capacity has capacity

cc((uu,,vv) >= 0) >= 0• every vertex is on some path from every vertex is on some path from

source source ss to sink to sink tt• GG is connected is connected• ||EE| >= || >= |VV| - 1| - 1

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

2 3

2

3

3

1

1 1

capacity

Page 9: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Flow PropertiesFlow Properties

• Flow in Flow in GG is is ff: : VVxxVV -> -> RR satisfying: satisfying:• Capacity Constraint:Capacity Constraint:• Skew Symmetry:Skew Symmetry:• Flow Conservation:Flow Conservation:

• f f ((uu,,vv) is net flow from vertex ) is net flow from vertex uu to vertex to vertex vv• positive flow entering vertex positive flow entering vertex vv::

• positive flow entering a vertex other than source or sink must = positive flow entering a vertex other than source or sink must = positive flow leaving the vertexpositive flow leaving the vertex

• Value of a flow Value of a flow ff is total net flow is total net flow out of sourceout of source::

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

),(),(, vucvufVvu

),(),(, uvfvufVvu

VvvuftsVu 0),(},{

Vvvsff ),(||

0),(

),(vuf

Vu vuf

VuvuftsVv 0),(},{

Page 10: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Flow Properties (continued)Flow Properties (continued)

• Lemma 26.1Lemma 26.1• Let Let GG=(=(VV, , EE) be a flow network, and let f be a flow ) be a flow network, and let f be a flow

in in GG. Then:. Then:

• Implicit summation notation:Implicit summation notation:

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

0),( XXfVX

),(),(, XYfYXfVYX

),(),(),(0,, ZYfZXfZYXfYXwithVZYX

),(),(),(0,, YZfXZfYXZfYXwithVZYX

Exercise: showExercise: show ),(|| tVff

Xx Yy

yxfYXf ),(),(

Page 11: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Flow Properties (continued)Flow Equilibrium

A = amount of flow into left set from outside right set

B = amount of flow out of left set (and not into right set)

C = amount of flow into right set from outside left set

D = amount of flow out of right set (and not into left set)

y = amount of flow into right set from left set

x = amount of flow into left set from right set

EQUILIBRIUM requires

A + x = B + y C + y = D + x

A + C = B + D

Page 12: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Controlling Network FlowControlling Network Flow

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

open switches along path < 0,1,3,5>open switches along path < 0,1,3,5> open switches along path < 0,2,4,5>open switches along path < 0,2,4,5>

change switch at 1 to change switch at 1 to redirect flow to fill 1-4; redirect flow to fill 1-4; add flow on < 0,2,3,5 > add flow on < 0,2,3,5 > (maxflow results)(maxflow results)

2 leave s

2 flow into t

3 leave s

3 flow into t

increase by 1reduce by 1

Page 13: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Augmenting Paths

Augmenting Paths

Page 14: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Augmenting Flow on a PathAugmenting Flow on a Path

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

increase flow in <0,2>

2

2

2

2

increase flow in <2,3> decrease flow in <1,3>

increase flow on <1,4>,<4,5>

11

1

1

2

flow increases from 3 to 4

Page 15: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Augmenting Path SequencesAugmenting Path Sequences

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Try #1

Try #2Try #3

Strategy: Keep trying until no augmenting path can be found. Same max flow in each case although strategy is greedy!

To show strategy always produces max flow, 3 key ideas:

1) augmenting paths2) residual networks3) cuts

Page 16: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Residual Networks

Residual Networks

Page 17: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Residual NetworksResidual Networks

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

flow network G and flow f

residual network Gf

and augmenting path

augmented flow in G

augmented flow in Gf

min capacity on augmenting path

26.3 26.1

residual capacity

flow = amount we can remove

(no 0 edges are shown)

Page 18: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Cuts

Cuts

Page 19: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

s-t Cuts-t Cut

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Disconnects source from sinkDisconnects source from sink

Page 20: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

s-t Cuts-t Cut

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

c(S,T)=29 c(S,T)=26 c(S,T)=24

“Negative” flow edges are excluded from cut capacity.

Minimum cut is cut (S,T) whose capacity is minimum over all s-t cuts of network.

Page 21: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

MaxFlow MinCut TheoremMaxFlow MinCut Theorem

• If If ff is a flow in a flow network is a flow in a flow network GG=(=(VV, , EE) ) with source with source ss and sink and sink tt, then, equivalently:, then, equivalently:• 1. 1. ff is a maximum flow in is a maximum flow in GG• 2. The residual network 2. The residual network GGff contains no contains no

augmenting pathsaugmenting paths• 3. | 3. | f f | = | = cc((SS,,TT) for some cut () for some cut (SS,,TT) of ) of GG

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

Page 22: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

MaxFlow MinCut TheoremProof LayoutMaxFlow MinCut TheoremProof Layout

Lemma 26.1Lemma 26.1Lemma 26.2Lemma 26.2Lemma 26.3Lemma 26.3

Corollary 26.4Corollary 26.4Lemma 26.5Lemma 26.5

Corollary 26.6Corollary 26.6

Eq 26.6Eq 26.6 Eq 26.4Eq 26.4

MaxFlow MinCutMaxFlow MinCut

Theorem 26.7Theorem 26.7

(3) -> (1)(3) -> (1)

(1) -> (2)(1) -> (2)

(2) -> (3)(2) -> (3)

Page 23: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Ford-Fulkerson MaxFlow Method

Ford-Fulkerson MaxFlow Method

Page 24: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

General ApproachGeneral Approach

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

Page 25: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

With Residual NetworksWith Residual Networks

EuvEvuvufvuf ),(or ),( if ],[),( EuvEvuvufvuf ),(or ),( if ],[),(

EuvEvuvufvuf ),(or ),( if ],[),(

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

EuvEvuvuf ),(nor ),(neither if undefined is ],[

Page 26: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

Page 27: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

AnalysisAnalysis

source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.

Time depends Time depends on methodon methodtermination?termination?

(|(|EE|) time|) time

( |( |maxmax f f | ) | ) iterations, iterations, assuming integral assuming integral capacitiescapacities

Each iteration of while loop can be executed in O(|Each iteration of while loop can be executed in O(|EE|) |) time time = time to find path in residual network using BFS or DFS= time to find path in residual network using BFS or DFS

Total time Total time = = ( |( |EE| || |maxmax f f | ) | ) assuming integral capacitiesassuming integral capacities

Page 28: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Shortest Augmenting PathsEdmonds-KarpShortest Augmenting PathsEdmonds-Karp

source: Sedgewick, Graph Algorithmssource: Sedgewick, Graph Algorithms

Page 29: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2006

Shortest Augmenting PathsEdmonds-KarpShortest Augmenting PathsEdmonds-Karp

• Time is in O(|Time is in O(|VV||||EE||22):):• Each iteration of while loop takes time in O(|Each iteration of while loop takes time in O(|EE|)|)• Number of iterations is in O(|Number of iterations is in O(|VV||||EE|)|)

• Shortest-path distance in residual network increases Shortest-path distance in residual network increases monotonically with each flow augmentationmonotonically with each flow augmentation

• Total number of augmentations is in O(|Total number of augmentations is in O(|VV||||EE|)|)• Proof Sketch:Proof Sketch:

• Edge in residual network is critical on augmenting path if Edge in residual network is critical on augmenting path if residual capacity of path is residual capacity of (residual capacity of path is residual capacity of (u,v)u,v)

• Critical edge disappears from residual networkCritical edge disappears from residual network• Critical edge reappears Critical edge reappears onlyonly if net flow from if net flow from uu to to vv is is

decreased; happens only if (decreased; happens only if (v,uv,u) appears later on another ) appears later on another augmenting pathaugmenting path

• From time (From time (u,vu,v) becomes critical to time it next becomes ) becomes critical to time it next becomes critical, distance of u from source increases by >= 2critical, distance of u from source increases by >= 2

• ((u,vu,v) becomes critical at most O(|) becomes critical at most O(|VV|) times|) times