Harbin, China
description
Transcript of Harbin, China
Harbin, China2010 finals
ACM today… ! (2009)
Knapsack
DP for NP!
Floyd-Warshall
DP for APSP! Max Flow
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
? ? ? ?
Candy!
1800's
1950's
1970's
1920's
1940's
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
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
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
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
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
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
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
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!
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
Thanks to Allie Russell for this…
elite
but it's much too slow!
Python function decorators
plain
Python function decorators
decorated!
@dec42
Python function decorators
implementedfib = dec42(fib)
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
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
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
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:
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
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…
!
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
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
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
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
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 !
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
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 - -
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
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).
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.
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
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…
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
? ? ? ?
elite eating
problem
how does the solution depend on subproblems?
use it or lose it!
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
Candy!
1800's
1950's
1970's
1920's
1940's