Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jotto! Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2

Similar presentations


Presentation on theme: "Jotto! Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2"— Presentation transcript:

1 Jotto! Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2
Don C. is on my team... Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2 squid 1 squid 0 squid 1 fails 2 fails 2 fails 0 fails 2 PERL stories - use in ACM - creating scripts - LTS, BOT cache 1 cache 2 cache 3 cache 0 Srs and jrs guessing...

2 Next Tuesday: lab/qualifier
This will be our Mock ACM contest and qualifier for the ACM regionals... Tuesday, October 19th 9pm: Local qualifier and evening ACM lab Make up qualifier: Wednesday, 10/20 sometime ~ let me know Tuesday, October 26th Final ACM "lecture" meeting Tuesday, November 2nd Final ACM "lab" meeting PERL stories - use in ACM - creating scripts - LTS, BOT Tuesday, November 9th meet with the teams going to Riverside Saturday, November 13th Riverside regionals Tuesday, November 16th Final ACM meeting & debriefing...

3 tricount number of nodes Input number of edges 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 "tasks" from node 1 to node 2 the cost ("height") is 12 12 2 1 Output 3 5 8 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!

4 tricount Input Output 5 -5 0 0 2 11 2 -11 -6 11 -5 5 number of points
How many triangles include the origin? 5

5 tricount

6 tricount ANAK!

7 number of battles & questions Who will win the new battles?
starc number of battles & questions 3 3 J B J Input Output Who will win the new battles? J U Farmer John Bessie 6Y + 5Y + 4Z > 5Y + 4Y + 7Z 5Y + 4Y + 2Z < 3Y + 5Y + 5Z strength strength strength X Y Z ? 6Y + 6Y + 4Z > 5Y + 4Y + 7Z cattlebruisers cowtemplars ultracows

8 number of battles & questions Who will win the new battles?
starc number of battles & questions 3 3 J B J Input Output Who will win the new battles? J U Farmer John Bessie 6Y + 5Y + 4Z > 5Y + 4Y + 7Z 5Y + 4Y + 2Z < 3Y + 5Y + 5Z strength strength strength X Y Z ? Andrew! 6Y + 6Y + 4Z > 5Y + 4Y + 7Z cattlebruisers cowtemplars ultracows

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

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

11 Floyd-Warshall! dst dst src src 14 inf 100 14 inf 64 inf 14 50 inf 14
"to" "to" 1 2 3 4 1 2 3 4 14 inf 100 14 inf 64 1 1 2 inf 14 50 2 inf 14 50 src src "from" "from" inf inf 14 inf inf 14 3 3 10 24 inf 10 24 inf 4 4 1 intermediate node(s) 2 intermediate node(s)! 1 1 2

12 4 intermediate node(s) ~ done!
Floyd-Warshall! All-pairs shortest paths: dst dst "to" "to" 1 2 3 4 1 2 3 4 14 28 42 14 28 42 1 1 2 inf 14 28 2 38 14 28 src src "from" "from" inf inf 14 24 38 14 3 3 10 24 38 10 24 38 4 4 3 intermediate node(s) 4 intermediate node(s) ~ done! 1 2 3 1 2 3 4

13 minimum distance from src to dst using intermediate nodes 1..k
Floyd-Warshall! dst T[src][dst][k] "to" 1 2 3 4 minimum distance from src to dst using intermediate nodes 1..k 14 28 42 1 T[src][dst][k-1] inf 2 inf 14 28 src = min T[src][k][k-1] + T[k][dst][k-1] "from" inf inf 14 3 10 24 38 4 24 4 intermediate node(s) 1 2 3 4

14 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

15 Google's top image for Kevin Oelze?
Floyd-Warshall! Google's top image for Kevin Oelze?

16 Floyd-Warshall!

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

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

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

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

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

22 Keep going! Continue running this on the residual capacity until BFS fails… B 12/12 D 11/16 19/20 sink F A 0/10 1/4 0/9 7/7 source 12/13 4/4 C E 11/14 max flow: 23 Continue until there is no longer a path from A to F !

23 The max flow algorithm B D F A C E B D A F C E capacity flow residual
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 B 12 D 3 16 20 sink 4 2 1 F A 10 4 2 9 1 7 source 2 4 13 4 C 5 E 14 capacity flow residual “Residual Graph” 3 B D 12 9 18 Don't need to keep R around explicity: Keep only the current flow (F) and the original capacity (C). 4 A 2 9 1 5 2 7 6 F 2 5 11 C E 4 9

