Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Harbin, China 2010 finals ACM today… ! (2009) Knapsack DP for NP! Floyd-Warshall DP for APSP! Max Flow."— Presentation transcript:

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

2 Jotto! A word-guessing game similar to mastermind… next guesses? SophsJrsSrsProfs Chalk 1Chalk 0Chalk 1 Quine 1 Quine 2 ????

3 Candy! 1800's 1950's 1970's 1920's 1940's

4 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…? w 1 = 2 v 1 = 100 w 2 = 3 v 2 = 120 w 3 = 5 v 3 = 230 w 4 = 7 v 4 = 560 w 5 = 9 v 5 = 675

5 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! w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75

6 Knapsack problem i = 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 w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75

7 Knapsack problem i = 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" 0123456789 10111213 TOTAL WEIGHT max total value V(w) = w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75 max i V(w-w i ) + v i 0 0 100120200230300560 675680775800875

8 Knapsack problem i = 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…? w 1 = 2 v 1 = 100 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 "0-1 problem" IDEA: Do the same thing as before, but consider sublists of items that grow one-at-a-time v 5 = 675 vpw 5 = 75

9 Knapsack problem w 1 = 2 v 1 = 100 i = 1 i = 2 i = 3 i = 4 i = 5 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 v 5 = 675 vpw 5 = 75 Suppose you can consume 13 candywt units of candy. "0-1 problem" 0123456789 10111213 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 max V(i-1,w) V(i,w-w i ) + v i i

10 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 Input 4 1 2 3 1 2 4 2 3 4 1 3 4 14 21 29 26 total

11 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 Input 4 1 2 3 1 2 4 2 3 4 1 3 4 14 21 29 26 total "use it or lose it" analysis You're either going to USE the first cow - or you're not. Sum up both cases!

12 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 Input 4 1 2 3 1 2 4 2 3 4 1 3 4 14 21 29 26 "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

13 Thanks to Allie Russell for this…

14 elite but it's much too slow!

15 Python function decorators plain

16 Python function decorators decorated! @dec42

17 Python function decorators implemented fib = dec42(fib)

18 DP for APSP… Directed graph as adjacency matrix: 1 2 3 4 14 10 50 100 Directed graph: src 1 2 3 4 1234 "from" dst "to" 100inf 140 0 0 0 inf50 14 inf 14 10 0 intermediate nodes all-pairs-shortest-paths

19 Floyd-Warshall! src 1 2 3 4 1234 "from" dst "to" 100inf 140 0 0 0 inf50 14 inf 14 10 0 intermediate nodes src 1 2 3 4 1234 "from" dst "to" 100inf 140 0 0 0 inf50 14 inf 24 14 10 1 intermediate node(s)! 1

20 Floyd-Warshall! src 1 2 3 4 1234 "from" dst "to" 100inf 140 0 0 0 inf50 14 inf 24 14 10 src 1 2 3 4 1234 "from" dst "to" 64inf 140 0 0 0 inf50 14 inf 24 14 10 2 intermediate node(s)! 1 1 intermediate node(s) 1 2

21 Floyd-Warshall! src 1 2 3 4 1234 "from" dst "to" 4228 140 0 0 0 inf28 14 inf 38 24 14 10 src 1 2 3 4 1234 "from" dst "to" 4228 140 0 0 0 3828 14 38 24 38 24 14 10 4 intermediate node(s) ~ done! 1 3 intermediate node(s) 122334 All-pairs shortest paths:

22 Floyd-Warshall! src 1 2 3 4 1234 "from" dst "to" 4228 140 0 0 0 inf28 14 inf 38 24 14 10 24 4 intermediate node(s) 123 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

23 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

24 This week's problems… These two are available if you haven't submitted them successfully before…

25 This week's problems… !

26 hurdles What is the minimum cost along any one of the edges required to get from start to end ? Input Output 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 number of nodes number of edges number of "tasks" from node 1 to node 2 the cost ("height") is 12 4 8 not possible! 1 2 5 4 3 12 5 8 4 8 3

27 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 number of cows Input total # of foods total # of drinks # of foods cow[i] likes # of drinks cow[i] likes foodsdrinks 0 Output # of cows that can receive both a food and a drink they like… 3 each can be used only once Likes foodsdrinks 1 2 3 1 2 2 3 1 3 3 1 1 2 3 What is a cow-satisfying assignment here? dining

28 Max Flow A B E D C 13 F 16 104 9 12 14 7 20 4 source capacity sink The problem how much traffic (flow) can get from the source to the sink ? Ford-Fulkerson algorithm

29 Max Flow The problem how much traffic can get from the source to the sink ? A B E D C 13 A B C D E FROM ABCDE F -1613-- --1012- -4--14 --9-- ---7- - - - 20 4 ------ F F 16 104 9 12 14 7 20 4 TO “Capacity Graph” source sink capacity C

30 Find a path in C via BFS The problem how much traffic can get from the source to the sink ? A B E D C 13 FROM F -1613-- --1012- -4--14 --9-- ---7- - - - 20 4 ------ 16 104 9 12 14 7 20 4 TO source sink capacity C A B C D E ABCDE F F Need to subtract this flow from C !

31 Create F Create a FLOW GRAPH with the minimum weight from that path A B E D C 13 A B C D E FROM ABCDE F -120-- -12-012- -0--0 --120-- ---0- - - - 12 0 ----12-- F F 16 104 9 12 14 7 20 4 TO “Flow Graph” source sink capacity F 12 flow in one direction is negative flow in the other direction

32 R = C - F Use the RESIDUAL GRAPH with the rest of the capacity after that flow A B E D C 13 A B C D E FROM ABCDE F F F 4 104 9 0 14 7 8 4 TO “Residual Graph” source sink capacity R 12 reverse edges allow the "undoing" of previous flow! 12 -413-- 12-100- -4--14 -129-- ---7- - - - 8 4 --- --

33 Keep going! Continue running this on the residual capacity until BFS fails… A B E D C 12/13 F 11/16 0/101/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

34 The max flow algorithm A B E D C 13 F 16 104 9 12 14 7 20 4 “Residual Graph” source sink capacity 4 3 2 1 5 2 2 1 0 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 E F 2 18 4 3 residual 9 5 9 7 2 5 9 2 11 12 4 6 1 Floyd-Fulkerson 1. Set F to all 0 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).

35 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_karp This is the algorithm.

36 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

37 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…

38 Jotto! A word-guessing game similar to mastermind… next guesses? SophsJrsSrsProfs Chalk 1Chalk 0Chalk 1 Quine 1 Quine 2 ????

39 elite eating problem how does the solution depend on subproblems? use it or lose it!

40 Knapsack problem w 1 = 2 v 1 = 100 i = 1 i = 2 i = 3 i = 4 i = 5 vpw 1 = 50 w 2 = 3 v 2 = 120 vpw 2 = 40 w 3 = 5 v 3 = 230 vpw 3 = 46 w 4 = 7 v 4 = 560 vpw 4 = 80 w 5 = 9 Suppose you can consume 13 candywt units of candy. "0-1 problem" 0123456789 10111213 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 v 5 = 675 vpw 5 = 75

41 Candy! 1800's 1950's 1970's 1920's 1940's


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

Similar presentations


Ads by Google