29
UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2006 Lecture 5 Lecture 5 Tuesday, 2/27/06 Tuesday, 2/27/06 Graph Algorithms: Part 2 Graph Algorithms: Part 2

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2006

  • Upload
    heath

  • View
    31

  • Download
    1

Embed Size (px)

DESCRIPTION

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2006. Lecture 5 Tuesday, 2/27/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

Citation preview

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

UMass Lowell Computer Science 91.503

Analysis of Algorithms Prof. Karen Daniels

Spring, 2006

UMass Lowell Computer Science 91.503

Analysis of Algorithms Prof. Karen Daniels

Spring, 2006

Lecture 5Lecture 5Tuesday, 2/27/06Tuesday, 2/27/06

Graph Algorithms: Part 2Graph Algorithms: Part 2

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

Network Flow Network Flow

Chapter 26Chapter 26

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

Basic Network Flow Concepts

Basic Network Flow Concepts

Page 4: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 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 Spring, 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 Spring, 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 Spring, 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 Spring, 2006

Flow DefinitionsFlow Definitions

Flow network G = (V,E)Flow network G = (V,E) directed graphdirected graph each edge (u,v) in E has capacity each edge (u,v) in E has capacity

c(u,v) >= 0c(u,v) >= 0 every vertex is on some path from every vertex is on some path from

source s to sink tsource s to sink t G is connectedG is connected |E| >= |V| - 1|E| >= |V| - 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 Spring, 2006

Flow PropertiesFlow Properties

Flow in G is f: VxV -> R satisfying:Flow in G is f: VxV -> R satisfying: Capacity Constraint:Capacity Constraint: Skew Symmetry:Skew Symmetry: Flow Conservation:Flow Conservation:

f(u,v) is net flow from vertex u to vertex vf(u,v) is net flow from vertex u to vertex v positive net flow entering vertex v:positive net flow entering vertex v:

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

Value of a flow f is total net flow Value of a flow f 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 Spring, 2006

Flow Properties (continued)Flow Properties (continued)

Lemma 26.1Lemma 26.1 Let G=(V,E) be a flow network, and let f be a flow Let G=(V,E) be a flow network, and let f be a flow

in G. Then:in G. 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 Spring, 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 Spring, 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 Spring, 2006

Augmenting Paths

Augmenting Paths

Page 14: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 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 Spring, 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 Spring, 2006

Residual Networks

Residual Networks

Page 17: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 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 Spring, 2006

Cuts

Cuts

Page 19: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 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 Spring, 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 Spring, 2006

MaxFlow MinCut TheoremMaxFlow MinCut Theorem

If If ff is a flow in a flow network G=(V,E) is a flow in a flow network G=(V,E) with source with source ss and sink and sink tt, then, equivalently:, then, equivalently: 1. 1. ff is a maximum flow in G is a maximum flow in G 2. The residual network G2. The residual network Gff contains no contains no

augmenting pathsaugmenting paths 3. | 3. | f f | = c(S,T) for some cut (S,T) of G| = c(S,T) for some cut (S,T) of G

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 Spring, 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 Spring, 2006

Ford-Fulkerson MaxFlow Method

Ford-Fulkerson MaxFlow Method

Page 24: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 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 Spring, 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 Spring, 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 Spring, 2006

AnalysisAnalysis

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

Time depends Time depends on methodon methodtermination?termination?

(|E|) time(|E|) time

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

Each iteration of while loop can be executed in O(|E|) Each iteration of while loop can be executed in O(|E|) 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 = = ( |E| |( |E| |maxmax f f | ) | ) assuming integral capacitiesassuming integral capacities

Page 28: UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 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 Spring, 2006

Shortest Augmenting PathsEdmonds-KarpShortest Augmenting PathsEdmonds-Karp

Time is in O(|V||E|Time is in O(|V||E|22):): Each iteration of while loop takes time in O(|E|)Each iteration of while loop takes time in O(|E|) Number of iterations is in O(|V||E|)Number of iterations is in O(|V||E|)

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(|V||E|)Total number of augmentations is in O(|V||E|) 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 (u,v)residual capacity of path is residual capacity of (u,v)

Critical edge disappears from residual networkCritical edge disappears from residual network Critical edge reappears Critical edge reappears onlyonly if net flow from u to v is if net flow from u to v is

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

From time (u,v) becomes critical to time it next becomes From time (u,v) 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,v) becomes critical at most O(|V|) times(u,v) becomes critical at most O(|V|) times