CSE 421 Algorithms

52
CSE 421 Algorithms Richard Anderson Lecture 24 Network Flow Applications

description

CSE 421 Algorithms. Richard Anderson Lecture 24 Network Flow Applications. Today’s topics. Problem Reductions Undirected Flow to Flow Bipartite Matching Disjoint Path Problem Circulations Lowerbound constraints on flows Survey design. Problem Reduction. Reduce Problem A to Problem B - PowerPoint PPT Presentation

Transcript of CSE 421 Algorithms

Page 1: CSE 421 Algorithms

CSE 421Algorithms

Richard AndersonLecture 24

Network Flow Applications

Page 2: CSE 421 Algorithms

Today’s topics

• Problem Reductions– Undirected Flow to Flow– Bipartite Matching– Disjoint Path Problem

• Circulations• Lowerbound constraints on flows• Survey design

Page 3: CSE 421 Algorithms

Problem Reduction

• Reduce Problem A to Problem B– Convert an instance of Problem A to an instance

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 4: CSE 421 Algorithms

Problem Reduction Examples

• Reduce the problem of finding the Maximum of a set of integers to finding the Minimum of a set of integers

Find the maximum of: 8, -3, 2, 12, 1, -6

Construct an equivalent minimization problem

Page 5: CSE 421 Algorithms

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 6: CSE 421 Algorithms

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 7: CSE 421 Algorithms

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 8: CSE 421 Algorithms

Converting Matching to Network Flow

ts

Page 9: CSE 421 Algorithms

Finding edge disjoint paths

st

Construct a maximum cardinality set of edge disjoint paths

Page 10: CSE 421 Algorithms

Theorem

• The maximum number of edge disjoint paths equals the minimum number of edges whose removal separates s from t

Page 11: CSE 421 Algorithms

• Directed graph with capacities, c(e) on the edges, and demands d(v) on vertices

• Find a flow function that satisfies the capacity constraints and the vertex demands– 0 <= f(e) <= c(e)– fin(v) – fout(v) = d(v)

• Circulation facts:– Feasibility problem– d(v) < 0: source; d(v) > 0: sink– Must have vd(v)=0 to be feasible

Circulation Problem

u

s t

v

10

10

10

20

20

-25

-5

10

20

Page 12: CSE 421 Algorithms

Find a circulation in the following graph

a

b e

c f

d g

h

2

0-4

-5

-2 2

3

4

10

8

2

4

363

7 2 1

432

5

9

5

Page 13: CSE 421 Algorithms

Reducing the circulation problem to Network flow

u

a b

v

10

10

10

20

20

-25

-5

10

20

Page 14: CSE 421 Algorithms

Formal reduction

• Add source node s, and sink node t• For each node v, with d(v) < 0, add an

edge from s to v with capacity -d(v)• For each node v, with d(v) > 0, add an

edge from v to t with capacity d(v)• Find a maximum s-t flow. If this flow has

size vcap(s,v) then the flow gives a circulation satisifying the demands

Page 15: CSE 421 Algorithms

Circulations with lowerbounds on flows on edges

• Each edge has a lowerbound l(e).– The flow f must satisfy l(e) <= f(e) <= c(e)

a x

yb

-2

-4

3

32,4

1,2

0,2

1,4

Page 16: CSE 421 Algorithms

Removing lowerbounds on edges

• Lowerbounds can be shifted to the demands

yb-4 32,5

yb-2 13

Page 17: CSE 421 Algorithms

Formal reduction

• Lin(v): sum of lowerbounds on incoming edges

• Lout(v): sum of lowerbounds on outgoing edges

• Create new demands d’ and capacities c’ on vertices and edges– d’(v) = d(v) + lout(v) – lin(v)– c’(e) = c(e) – l(e)

Page 18: CSE 421 Algorithms

Application

• Customized surveys– Ask customers about products

• Only ask customers about products they use• Limited number of questions you can ask each

customer• Need to ask a certain number of customers about

each product• Information available about which products each

customer has used

Page 19: CSE 421 Algorithms

Details

• Customer C1, . . ., Cn

• Products P1, . . ., Pm

• Si is the set of products used by Ci

• Customer Ci can be asked between ci and c’i questions

