Download presentation
Presentation is loading. Please wait.
Published byJudith Fitzgerald Modified over 8 years ago
1
ACM today The Fall competition is getting organized… it's going to be lonely…
2
Mock mock contest #2 More teams solved problems; more problems were solved All problems solved by some team shippingforwardpanic all-pairs shortest paths could be used for this one… following calls through a graph of forwarding instructions Still one computer per team… API-reference is OK on another machine
3
Infinities seen…
4
int BIG = 10000000; Michael E.
5
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P.
6
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P.
7
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R.
8
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E.
9
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E. public static int Inf = 200; George T.
10
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E. public static int Inf = 200; George T. adjacency[a][b] = 0; Andrew H.
11
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E. public static int Inf = 200; George T. adjacency[a][b] = 100; Andrew H. int infinity = 31; Kwang K.
12
Infinities seen… int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E. public static int Inf = 200; George T. adjacency[a][b] = 100; Andrew H. int infinity = 31; Kwang K. #define INFINITE 0x0fffffff Andrew F.
13
Infinities seen… for (int woo = 0; woo < tries; woo++) { int hashnode(char *buf) { int hv = 0; while(*buf) { hv <<= 6; hv |= *buf++ & 31; } return hv; } 268435455 int BIG = 10000000; Michael E. grid[j][k] = 1000000; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E. public static int Inf = 200; George T. adjacency[a][b] = 100; Andrew H. int infinity = 31; Kwang K. #define INFINITE 0x0fffffff Andrew F.
14
Schedule 10/2/2007 10/9/2007 10/16/2007 10/23/2007 10/30/2007 11/6/2007 11/13/2007 Today -- search and related problems Lab / mock mock contest No class: conference & fall break… Real mock contest - "real" rules apply Meeting for ACM teams Final CS 189 meeting: contest wrap-up 9pm to 1am Tuesday, 10/30 one more algorithm
15
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 can get from the source to the sink ? Ford-Fulkerson algorithm
16
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
17
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 add this into here…
18
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
19
R = C - F Create a 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 --- --
20
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 There is no longer a path from A to F !
21
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).
22
Get into the flow! def edmonds_karp(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 flow = float(1e309) # Infinity ! # traverse path to find smallest capacity for (u,v) in path: flow = min(flow, C[u][v] - F[u][v]) # traverse path to update flow for u,v in path: F[u][v] += flow F[v][u] -= flow return sum([F[source][i] for i in xrange(n)]) def BFS(C, F, source, sink): queue = [source] paths = {source: []} while queue: u = queue.pop(0) for v in range(len(C)): if C[u][v] - F[u][v] > 0 and v not in paths: # Python == English ! paths[v] = paths[u] + [(u,v)] if v == sink: return paths[v] queue.append(v) return None Pseudo-code (Python) A little bit of name contention… edmonds_karp is really Ford-Fulkerson, but with other people getting the credit…
23
Max flow examples… Dinner problem Circuses problem A E C F D B G Circus inspectors need to visit every circus -- starting from anywhere. What is the smallest # of inspectors needed to visit each node without overlap? There are M teams with M[i] team members T tables with T[i] seating capacity No two members from one team should share a table. Can you still seat everyone? 3 5 2 6 3 tables with capacities team sizes 4 5 3 5
24
How does maxflow help… ? The problem Fewest number of people to reach all nodes without coinciding... A E C F D B G one instance ABCDEFG S T ABCDEFG answer is N-maxflow (all edges 1 unit) graph of constraints
25
Get into the flow! def edmonds_karp(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 flow = float(1e309) # Infinity ! # traverse path to find smallest capacity for (u,v) in path: flow = min(flow, C[u][v] - F[u][v]) # traverse path to update flow for u,v in path: F[u][v] += flow F[v][u] -= flow return sum([F[source][i] for i in xrange(n)]) def BFS(C, F, source, sink): queue = [source] paths = {source: []} while queue: u = queue.pop(0) for v in range(len(C)): 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) return None Pseudo-code (Python) A little bit of name contention… edmonds_karp is really Floyd-Fulkerson, but with other people getting the credit… many problems require no more than BFS or DFS
26
Problem Set #5: Search algorithms, etc. orchard gumbo getout sq circuses
27
Subdividing Squares orchard gumbo getout sq circuses 9 smaller squares that cover a larger 7x7 square. 3 1 1 2 Input Output 34373437 469469 side length in the larger square # of problems smallest number of smaller squares covering the original
28
Counting trees 1.5 1.5 1.5 6.8 6.8 1.5 0.9 0.9 1.1 0.9 1.1 1.1 0.9 1.1 Input vertices of a polygon on a single line of input Output orchard.X 15 1 right-justified number of lattice points in each polygon 0,0 7,7
29
Java Geometry
30
Ascii Geometry START 3 -10,-2,1 300,14.5,-20 350,-80,0 400,28.75,26 @@@@ n #^^# oU ## o ooooooooooo o oooooo o oooo o DDDD oo DDDD D D TT TT END number of bullets to be fired Input velocity of the target bullet velocities: N, E, up target shape getout.X the bullets are fired 10m due south of the center of the target
31
Ascii Geometry START 3 -10,-2,1 300,14.5,-20 350,-80,0 400,28.75,26 @@@@ n #^^# oU ## o ooooooooooo o oooooo o oooo o DDDD oo DDDD D D TT TT END number of bullets to be fired Input velocity of the target bullet velocities: N, E, up target shape Output getout.X @@@@ * #^^# oU ## o ooooooooooo o oooooo o oooo o DDDD oo DDDD D D TT TT asterisk indicating hits the bullets are fired 10m due south of the center of the target Matt Streshinsky… each ASCII character is 10cm x 10cm
33
Jotto… SophomoresJuniorsSeniorsMe fjord 3fjord 0fjord 1fjord 2 tempt 1 marks 1 tempt 1 marks 3 tempt 2 marks 0 tempt 0 marks 1 diner 1 diner 0diner 2 chore 2chore 0chore 1
34
See you next week!
35
ACM today The Fall competition is getting organized…
36
Schedule 10/2/2007 10/9/2007 10/16/2007 10/23/2007 10/30/2007 11/6/2007 11/13/2007 Today -- search and misc problems Lab / mock mock contest No class: conference & fall break… Real mock contest - "real" rules apply Meeting for ACM teams Final CS 189 meeting: contest wrap-up 9pm to 1am Tuesday, 10/30
37
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
38
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
39
R = C - F Create a 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 --- --
40
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 There is no longer a path from A to F !
41
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. 4a. If no path, you’re done. 4b. If a path, add the available capacity to F; goto (2).
42
Get into the flow! def edmonds_karp(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 flow = float(1e309) # Infinity ! # traverse path to find smallest capacity for (u,v) in path: flow = min(flow, C[u][v] - F[u][v]) # traverse path to update flow for u,v in path: F[u][v] += flow F[v][u] -= flow return sum([F[source][i] for i in xrange(n)]) def BFS(C, F, source, sink): queue = [source] paths = {source: []} while queue: u = queue.pop(0) for v in range(len(C)): 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) return None Pseudo-code (Python) A little bit of name contention… edmonds_karp is really Floyd-Fulkerson, but with other people getting the credit…
43
Max flow examples… Dinner problem Circuses problem A E C F D B G Circus inspectors need to visit every circus -- starting from anywhere. What is the smallest # of inspectors needed to visit each node without overlap? There are M teams with M[i] team members T tables with T[i] seating capacity No two members from one team should share a table. Can you still seat everyone? 3 5 2 6 3 tables with capacities team sizes 4 5 3 5
44
Subdividing Squares orchard gumbo getout sq circuses 9 smaller squares that cover a larger 7x7 square. 3 1 1 2 Input Output 34373437 469469 side length in the larger square # of problems smallest number of smaller squares covering the original
45
Java Geometry
46
Jotto… SophomoresJuniorsSeniorsMe fjord 3fjord 0fjord 1fjord 2 tempt 1 marks 1 tempt 1 marks 3 tempt 2 marks 0 tempt 0 marks 1
47
Ascii Geometry 2 0 0 2 2 0 0 0 1 0 1 0 2 0 2 1 2 1 2 2 2 3 1 1 1 1 2 3 1 1 2 1 1 3 1 1 3 1 2 3 1 1 1 1 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 number of dimensions Input starting point and destination point edges in the graph next number of dimensions 0 2 0 1 0 2 1 2 Output Maze #1 can be traveled Maze #2 cannot be traveled getout.X
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.