ACM today… ! (2009)
Sponsored Links
This presentation is the property of its rightful owner.
1 / 41

Harbin, China PowerPoint PPT Presentation


  • 145 Views
  • Uploaded on
  • Presentation posted in: General

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. ?. ?. ?. ?.

Download Presentation

Harbin, China

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

?

?

?

?

next guesses?


Candy!

1800's

1920's

1940's

1950's

1970's


v1 = 100

Maximizing Candy!

w1 = 2

i = 1

Suppose you can consume 13 candywt units of candy.

v2 = 120

w2 = 3

i = 2

What choice will maximize your candy-value experience?

v3 = 230

w3 = 5

i = 3

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

v4 = 560

w4 = 7

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

i = 4

v5 = 675

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

w5 = 9

i = 5


v1 = 100

Maximizing Candy!

w1 = 2

i = 1

vpw1 = 50

Suppose you can consume 13 candywt units of candy.

v2 = 120

w2 = 3

i = 2

vpw2 = 40

What choice will maximize your candy-value experience?

v3 = 230

w3 = 5

GO WITH GREED!

i = 3

vpw3 = 46

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

v4 = 560

w4 = 7

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

i = 4

vpw4 = 80

v5 = 675

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

w5 = 9

i = 5

vpw5 = 75


v1 = 100

Knapsack problem

w1 = 2

"unbounded"

i = 1

vpw1 = 50

Suppose you can consume 13 candywt units of candy.

v2 = 120

w2 = 3

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

i = 2

vpw2 = 40

v3 = 230

w3 = 5

i = 3

vpw3 = 46

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

v4 = 560

w4 = 7

i = 4

vpw4 = 80

v5 = 675

w5 = 9

i = 5

vpw5 = 75


v1 = 100

Knapsack problem

w1 = 2

"unbounded"

i = 1

vpw1 = 50

Suppose you can consume 13 candywt units of candy.

v2 = 120

w2 = 3

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

i = 2

vpw2 = 40

v3 = 230

TOTAL WEIGHT

w3 = 5

0

1

2

3

4

5

6

7

8

9

10

11

12

13

i = 3

vpw3 = 46

0

0

v4 = 560

100

120

200

230

300

560

560

675

680

775

800

875

w4 = 7

i = 4

vpw4 = 80

max total value

v5 = 675

w5 = 9

max

V(w-wi) + vi

V(w) =

i

i = 5

vpw5 = 75


v1 = 100

Knapsack problem

w1 = 2

"0-1 problem"

i = 1

vpw1 = 50

Suppose you can consume 13 candywt units of candy.

v2 = 120

w2 = 3

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

i = 2

vpw2 = 40

v3 = 230

w3 = 5

i = 3

vpw3 = 46

IDEA:

v4 = 560

Do the same thing as before,

but consider sublists of items that grow one-at-a-time

w4 = 7

i = 4

vpw4 = 80

v5 = 675

w5 = 9

i = 5

vpw5 = 75


v1 = 100

Knapsack problem

w1 = 2

"0-1 problem"

i = 1

vpw1 = 50

Suppose you can consume 13 candywt units of candy.

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

v2 = 120

V(i-1,w)

w2 = 3

V(i,w) =

max

i = 2

vpw2 = 40

V(i,w-wi) + vi

i

v3 = 230

TOTAL WEIGHT

w3 = 5

0

1

2

3

4

5

6

7

8

9

10

11

12

13

i = 3

vpw3 = 46

0

01

v4 = 560

w4 = 7

012

i = 4

vpw4 = 80

0123

01234

v5 = 675

012345

w5 = 9

i = 5

Total Items

vpw5 = 75

max total value


elite

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

Input

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

3 30

Input

the number of different subsets of cows satisfying that constraint.

4

total

14

1 2 3

1 2 4

2 3 4

1 3 4

21

29

26


elite

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

Input

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

3 30

"use it or lose it"

Input

analysis

the number of different subsets of cows satisfying that constraint.

4

total

14

1 2 3

1 2 4

2 3 4

1 3 4

21

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

29

26


elite

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

Input

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

3 30

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

N(3,30,1) =

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

Input

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

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

the number of different subsets of cows satisfying that constraint.

4

"use it or lose it"

total

14

1 2 3

1 2 4

2 3 4

1 3 4

21

29

26


Thanks to Allie Russell for this…


elite

but it's much too slow!


Pythonfunction decorators

plain


Pythonfunction decorators

@dec42

decorated!


Pythonfunction decorators

fib = dec42(fib)

implemented


DP for APSP…

all-pairs-shortest-paths

Directed graph as adjacency matrix:

dst

Directed graph:

"to"

1

2

3

4

100

inf

100

0

14

1

2

14

1

14

inf

0

50

2

50

src

"from"

inf

inf

0

14

3

10

14

inf

10

inf

0

4

4

3

14

0 intermediate nodes


Floyd-Warshall!

dst

dst

"to"

"to"

1

2

3

4

1

2

3

4

inf

100

inf

100

0

14

0

14

1

1

14

14

inf

0

50

inf

0

50

2

2

src

src

"from"

"from"

inf

inf

inf

0

14

inf

0

14

3

3

24

inf

10

inf

0

10

inf

0

4

4

1 intermediate node(s)!

0 intermediate nodes

1


Floyd-Warshall!

dst

dst

"to"

"to"

1

2

3

4

1

2

3

4

inf

64

inf

100

0

14

0

14

1

1

14

14

inf

0

50

inf

0

50

2

2

src

src

"from"

"from"

inf

inf

inf

0

14

inf

0

14

3

3

24

24

10

inf

0

10

inf

0

4

4

1 intermediate node(s)

2 intermediate node(s)!

1

1

2


Floyd-Warshall!

All-pairs shortest paths:

dst

dst

"to"

"to"

1

2

3

4

1

2

3

4

28

42

28

42

0

14

0

14

1

1

14

14

38

0

28

inf

0

28

2

2

src

src

"from"

"from"

24

inf

38

0

14

inf

0

14

3

3

24

24

10

38

0

10

38

0

4

4

3 intermediate node(s)

4 intermediate node(s) ~ done!

1

2

3

1

2

3

4


Floyd-Warshall!

dst

T[src][dst][k]

"to"

1

2

3

4

minimum distance from src to dst using intermediate nodes 1..k

28

42

0

14

1

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

inf

14

inf

0

28

2

min

=

src

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

T[k][dst][k-1]

14 + 10

"from"

inf

inf

0

14

3

24

10

38

0

4

24

4 intermediate node(s)

1

2

3

4


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


This week's problems…

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


This week's problems…

!


number of nodes

hurdles

Input

number of edges

number of "tasks"

5 6 3

1 2 12

3 2 8

1 3 5

2 5 3

3 4 4

2 4 8

3 4

1 2

5 1

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

12

2

1

Output

3

8

5

What is the minimum cost along

any one of the edges required

to get from start to end ?

5

3

8

4

4

8

-1

4

not possible!


number of cows

dining

Input

total # of foods

total # of drinks

4 3 3

2 2 1 2 3 1

2 2 2 3 1 2

2 2 1 3 1 2

2 1 1 3 3

Likes

1 2

2 3

1 3

1 3

3 1

1 2

1 2

3

0

# of foods cow[i] likes

foods

drinks

1

# of drinks cow[i] likes

2

Output

3

What is a cow-satisfying assignment here?

3

# of cows that can receive both a food and a drink they like…

foods

drinks

each can be used only once


Max Flow

Ford-Fulkerson algorithm

12

B

D

20

16

sink

F

A

10

4

9

7

source

4

13

C

E

14

capacity

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


-

16

13

-

-

-

-

-

10

12

-

-

-

4

-

-

14

-

-

-

9

-

-

20

-

-

-

7

-

4

-

-

-

-

-

-

Max Flow

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

12

B

D

20

16

sink

F

A

10

4

9

7

source

4

13

“Capacity Graph”

C

E

14

TO

C

capacity

A

B

C

D

E

F

A

B

C

FROM

D

E

F


-

16

13

-

-

-

-

-

10

12

-

-

-

4

-

-

14

-

-

-

9

-

-

20

-

-

-

7

-

4

-

-

-

-

-

-

Find a path in C via BFS

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

Need to subtract this flow from C !

12

B

D

20

16

sink

F

A

10

4

9

7

source

4

13

C

E

14

TO

C

capacity

A

B

C

D

E

F

A

B

C

FROM

D

E

F


-

12

0

-

-

-

-12

-

0

12

-

-

-

0

-

-

0

-

-

-12

0

-

-

12

-

-

-

0

-

0

-

-

-

-12

-

-

Create F

Create a FLOW GRAPH with the minimum weight from that path

12

B

D

12

20

16

sink

12

12

F

A

10

4

9

7

source

4

13

“Flow Graph”

C

E

14

TO

F

capacity

A

B

C

D

E

F

A

B

flow in one direction is negative flow in the other direction

C

FROM

D

E

F


-

4

13

-

-

-

12

-

10

0

-

-

-

4

-

-

14

-

-

12

9

-

-

8

-

-

-

7

-

4

-

-

-

12

-

-

R = C - F

Use the RESIDUAL GRAPH with the rest of the capacity after that flow

0

B

D

12

8

4

sink

12

12

F

A

10

4

9

7

source

4

13

“Residual Graph”

C

E

14

TO

R

capacity

A

B

C

D

E

F

A

B

reverse edges allow the "undoing" of previous flow!

C

FROM

D

E

F


Keep going!

Continue running this on the residual capacity until BFS fails…

12/12

B

D

19/20

11/16

sink

F

A

0/10

1/4

0/9

7/7

source

4/4

12/13

C

E

11/14

max flow:23

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


1. Set F to all 0

The max flow algorithm

2. Compute R = C - F

3. 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).

Floyd-Fulkerson

12

B

D

3

20

16

sink

2

4

1

0

F

A

10

4

2

9

1

7

2

source

4

4

13

C

5

E

14

capacity

flow

residual

“Residual Graph”

3

B

D

12

18

9

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

4

2

A

1

9

2

5

6

F

7

2

5

C

E

4

11

9


Get into the flow!

A little bit of name contention…

edmonds_karp

This is the algorithm.

defmax_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]

whileTrue:

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 inrange(n)]) # out from source


Useful alone, too

A brief BFS algorithm using the Capacity matrix

defBFS(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 inrange(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 notin paths:

paths[v] = paths[u] + [(u,v)]

if v == sink:

return paths[v]

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

returnNone


Setting up…

And the code needed to run it…

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

Linked at the ACM website by the slides…


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

?

?

?

?

next guesses?


elite eating

problem

how does the solution depend on subproblems?

use it or lose it!


v1 = 100

Knapsack problem

w1 = 2

"0-1 problem"

i = 1

vpw1 = 50

Suppose you can consume 13 candywt units of candy.

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

v2 = 120

w2 = 3

V(i,w) =

i = 2

vpw2 = 40

v3 = 230

TOTAL WEIGHT

w3 = 5

0

1

2

3

4

5

6

7

8

9

10

11

12

13

i = 3

vpw3 = 46

0

01

v4 = 560

w4 = 7

012

i = 4

vpw4 = 80

0123

01234

v5 = 675

012345

w5 = 9

i = 5

Total Items

vpw5 = 75

max total value


Candy!

1800's

1920's

1940's

1950's

1970's


  • Login