• Questions about product Pj must be asked on between pj and p’j surveys

Page 20: CSE 421 Algorithms

Circulation construction

Page 21: CSE 421 Algorithms

Today’s topics

• 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 22: CSE 421 Algorithms

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 23: CSE 421 Algorithms

Mine Graph

-3

-10

-4

3

-2

-3

-1

8

-2

4

3

-1

-7

-10

-1

Page 24: CSE 421 Algorithms

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 25: CSE 421 Algorithms

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 26: CSE 421 Algorithms

Min cut algorithm for profit maximization

• Construct a flow graph where the minimum cut identifies a feasible set that maximizes profit

Page 27: CSE 421 Algorithms

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 28: CSE 421 Algorithms

Show a finite value cut with at least two vertices on each side of the cut

s

t

Infinite

Finite

Page 29: CSE 421 Algorithms

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 30: CSE 421 Algorithms

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 31: CSE 421 Algorithms

Enumerate all finite s,t cuts and show their capacities

-2 1 2 -2 1

s

t

2 2

1 12

Page 32: CSE 421 Algorithms

Minimum cut gives optimal solutionWhy?

-2 1 2 -2 1

s

t

2 2

1 12

Page 33: CSE 421 Algorithms

Computing the Profit

• Cost(W) = {w in W; p(w) < 0}-p(w)

• Benefit(W) = {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 34: CSE 421 Algorithms

Express Cap(S,T) in terms of B, C, Cost(T), Benefit(T), and Profit(T)

Page 35: CSE 421 Algorithms

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

Page 36: CSE 421 Algorithms

Today’s topics

• More network flow reductions– Airplane scheduling– Image segmentation– Baseball elimination

Page 37: CSE 421 Algorithms

Airplane Scheduling

• Given an airline schedule, and starting locations for the planes, is it possible to use a fixed set of planes to satisfy the schedule.

• Schedule– [segments] Departure, arrival pairs (cities and times)

• Approach– Construct a circulation problem where paths of flow

give segments flown by each plane

Page 38: CSE 421 Algorithms

Example• Seattle->San Francisco, 9:00 – 11:00• Seattle->Denver, 8:00 – 11:00• San Francisco -> Los Angeles, 13:00 – 14:00• Salt Lake City -> Los Angeles, 15:00-17:00• San Diego -> Seattle, 17:30-> 20:00• Los Angeles -> Seattle, 18:00->20:00

• Flight times:– Denver->Salt Lake City, 2 hours– Los Angeles->San Diego, 1 hour

Can this schedule be full filled with two planes, starting from Seattle?

Page 39: CSE 421 Algorithms

Compatible segments

• Segments S1 and S2 are compatible if the same plane can be used on S1 and S2

– End of S1 equals start of S2, and enough time for turn around between arrival and departure times

– End of S1 is different from S2, but there is enough time to fly between cities

Page 40: CSE 421 Algorithms

• Each segment, Si, is represented as a pair of vertices (di, ai, for departure and arrival), with an edge between them.

• Add an edge between ai and dj if Si is compatible with Sj.

Graph representation

di ai

ai dj

Page 41: CSE 421 Algorithms

Setting up a flow problem

di ai

ai dj

PiP’i

1,1

0,1

-1 1

Page 42: CSE 421 Algorithms

Result

• The planes can satisfy the schedule iff there is a feasible circulation

Page 43: CSE 421 Algorithms

Image Segmentation

• Separate foreground from background

Page 44: CSE 421 Algorithms
Page 45: CSE 421 Algorithms

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) = {i in A}ai + {j in B}bj - {(i,j) in E, i in A, j in B}pij

Page 46: CSE 421 Algorithms

Pixel graph to flow graphs

t

Page 47: CSE 421 Algorithms

Mincut Construction

u v

s

t

bv

av

puv

pvu

Page 48: CSE 421 Algorithms

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 49: CSE 421 Algorithms

Baseball elimination

• Can the Fruit Flies win 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 50: CSE 421 Algorithms

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 51: CSE 421 Algorithms

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 52: CSE 421 Algorithms

Network flow applications summary

• Bipartite Matching• Disjoint Paths• Airline Scheduling• Survey Design• Baseball Elimination• Project Selection• Image Segmentation