Harbin, China

41
Harbin, China 2010 finals ACM today… ! (2009) Knapsack DP for NP! Floyd- Warshall DP for APSP! Max Flow

description

ACM today… ! (2009). DP for NP!. DP for APSP!. Max Flow. Knapsack. Floyd-Warshall. Harbin, China. 2010 finals. Jotto!. A word-guessing game similar to mastermind…. Sophs. Jrs. Srs. Profs. Chalk 1. Chalk 0. Chalk 1. Chalk 1. Quine 1. Quine 1. Quine 2. Quine 2. ?. ?. ?. ?. - PowerPoint PPT Presentation

Transcript of Harbin, China

Page 1: Harbin, China

Harbin, China2010 finals

ACM today… ! (2009)

Knapsack

DP for NP!

Floyd-Warshall

DP for APSP! Max Flow

Page 2: Harbin, China

Jotto!A word-guessing game similar to mastermind…

next guesses?

Sophs Jrs Srs Profs

Chalk 1 Chalk 0 Chalk 1 Chalk 1

Quine 1 Quine 1 Quine 2 Quine 2

? ? ? ?

Page 3: Harbin, China

Candy!

1800's

1950's

1970's

1920's

1940's

Page 4: Harbin, China

Maximizing Candy!i = 1

i = 2

i = 3

i = 4

i = 5

Suppose you can consume 13 candywt units of candy.What choice will maximize your candy-value experience?

(1) if you can take fractional parts of candy packages…?

(2) if you can take any number of whole candy packages…?

(3) if you can take 0 or 1 of each whole candy package…?

w1 = 2v1 = 100

w2 = 3v2 = 120

w3 = 5v3 = 230

w4 = 7v4 = 560

w5 = 9v5 = 675

Page 5: Harbin, China

Maximizing Candy!i = 1

i = 2

i = 3

i = 4

i = 5

Suppose you can consume 13 candywt units of candy.What choice will maximize your candy-value experience?

(1) if you can take fractional parts of candy packages…?

(2) if you can take any number of whole candy packages…?

(3) if you can take 0 or 1 of each whole candy package…?

GO WITH GREED!

w1 = 2v1 = 100

vpw1 = 50

w2 = 3v2 = 120

vpw2 = 40

w3 = 5v3 = 230

vpw3 = 46

w4 = 7v4 = 560

vpw4 = 80

w5 = 9v5 = 675

vpw5 = 75

Page 6: Harbin, China

Knapsack problemi = 1

i = 2

i = 3

i = 4

i = 5

Suppose you can consume 13 candywt units of candy.

(2) if you can take any number of whole candy packages…?

"unbounded"

IDEA:

Consider all possible weights (integers) from 0 up to the weight you can carry -For each one choose the best from all N items

w1 = 2v1 = 100

vpw1 = 50

w2 = 3v2 = 120

vpw2 = 40

w3 = 5v3 = 230

vpw3 = 46

w4 = 7v4 = 560

vpw4 = 80

w5 = 9v5 = 675

vpw5 = 75

Page 7: Harbin, China

Knapsack problemi = 1

i = 2

i = 3

i = 4

i = 5

Suppose you can consume 13 candywt units of candy.

(2) if you can take any number of whole candy packages…?

"unbounded"

0 1 2 3 4 5 6 7 8 9 10 11 12 13

TOTAL WEIGHT

max total value

V(w) =

w1 = 2v1 = 100

vpw1 = 50

w2 = 3v2 = 120

vpw2 = 40

w3 = 5v3 = 230

vpw3 = 46

w4 = 7v4 = 560

vpw4 = 80

w5 = 9v5 = 675

vpw5 = 75

max i

V(w-wi) + vi

0 0 100 120 200 230 300 560 560 675 680 775 800 875

Page 8: Harbin, China

Knapsack problemi = 1

i = 2

i = 3

i = 4

i = 5

Suppose you can consume 13 candywt units of candy.

(3) if you can take 0 or 1 of each whole candy package…?

w1 = 2v1 = 100

