csep 521 applied algorithms
DESCRIPTION
CSEP 521 Applied Algorithms. Richard Anderson Lecture 9 Network Flow Applications. Announcements. Reading for this week 7.5-7.12. Network flow applications Next week: Chapter 8. NP-Completeness Final exam, March 18, 6:30 pm. At UW. 2 hours In class (CSE 303 / CSE 305) Comprehensive - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/1.jpg)
CSEP 521Applied Algorithms
Richard AndersonLecture 9
Network Flow Applications
![Page 2: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/2.jpg)
Announcements
• Reading for this week– 7.5-7.12. Network flow applications– Next week: Chapter 8. NP-Completeness
• Final exam, March 18, 6:30 pm. At UW.– 2 hours– In class (CSE 303 / CSE 305)– Comprehensive
• 67% post midterm, 33% pre midterm
![Page 3: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/3.jpg)
Network Flow
![Page 4: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/4.jpg)
Review
• Network flow definitions• Flow examples• Augmenting Paths• Residual Graph• Ford Fulkerson Algorithm• Cuts• Maxflow-MinCut Theorem
![Page 5: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/5.jpg)
Network Flow Definitions• Flowgraph: Directed graph with distinguished
vertices s (source) and t (sink)• Capacities on the edges, c(e) >= 0• Problem, assign flows f(e) to the edges such
that:– 0 <= f(e) <= c(e)– Flow is conserved at vertices other than s and t
• Flow conservation: flow going into a vertex equals the flow going out
– The flow leaving the source is a large as possible
![Page 6: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/6.jpg)
Find a maximum flow
a
s
d
b
c f
e
g
h
i
t
15/25
5/5
20/20 20/20
20/20
25/30
20/20
5/5
20/20
0/5
20/20
15/20
10/10
20/20
5/5
20/20
30/30
0/5
0/5
0/5
0/5
0/5
0/5
0/20
![Page 7: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/7.jpg)
Residual Graph
• Flow graph showing the remaining capacity• Flow graph G, Residual Graph GR
– G: edge e from u to v with capacity c and flow f– GR: edge e’ from u to v with capacity c – f– GR: edge e’’ from v to u with capacity f
![Page 8: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/8.jpg)
Residual Graph
u
s t
v
15/20
20/20
15/30
0/10
5/10
u
s t
v
5
15
10
5 20
15 15
5
![Page 9: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/9.jpg)
Augmenting Path Lemma
• Let P = v1, v2, …, vk be a path from s to t with minimum capacity b in the residual graph.
• b units of flow can be added along the path P in the flow graph.
u
s t
v
15/20
20/20
15/30
0/10
5/10
u
s t
v
5
15
10
5 20
15 15
5
![Page 10: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/10.jpg)
Ford-Fulkerson Algorithm (1956)
while not done
Construct residual graph GR
Find an s-t path P in GR with capacity b > 0
Add b units along in G
If the sum of the capacities of edges leaving S is at most C, then the algorithm takes at most C iterations
![Page 11: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/11.jpg)
Cuts in a graph• Cut: Partition of V into disjoint sets S, T with s in
S and t in T.• Cap(S,T): sum of the capacities of edges from
S to T• Flow(S,T): net flow out of S
– Sum of flows out of S minus sum of flows into S
• Flow(S,T) <= Cap(S,T)
![Page 12: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/12.jpg)
Ford Fulkerson MaxFlow – MinCut Theorem
• There exists a flow which has the same value of the minimum cut– Shows that a cut is the dual of the flow– Proves that the augmenting paths algorithm finds a
maximum flow– Gives an algorithms for finding the minimum cut
![Page 13: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/13.jpg)
Better methods of for constructing a network flow
• Improved methods for finding augmenting paths or blocking flows
• Goldberg’s Preflow-Push algorithm– Text, section 7.4
![Page 14: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/14.jpg)
Applications of Network Flow
![Page 15: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/15.jpg)
Problem Reduction• Reduce Problem A to Problem B
– Convert an instance of Problem A to an instance of Problem B
– Use a solution of Problem B to get a solution to Problem A
• Practical– Use a program for Problem B to solve Problem A
• Theoretical– Show that Problem B is at least as hard as Problem A
![Page 16: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/16.jpg)
Problem Reduction Examples
• Reduce the problem of finding the longest path in a directed graph to the problem of finding a shortest path in a directed graph
Construct an equivalent minimization problem
![Page 17: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/17.jpg)
Undirected Network Flow
• Undirected graph with edge capacities• Flow may go either direction along the
edges (subject to the capacity constraints)
u
s t
v
10
10
5
20
20
Construct an equivalent flow problem
![Page 18: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/18.jpg)
Multi-source network flow
• Multi-source network flow– Sources s1, s2, . . ., sk
– Sinks t1, t2, . . . , tj
• Solve with Single source network flow
![Page 19: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/19.jpg)
Bipartite Matching
• A graph G=(V,E) is bipartite if the vertices can be partitioned into disjoints sets X,Y
• A matching M is a subset of the edges that does not share any vertices
• Find a matching as large as possible
![Page 20: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/20.jpg)
Application• A collection of teachers• A collection of courses• And a graph showing which teachers can
teach which coursesRA
PB
CC
DG
AK
303
321
326
401
421
![Page 21: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/21.jpg)
Converting Matching to Network Flow
ts
![Page 22: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/22.jpg)
Finding edge disjoint paths
st
Construct a maximum cardinality set of edge disjoint paths
![Page 23: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/23.jpg)
Theorem
• The maximum number of edge disjoint paths equals the minimum number of edges whose removal separates s from t
![Page 24: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/24.jpg)
Finding vertex disjoint paths
st
Construct a maximum cardinality set of vertiex disjoint paths
![Page 25: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/25.jpg)
Network flow with vertex capacities
![Page 26: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/26.jpg)
Balanced allocationProblem 9, Page 419
• To make a long story short:– N injured people– K hospitals– Assign each person to a hospital with 30
minutes drive– Assign N/K patients to each hospital
![Page 27: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/27.jpg)
Baseball elimination• Can the Dinosaurs
win the league?• Remaining games:
– AB, AC, AD, AD, AD, BC, BC, BC, BD, CD
W LAnts 4 2Bees 4 2Cockroaches 3 3Dinosaurs 1 5
A team wins the league if it has strictly more wins than any other team at the end of the seasonA team ties for first place if no team has more wins, and there is some other team with the same number of wins
![Page 28: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/28.jpg)
Baseball elimination• Can the Fruit Flies
win or tie the league?• Remaining games:
– AC, AD, AD, AD, AF, BC, BC, BC, BC, BC, BD, BE, BE, BE, BE, BF, CE, CE, CE, CF, CF, DE, DF, EF, EF
W LAnts 17 12Bees 16 7Cockroaches 16 7Dinosaurs 14 13Earthworms 14 10Fruit Flies 12 15
![Page 29: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/29.jpg)
Assume Fruit Flies win remaining games
• Fruit Flies are tied for first place if no team wins more than 19 games
• Allowable wins– Ants (2)– Bees (3)– Cockroaches (3)– Dinosaurs (5)– Earthworms (5)
• 18 games to play– AC, AD, AD, AD, BC, BC,
BC, BC, BC, BD, BE, BE, BE, BE, CE, CE, CE, DE
W L
Ants 17 13
Bees 16 8
Cockroaches 16 9
Dinosaurs 14 14
Earthworms 14 12
Fruit Flies 19 15
![Page 30: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/30.jpg)
Remaining gamesAC, AD, AD, AD, BC, BC, BC, BC, BC, BD, BE, BE, BE, BE, CE, CE, CE, DE
s
AC AD BC BD BE CE
A B C D E
T
DE
![Page 31: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/31.jpg)
Solving problems with a minimum cut
• Image Segmentation• Open Pit Mining / Task Selection Problem
S, T is a cut if S, T is a partition of the vertices with s in S and t in TThe capacity of an S, T cut is the sum of the capacities ofall edges going from S to T
![Page 32: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/32.jpg)
Image Segmentation• Separate foreground
from background• Reduction to min-cut
problem
S, T is a cut if S, T is a partition of the vertices with s in S and t in T
The capacity of an S, T cut is the sum of the capacities ofall edges going from S to T
![Page 33: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/33.jpg)
![Page 34: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/34.jpg)
Image analysis
• ai: value of assigning pixel i to the foreground• bi: value of assigning pixel i to the background• pij: penalty for assigning i to the foreground, j to
the background or vice versa• A: foreground, B: background• Q(A,B) = S{i in A}ai + S{j in B}bj - S{(i,j) in E, i in A, j in B}pij
![Page 35: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/35.jpg)
Pixel graph to flow graphs
t
![Page 36: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/36.jpg)
Mincut Construction
u v
s
t
bv
av
puv
pvu
![Page 37: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/37.jpg)
Open Pit Mining
![Page 38: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/38.jpg)
Application of Min-cut
• Open Pit Mining Problem• Task Selection Problem• Reduction to Min Cut problem
S, T is a cut if S, T is a partition of the vertices with s in S and t in TThe capacity of an S, T cut is the sum of the capacities ofall edges going from S to T
![Page 39: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/39.jpg)
Open Pit Mining
• Each unit of earth has a profit (possibly negative)
• Getting to the ore below the surface requires removing the dirt above
• Test drilling gives reasonable estimates of costs
• Plan an optimal mining operation
![Page 40: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/40.jpg)
Mine Graph
-3
-10
-4
3
-2
-3
-1
8
-2
4
3
-1
-7
-10
-1
![Page 41: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/41.jpg)
Determine an optimal mine
-10
-10
-10
-10
-10
-5
-1
-10
-10
-10
-10
-3
-3
4
6
-10
-10
-4
-3
7
8
1
-10
-4
3
-2
6
4
-10
2
4
-10
-3
4
-10
-4
4
3
-10
-10
-10
3
-3
-10
-10
-10
-10
-1
-10
-10
-10
-10
-10
-7
![Page 42: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/42.jpg)
Generalization• Precedence graph
G=(V,E)• Each v in V has a
profit p(v)• A set F if feasible if
when w in F, and (v,w) in E, then v in F.
• Find a feasible set to maximize the profit
5
-10
6
-1
-2 -3
4
-4
![Page 43: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/43.jpg)
Min cut algorithm for profit maximization
• Construct a flow graph where the minimum cut identifies a feasible set that maximizes profit
![Page 44: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/44.jpg)
Precedence graph construction• Precedence graph
G=(V,E)• Each edge in E has
infinite capacity• Add vertices s, t• Each vertex in V is
attached to s and t with finite capacity edges
![Page 45: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/45.jpg)
Show a finite value cut with at least two vertices on each side of the cut
s
t
Infinite
Finite
![Page 46: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/46.jpg)
The sink side of a finite cut is a feasible set
• No edges permitted from S to T
• If a vertex is in T, all of its ancestors are in T
![Page 47: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/47.jpg)
Setting the costs• If p(v) > 0,
– cap(v,t) = p(v)– cap(s,v) = 0
• If p(v) < 0– cap(s,v) = -p(v)– cap(v,t) = 0
• If p(v) = 0– cap(s,v) = 0– cap(v,t) = 0
s
t
3
-3
2
1
0-1-3
31
3
2 1 3
![Page 48: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/48.jpg)
Enumerate all finite s,t cuts and show their capacities
-2 1 2 -2 1
s
t
2 2
1 12
![Page 49: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/49.jpg)
Minimum cut gives optimal solutionWhy?
-2 1 2 -2 1
s
t
2 2
1 12
![Page 50: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/50.jpg)
Computing the Profit
• Cost(W) = S{w in W; p(w) < 0}-p(w)• Benefit(W) = S{w in W; p(w) > 0} p(w)• Profit(W) = Benefit(W) – Cost(W)
• Maximum cost and benefit– C = Cost(V) – B = Benefit(V)
![Page 51: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/51.jpg)
Express Cap(S,T) in terms of B, C, Cost(T), Benefit(T), and Profit(T)
![Page 52: CSEP 521 Applied Algorithms](https://reader036.vdocuments.us/reader036/viewer/2022062501/5681645f550346895dd63522/html5/thumbnails/52.jpg)
Summary• Construct flow graph
– Infinite capacity for precedence edges– Capacities to source/sink based on
cost/benefit• Finite cut gives a feasible set of tasks• Minimizing the cut corresponds to
maximizing the profit• Find minimum cut with a network flow
algorithm