24 Get into the flow! edmonds_karp def max_flow(C, source, sink):
A little bit of name contention… This is the algorithm. edmonds_karp 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

25 A brief BFS algorithm using the Capacity matrix
Useful alone, too A brief BFS algorithm using the Capacity matrix 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

26 And the code needed to run it…
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…

27 Try one or more of this week's problems!

28

29 Knapsack problem v1 = 100 w1 = 2 i = 1 v2 = 120 w2 = 3 V(i,w) = i = 2
vpw1 = 50 i = 1 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) = vpw2 = 40 i = 2 TOTAL WEIGHT v3 = 230 w3 = 5 1 2 3 4 5 6 7 8 9 10 11 12 13 vpw3 = 46 i = 3 01 v4 = 560 w4 = 7 012 vpw4 = 80 i = 4 0123 01234 v5 = 675 012345 w5 = 9 Total Items max total value vpw5 = 75 i = 5

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

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

32 Jotto! Sophs Jrs Srs Profs Chalk 1 Chalk 0 Chalk 1 Chalk 1 Quine 1
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 ? ? ? ? PERL stories - use in ACM - creating scripts - LTS, BOT next guesses?

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

34 Maximizing Candy! Suppose you can consume 13 candywt units of candy.
v1 = 100 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

35 Maximizing Candy! Suppose you can consume 13 candywt units of candy.
v1 = 100 w1 = 2 vpw1 = 50 i = 1 Suppose you can consume 13 candywt units of candy. v2 = 120 w2 = 3 vpw2 = 40 i = 2 What choice will maximize your candy-value experience? v3 = 230 w3 = 5 GO WITH GREED! i = 3 (1) if you can take fractional parts of candy packages…? vpw3 = 46 v4 = 560 w4 = 7 (2) if you can take any number of whole candy packages…? vpw4 = 80 i = 4 v5 = 675 (3) if you can take 0 or 1 of each whole candy package…? w5 = 9 vpw5 = 75 i = 5

36 Knapsack problem v1 = 100 w1 = 2 i = 1
"unbounded" vpw1 = 50 i = 1 Suppose you can consume 13 candywt units of candy. v2 = 120 w2 = 3 (2) if you can take any number of whole candy packages…? vpw2 = 40 i = 2 v3 = 230 w3 = 5 vpw3 = 46 i = 3 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 vpw4 = 80 i = 4 v5 = 675 w5 = 9 vpw5 = 75 i = 5

37 Knapsack problem v1 = 100 w1 = 2 i = 1
"unbounded" vpw1 = 50 i = 1 Suppose you can consume 13 candywt units of candy. v2 = 120 w2 = 3 (2) if you can take any number of whole candy packages…? vpw2 = 40 i = 2 TOTAL WEIGHT v3 = 230 w3 = 5 1 2 3 4 5 6 7 8 9 10 11 12 13 vpw3 = 46 i = 3 100 120 200 230 300 560 560 675 680 775 800 875 v4 = 560 w4 = 7 vpw4 = 80 i = 4 max total value v5 = 675 V(w) = max V(w-wi) + vi w5 = 9 i vpw5 = 75 i = 5

38 Knapsack problem v1 = 100 w1 = 2 i = 1
vpw1 = 50 i = 1 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…? vpw2 = 40 i = 2 v3 = 230 w3 = 5 vpw3 = 46 i = 3 IDEA: v4 = 560 Do the same thing as before, but consider sublists of items that grow one-at-a-time w4 = 7 vpw4 = 80 i = 4 v5 = 675 w5 = 9 vpw5 = 75 i = 5

39 Knapsack problem v1 = 100 w1 = 2 i = 1 v2 = 120 V(i-1,w) w2 = 3
vpw1 = 50 i = 1 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 V(i,w-wi) + vi vpw2 = 40 i = 2 TOTAL WEIGHT v3 = 230 w3 = 5 1 2 3 4 5 6 7 8 9 10 11 12 13 vpw3 = 46 i = 3 01 v4 = 560 w4 = 7 012 vpw4 = 80 i = 4 0123 01234 v5 = 675 012345 w5 = 9 Total Items max total value vpw5 = 75 i = 5

40 elite exact number of cows we need, indexed from 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 1 2 3 1 2 4 2 3 4 1 3 4 14 21 29 26

41 elite "use it or lose it" Input 3 30 Input 4 analysis
exact number of cows we need, indexed from 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 1 2 3 1 2 4 2 3 4 1 3 4 14 21 29 You're either going to USE the first cow - or you're not. Sum up both cases! 26