vpw1 = 50

w2 = 3v2 = 120

vpw2 = 40

w3 = 5v3 = 230

vpw3 = 46

w4 = 7v4 = 560

vpw4 = 80

w5 = 9

"0-1 problem"

IDEA:

Do the same thing as before, but consider sublists of items that grow one-at-a-time

v5 = 675

vpw5 = 75

Page 9: Harbin, China

Knapsack problem w1 = 2v1 = 100

i = 1

i = 2

i = 3

i = 4

i = 5

vpw1 = 50

w2 = 3v2 = 120

vpw2 = 40

w3 = 5v3 = 230

vpw3 = 46

w4 = 7v4 = 560

vpw4 = 80

w5 = 9v5 = 675

vpw5 = 75

Suppose you can consume 13 candywt units of candy.

"0-1 problem"

0 1 2 3 4 5 6 7 8 9 10 11 12 13

TOTAL WEIGHT

max total value

V(i,w) =

(3) if you can take 0 or 1 of each whole candy package…?

Total Items

0

01

012

0123

01234

012345

maxV(i-1,w)

V(i,w-wi) + vi i

Page 10: Harbin, China

elite

3 30

the number of different subsets of cows satisfying that constraint.

Input

exact number of cows we need, indexed from 1 .. 100

but the sum-of-squares of their indices must be < 30

Input4

1 2 31 2 42 3 41 3 4

14212926

total

Page 11: Harbin, China

elite

3 30

the number of different subsets of cows satisfying that constraint.

Input

exact number of cows we need, indexed from 1 .. 100

but the sum-of-squares of their indices must be < 30

Input4

1 2 31 2 42 3 41 3 4

14212926

total

"use it or lose it"analysis

You're either going to USE the first cow - or you're not. Sum up both cases!

Page 12: Harbin, China

elite

3 30

the number of different subsets of cows satisfying that constraint.

Input

exact number of cows we need, indexed from 1 .. 100

but the sum-of-squares of their indices must be < 30

Input4

1 2 31 2 42 3 41 3 4

14212926

"use it or lose it"

N(3,30,1) = N(2,29,2) + N(3,30,2)

# of len-3 subsets, totaling under 30, starting from 1

# of len-3 subsets, totaling under 30, starting from 2

# of len-2 subsets, totaling under 29, starting from 2

total

Page 13: Harbin, China

Thanks to Allie Russell for this…

Page 14: Harbin, China

elite

but it's much too slow!

Page 15: Harbin, China

Python function decorators

plain

Page 16: Harbin, China

Python function decorators

decorated!

@dec42

Page 17: Harbin, China

Python function decorators

implementedfib = dec42(fib)

Page 18: Harbin, China

DP for APSP…

Directed graph as adjacency matrix:

1

2

34

14

14

14

10

50

100

Directed graph:

src

1

2

3

4

1 2 3 4

"from"

dst"to"

100inf140

0

0

0

inf 5014

infinf

infinf

14

10

0 intermediate nodes

all-pairs-shortest-paths

Page 19: Harbin, China

Floyd-Warshall!

src

1

2

3

4

1 2 3 4

"from"

dst"to"

100inf140

0

0

0

inf 5014

infinf

infinf

14

10

0 intermediate nodes

src

1

2

3

4

1 2 3 4

"from"

dst"to"

100inf140

0

0

0

inf 5014

infinf

inf24

14

10

1 intermediate node(s)!1

Page 20: Harbin, China

Floyd-Warshall!

src

1

2

3

4

1 2 3 4

"from"

dst"to"

100inf140

0

0

0

inf 5014

infinf

inf24

14

10

src

1

2

3

4

1 2 3 4

"from"

dst"to"

64inf140

0

0

0

inf 5014

infinf

inf24

14

10

2 intermediate node(s)!1

1 intermediate node(s)1 2

Page 21: Harbin, China

Floyd-Warshall!

src

1

2

3

4

1 2 3 4

"from"

dst"to"

4228140

0

0

0

inf 2814

