Post on 21-Jan-2016
description
Harbin, China2010 finals
ACM today… !
Knapsack
DP for NP!
Floyd-Warshall
DP for APSP!
ACM "abroad"
ACM progress…
Candy!
1800's
1950's
1970's
1920's
1940's
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 = 2
v1 = 100
w2 = 3
v2 = 120
w3 = 5
v3 = 230
w4 = 7
v4 = 560
w5 = 9
v5 = 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 = 2
v1 = 100
vpw1 = 50
w2 = 3
v2 = 120
vpw2 = 40
w3 = 5
v3 = 230
vpw3 = 46
w4 = 7
v4 = 560
vpw4 = 80
w5 = 9
v5 = 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 = 2
v1 = 100
vpw1 = 50
w2 = 3
v2 = 120
vpw2 = 40
w3 = 5
v3 = 230
vpw3 = 46
w4 = 7
v4 = 560
vpw4 = 80
w5 = 9
v5 = 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 = 2
v1 = 100
vpw1 = 50
w2 = 3
v2 = 120
vpw2 = 40
w3 = 5
v3 = 230
vpw3 = 46
w4 = 7
v4 = 560
vpw4 = 80
w5 = 9
v5 = 675
vpw5 = 75
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 = 2
v1 = 100
vpw1 = 50
w2 = 3
v2 = 120
vpw2 = 40
w3 = 5
v3 = 230
vpw3 = 46
w4 = 7
v4 = 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 = 2
v1 = 100
i = 1
i = 2
i = 3
i = 4
i = 5
vpw1 = 50
w2 = 3
v2 = 120
vpw2 = 40
w3 = 5
v3 = 230
vpw3 = 46
w4 = 7
v4 = 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
Knapsack problem w1 = 2
v1 = 100
i = 1
i = 2
i = 3
i = 4
i = 5
vpw1 = 50
w2 = 3
v2 = 120
vpw2 = 40
w3 = 5
v3 = 230
vpw3 = 46
w4 = 7
v4 = 560
vpw4 = 80
w5 = 9
v5 = 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
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
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:
Wikipedia's code
Longest paths? What has to be true about the
graph?
My hope is for FW to become a
Wikipedia pseudocode primitivenot yet!
Tracking the paths…Floyd Warshall…
Dynamic programming
When a seemingly intractable problem has lots of repeated substructure, go DP!
Build a table of partial results.
Replace computation with table look-up when possible
notree
Input
Output
4 50 1 1 0 92 5 5 1 331 5 5 1 100 1 1 0 99
dimensions grid of costs for clearing trees
6
minimum cost
4 50 1 1 0 92 5 5 1 331 5 5 1 100 1 1 0 99
Cows helicopter in and clear trees in order to "connect
the dots"
notreeinline bool valid (int r, int c){ return r >= 0 && r < R && c >= 0 && c < C;}
void init (){ memset (dist, 63, sizeof (dist)); int dr [] = {-1, 0, 1, 0}, dc [] = {0, 1, 0, -1};
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) for (int d = 0; d < 4; d++) { int nr = r + dr [d], nc = c + dc [d];
if (valid (nr, nc)) dist [r][c][nr][nc] = cost [nr][nc]; }
for (int a = 0; a < R; a++) for (int b = 0; b < C; b++) for (int c = 0; c < R; c++) for (int d = 0; d < C; d++) for (int e = 0; e < R; e++) for (int f = 0; f < C; f++) dist [c][d][e][f] = min (dist [c][d][e][f], dist [c][d][a][b] + dist [a][b][e][f]);}
Where's Floyd Warshall here?
4 50 1 1 0 92 5 5 1 331 5 5 1 100 1 1 0 99
cost [nr][nc]
notree
Build a table! All subsets of treats…000 001 010 011 100 101 110 111
All starting points (r,c)
…
(0,0)
(0,1)
(0,9)
(1,0)
(1,1)
(1,9)
(9,0)
(9,1)
(9,9)
…
…
…
…
Each cell holds the minimum cost, starting from (r,c) of reaching all the treats in the
subset.
Which squares are easy?
notree
Build a table! All subsets of treats…000 001 010 011 100 101 110 111
All starting points (r,c)
…
(0,0)
(0,1)
(0,9)
(1,0)
(1,1)
(1,9)
(9,0)
(9,1)
(9,9)
…
…
…
…
Each cell holds the minimum cost, starting from (r,c) of reaching all the treats in the
subset.
Which squares are easy?
T[ss][r,c] = min T[s1][r,c] + T[s2][r,c]
s1 & s2 = s
T[ss][r,c] = min T[ss][a,b] + dist[r,c][a,b]
(a,b)
notree
Build a table! All subsets of treats…000 001 010 011 100 101 110 111
All starting points (r,c)
…
(0,0)
(0,1)
(0,9)
(1,0)
(1,1)
(1,9)
(9,0)
(9,1)
(9,9)
…
…
…
…
Each cell holds the minimum cost, starting from (r,c) of reaching all the treats in the
subset.
T[ss][r,c] = min T[s1][r,c] + T[s2][r,c]
s1 & s2 = s
T[ss][r,c] = min T[ss][a,b] + dist[r,c][a,b]
(a,b)
for (int s = ss; s != 0; s = s - 1 & ss) for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) T[ss][i][j] = min(T[ss][i][j], T[s][i][j] + T[ss^s][i][j]);Crazy!
ACM contest == CS60?
Greg Rae, HMC trustee
Most venerable problem in cs60? (and
cs42?)
these lines are from CS
60, Fall 1999
ACM regionals ~
CS 60?
Tautology checking (and parsing!)
( no Prolog permitted )
ACM regionals ~
CS 60?
20 questions: given objects, given questions, and given the answers to the questions (!),
Identify the object in the specified number of questions…
ACM regionals ~
CS 60?
from top left to bottom right as cheaply as possible…
"Quiz"
Read over this week's problems…
Which is shortest paths?
Which is knapsack?
What else is there?
Finished? Jotto!
Jotto!
Sophs Jrs Srs “Other”
A word-guessing game similar to mastermind…
icily 0 icily 0 icily 1 icily 1
Next Tuesday: Labbring a laptop, if you
have one…
strep 2 strep 2 strep 2 strep 1
spork 1 spork 3 spork 0 spork 0