42 elite Input 3 30 N(3,30,1) = Input 4 N(2,29,2) + N(3,30,2)
exact number of cows we need, indexed from 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 total "use it or lose it" 1 2 3 1 2 4 2 3 4 1 3 4 14 21 29 26

43 Thanks to Allie Russell for this…

44 elite but it's much too slow!

45 Python function decorators
plain

46 Python function decorators
decorated!

47 Python function decorators
fib = dec42(fib) implemented

48 This week's problems… !

49 Jotto! Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2
Don C. is on my team... Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2 squid 1 squid 0 squid 1 fails 2 fails 2 fails 0 fails 2 PERL stories - use in ACM - creating scripts - LTS, BOT cache 1 cache 2 cache 3 cache 0 Srs and jrs guessing...

50 Next Tuesday: lab/qualifier
This will be our Mock ACM contest and qualifier for the ACM regionals... Tuesday, October 19th 9pm: Local qualifier and evening ACM lab Make up qualifier: Wednesday, 10/20 sometime ~ let me know Tuesday, October 26th Final ACM "lecture" meeting Tuesday, November 2nd Final ACM "lab" meeting PERL stories - use in ACM - creating scripts - LTS, BOT Tuesday, November 9th meet with the teams going to Riverside Saturday, November 13th Riverside regionals Tuesday, November 16th Final ACM meeting & debriefing...

51 tricount number of nodes Input number of edges 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 "tasks" from node 1 to node 2 the cost ("height") is 12 12 2 1 Output 3 5 8 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!

52 tricount Input Output 5 -5 0 0 2 11 2 -11 -6 11 -5 5 number of points
How many triangles include the origin? 5

53 tricount ANAK!

54 number of battles & questions Who will win the new battles?
starc number of battles & questions 3 3 J B J Input Output Who will win the new battles? J U Farmer John Bessie 6Y + 5Y + 4Z > 5Y + 4Y + 7Z 5Y + 4Y + 2Z < 3Y + 5Y + 5Z strength strength strength X Y Z ? 6Y + 6Y + 4Z > 5Y + 4Y + 7Z cattlebruisers cowtemplars ultracows

55 number of battles & questions Who will win the new battles?
starc number of battles & questions 3 3 J B J Input Output Who will win the new battles? J U Farmer John Bessie 6Y + 5Y + 4Z > 5Y + 4Y + 7Z 5Y + 4Y + 2Z < 3Y + 5Y + 5Z strength strength strength X Y Z ? Andrew! 6Y + 6Y + 4Z > 5Y + 4Y + 7Z cattlebruisers cowtemplars ultracows

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

57 Floyd-Warshall! dst dst src src 14 inf 100 14 inf 64 inf 14 50 inf 14
"to" "to" 1 2 3 4 1 2 3 4 14 inf 100 14 inf 64 1 1 2 inf 14 50 2 inf 14 50 src src "from" "from" inf inf 14 inf inf 14 3 3 10 24 inf 10 24 inf 4 4 1 intermediate node(s) 2 intermediate node(s)! 1 1 2

58 minimum distance from src to dst using intermediate nodes 1..k
Floyd-Warshall! dst T[src][dst][k] "to" 1 2 3 4 minimum distance from src to dst using intermediate nodes 1..k 14 28 42 1 T[src][dst][k-1] inf 2 inf 14 28 src = min T[src][k][k-1] + T[k][dst][k-1] "from" inf inf 14 3 10 24 38 4 24 4 intermediate node(s) 1 2 3 4

59 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

60 Google's top image for Kevin Oelze?
Floyd-Warshall! Google's top image for Kevin Oelze?

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

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

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

64 The max flow algorithm B D F A C E B D A F C E capacity flow residual
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 B 12 D 3 16 20 sink 4 2 1 F A 10 4 2 9 1 7 source 2 4 13 4 C 5 E 14 capacity flow residual “Residual Graph” 3 B D 12 9 18 Don't need to keep R around explicity: Keep only the current flow (F) and the original capacity (C). 4 A 2 9 1 5 2 7 6 F 2 5 11 C E 4 9

65 Get into the flow! edmonds_karp def max_flow(C, source, sink):
A little bit of name contention… This is the algorithm. edmonds_karp 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

66 A brief BFS algorithm using the Capacity matrix
Useful alone, too A brief BFS algorithm using the Capacity matrix 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

67 And the code needed to run it…
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…

68 Try one or more of this week's problems!


Download ppt "Jotto! Sophs Jrs Srs Profs pluot 1 pluot 2 pluot 1 pluot 2 squid 2"

Similar presentations


Ads by Google