infinf

3824

14

10

src

1

2

3

4

1 2 3 4

"from"

dst"to"

4228140

0

0

0

38

2814

38

24

3824

14

10

4 intermediate node(s) ~ done!1

3 intermediate node(s)1 22 3 3 4

All-pairs shortest paths:

Page 22: Harbin, China

Floyd-Warshall!

src

1

2

3

4

1 2 3 4

"from"

dst"to"

4228140

0

0

0

inf 2814

infinf

3824

14

10244 intermediate node(s)

1 2 3 4

minimum distance from src to dst using

intermediate nodes 1..k

min

T[src][dst][k]

=T[src][dst][k-1]

T[src][k][k-1] + T[k][dst][k-1]

inf

14 + 10

Page 23: Harbin, China

Tracking the paths…Floyd Warshall…

not a huge amount of extra work to keep an extra table (pred) of the first intermediate vertex on the path from i to j

Page 24: Harbin, China

This week's problems…

These two are available if you haven't submitted them successfully

before…

Page 25: Harbin, China

This week's problems…

!

Page 26: Harbin, China

hurdles

What is the minimum cost alongany one of the edges requiredto get from start to end ?

Input

Output

5 6 31 2 123 2 81 3 52 5 33 4 42 4 83 41 25 1

number of nodes

number of edges

number of "tasks"

from node 1 to node 2 the cost ("height") is 12

48-1 not possible!

1 2

5

4

3

12

5 8

4

8

3

Page 27: Harbin, China

4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3

number of cows

Input total # of foods

total # of drinks

# of foods cow[i] likes

# of drinks cow[i] likesfoods drinks

0

Output

# of cows that can receive both a food and a

drink they like…

3

each can be used only once

Likes

foods drinks

1

2

3

1 2

2 3

1 3

1 3

3 1

1 2

1 2

3What is a cow-satisfying assignment here?

dining

Page 28: Harbin, China

Max Flow

A

B

E

D

C13

F

16

10 4 9

12

14

7

20

4source

capacity

sink

The problem how much traffic (flow) can get from the source to the sink ?

Ford-Fulkerson algorithm

Page 29: Harbin, China

Max FlowThe problem how much traffic can get from the source to the sink ?

A

B

E

D

C13

ABCDE

FROM

A B C D E

F

- 16 13 - -- - 10 12 -- 4 - - 14- - 9 - -- - - 7 -

---204

- - - - - -F

F

16

10 4 9

12

14

7

20

4

TO“Capacity Graph”

source

sink

capacity C

Page 30: Harbin, China

Find a path in C via BFSThe problem how much traffic can get from the source to the sink ?

A

B

E

D

C13

FROM

F

- 16 13 - -- - 10 12 -- 4 - - 14- - 9 - -- - - 7 -

---204

- - - - - -

16

10 4 9

12

14

7

20

4

TO

source

sink

capacity CABCDE

A B C D E

F

F

Need to subtract this flow from C !

Page 31: Harbin, China

Create FCreate a FLOW GRAPH with the minimum weight from that

path

A

B

E

D

C13

ABCDE

FROM

A B C D E

F

- 12 0 - --12

- 0 12 -- 0 - - 0- -

120 - -

- - - 0 -

---120

- - - -12

- -F

F

16

10 4 9

12

14

7

20

4

TO“Flow Graph”

source

sink

capacity F

12

12

12

flow in one direction is negative flow in the other direction

Page 32: Harbin, China

R = C - FUse the RESIDUAL GRAPH with the rest of the capacity

after that flow

A

B

E

D

C13

ABCDE

FROM

A B C D E

F

F

F

4

10 4 9

0

14

7

8

4

TO

“Residual Graph”

source

sink

capacity R

12 12

reverse edges allow the "undoing" of previous flow!

12

- 4 13 - -12 - 10 0 -- 4 - - 14- 12 9 - -- - - 7 -

---84

- - - 12 - -

Page 33: Harbin, China

Keep going!Continue running this on the residual capacity until BFS

fails…

