1 / 30

Harbin, China

ACM today… !. DP for NP!. DP for APSP!. Knapsack. Floyd-Warshall. Harbin, China. 2010 finals. ACM "abroad". ACM progress…. Candy!. 1800's. 1920's. 1940's. 1950's. 1970's. Candy!. 1800's. 1920's. 1940's. 1950's. 1970's. v 1 = 100. Maximizing Candy!. w 1 = 2. i = 1.

lindsay
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ACM today… ! DP for NP! DP for APSP! Knapsack Floyd-Warshall Harbin, China 2010 finals

  2. ACM "abroad"

  3. ACM progress…

  4. Candy! 1800's 1920's 1940's 1950's 1970's

  5. Candy! 1800's 1920's 1940's 1950's 1970's

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

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

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

  9. 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 v4 = 560 w4 = 7 i = 4 vpw4 = 80 max total value v5 = 675 w5 = 9 V(w) = i = 5 vpw5 = 75

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

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

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

  13. DP for APSP… 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

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

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

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

  17. Wikipedia's code My hope is for FW to become a Wikipedia pseudocode primitive not yet! Longest paths? What has to be true about the graph?

  18. Tracking the paths… Floyd Warshall…

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

  20. notree Input dimensions grid of costs for clearing trees 4 5 0 1 1 0 9 2 5 5 1 33 1 5 5 1 10 0 1 1 0 99 Output 4 5 0 1 1 0 9 2 5 5 1 33 1 5 5 1 10 0 1 1 0 99 6 minimum cost Cows helicopter in and clear trees in order to "connect the dots"

  21. notree Where's Floyd Warshall here? inline 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]); } 4 5 0 1 1 0 9 2 5 5 1 33 1 5 5 1 10 0 1 1 0 99 cost [nr][nc]

  22. notree Build a table! All subsets of treats… 000 001 010 011 100 101 110 111 (0,0) Each cell holds the minimum cost, starting from (r,c) of reaching all the treats in the subset. (0,1) … (0,9) (1,0) All starting points (r,c) … (1,1) … (1,9) Which squares are easy? … (9,0) (9,1) … (9,9)

  23. notree Build a table! All subsets of treats… 000 001 010 011 100 101 110 111 (0,0) Each cell holds the minimum cost, starting from (r,c) of reaching all the treats in the subset. (0,1) … (0,9) (1,0) All starting points (r,c) … (1,1) … (1,9) Which squares are easy? … T[ss][r,c] = min T[s1][r,c] + T[s2][r,c] (9,0) s1 & s2 = s (9,1) … T[ss][r,c] = min T[ss][a,b] + dist[r,c][a,b] (9,9) (a,b)

  24. notree Build a table! All subsets of treats… 000 001 010 011 100 101 110 111 (0,0) Each cell holds the minimum cost, starting from (r,c) of reaching all the treats in the subset. (0,1) … (0,9) (1,0) All starting points (r,c) … 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]); (1,1) … Crazy! (1,9) … T[ss][r,c] = min T[s1][r,c] + T[s2][r,c] (9,0) s1 & s2 = s (9,1) … T[ss][r,c] = min T[ss][a,b] + dist[r,c][a,b] (9,9) (a,b)

  25. ACM contest == CS60? Most venerable problem in cs60? (and cs42?) these lines are from CS 60, Fall 1999 Greg Rae, HMC trustee

  26. ACM regionals ~ CS 60? Tautology checking (and parsing!) ( no Prolog permitted )

  27. 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…

  28. ACM regionals ~ CS 60? from top left to bottom right as cheaply as possible…

  29. "Quiz" Read over this week's problems… Which is shortest paths? Which is knapsack? What else is there? Finished? Jotto!

  30. Jotto! A word-guessing game similar to mastermind… Sophs Jrs Srs “Other” icily 0 icily 0 icily 1 icily 1 strep 2 strep 2 strep 2 strep 1 spork 1 spork 3 spork 0 spork 0 Next Tuesday: Lab bring a laptop, if you have one…

More Related