A

B

E

D

C12/13

F

11/16

0/10

1/4

0/9

12/12

11/14

7/7

19/20

4/4

source

sink

Continue until there is no longer a path from A to F !

max flow: 23

Page 34: Harbin, China

The max flow algorithm

A

B

E

D

C13

F

16

10 4 9

12

14

7

20

4

“Residual Graph”

source

sink

capacity

4

3

2

1

5

22

10

4

flow

Don't need to keep R around explicity: Keep only the current flow (F) and the

original capacity (C).A

B

C

D

EF2

18

4

3 residual9

5

9

72592

11

124

61

Floyd-Fulkerson

1. Set F to all 02. Compute R = C - F3. BFS in R from source to sink.3a. If no path, you’re done. 3b. If a path, add the available

capacity to F; goto (2).

Page 35: Harbin, China

Get into the flow!

def max_flow(C, source, sink): n = len(C) # C is the capacity matrix F = [[0] * n for i in range(n)] # F is the flow matrix # residual capacity from u to v is C[u][v] - F[u][v]

while True: path = BFS(C, F, source, sink) if not path: break # no path - we're done!

# find the path's flow, that is, the "bottleneck" edges = [C[u][v]-F[u][v] for u,v in path] path_flow = min( edges ) print "Augmenting by", path_flow for u,v in path: # traverse path to update flow F[u][v] += path_flow # forward edge up F[v][u] -= path_flow # backward edge down

return sum([F[source][i] for i in range(n)]) # out from source

A little bit of name contention…

edmonds_karpThis is the algorithm.

Page 36: Harbin, China

Useful alone, too

def BFS(C, F, source, sink): queue = [source] # the BFS queue paths = {source: []} # stores 1 path per graph node while queue:

u = queue.pop(0) # next node to explore (expand) for v in range(len(C)): # for each possible next node

# path from u to v? and not yet at v? if C[u][v] - F[u][v] > 0 and v not in paths: paths[v] = paths[u] + [(u,v)] if v == sink: return paths[v]

queue.append(v) # go from v in the future return None

A brief BFS algorithm using the Capacity matrix

Page 37: Harbin, China

Setting up…

if __name__ == "__main__":

# make a capacity graph # node A B C D E F C = [ [ 00, 16, 13, 00, 00, 00 ], # A [ 00, 00, 10, 12, 00, 00 ], # B [ 00, 04, 00, 00, 14, 00 ], # C [ 00, 00, 9, 00, 00, 20 ], # D [ 00, 00, 00, 7, 00, 4 ], # E [ 00, 00, 00, 00, 00, 00 ] ] # F

print "C is", C source = 0 # A sink = 5 # F

max_flow_value = max_flow( C, source, sink ) print "max_flow_value is", max_flow_value

And the code needed to run it…

Linked at the ACM website by the slides…

Page 38: Harbin, China

Jotto!A word-guessing game similar to mastermind…

next guesses?

Sophs Jrs Srs Profs

Chalk 1 Chalk 0 Chalk 1 Chalk 1

Quine 1 Quine 1 Quine 2 Quine 2

? ? ? ?

Page 39: Harbin, China

elite eating

problem

how does the solution depend on subproblems?

use it or lose it!

Page 40: Harbin, China

Knapsack problem w1 = 2v1 = 100

i = 1

i = 2

i = 3

i = 4

i = 5

vpw1 = 50

w2 = 3v2 = 120

vpw2 = 40

w3 = 5v3 = 230

vpw3 = 46

w4 = 7v4 = 560

vpw4 = 80

w5 = 9

Suppose you can consume 13 candywt units of candy.

"0-1 problem"

0 1 2 3 4 5 6 7 8 9 10 11 12 13

TOTAL WEIGHT

max total value

V(i,w) =

(3) if you can take 0 or 1 of each whole candy package…?

Total Items

0

01

012

0123

01234

012345v5 = 675

vpw5 = 75

Page 41: Harbin, China

Candy!

1800's

1950's

1970's

1920's

1940's