ACM today The Fall competition is getting organized… it's going to be lonely…
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
Infinities seen…
int BIG = ; Michael E.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; Scott P.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; Scott P. paths[i][j] = 9001; Martin P.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; Scott P. paths[i][j] = 9001; Martin P. direct = 2400; Russ R. int constant = 1000; Zvi E. public static int Inf = 200; George T.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; 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.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; 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.
Infinities seen… int BIG = ; Michael E. grid[j][k] = ; 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.
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; } int BIG = ; Michael E. grid[j][k] = ; 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.
Schedule 10/2/ /9/ /16/ /23/ /30/ /6/ /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
Max Flow A B E D C 13 F source capacity sink The problem how much traffic can get from the source to the sink ? Ford-Fulkerson algorithm
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 F F TO “Capacity Graph” source sink capacity C
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 TO source sink capacity C A B C D E ABCDE F F need to add this into here…
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 F F TO “Flow Graph” source sink capacity F 12 flow in one direction is negative flow in the other direction
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 TO “Residual Graph” source sink capacity R 12 reverse edges allow the "undoing" of previous flow!
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 !
The max flow algorithm A B E D C 13 F “Residual Graph” source sink capacity 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 residual 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).
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…
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? tables with capacities team sizes
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
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
Problem Set #5: Search algorithms, etc. orchard gumbo getout sq circuses
Subdividing Squares orchard gumbo getout sq circuses 9 smaller squares that cover a larger 7x7 square Input Output side length in the larger square # of problems smallest number of smaller squares covering the original
Counting trees 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
Java Geometry
Ascii Geometry START 3 -10,-2,1 300,14.5, ,-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
Ascii Geometry START 3 -10,-2,1 300,14.5, ,-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
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
See you next week!
ACM today The Fall competition is getting organized…
Schedule 10/2/ /9/ /16/ /23/ /30/ /6/ /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
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 F F TO “Capacity Graph” source sink capacity C
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 F F TO “Flow Graph” source sink capacity F 12 flow in one direction is negative flow in the other direction
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 TO “Residual Graph” source sink capacity R 12 reverse edges allow the "undoing" of previous flow!
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 !
The max flow algorithm A B E D C 13 F “Residual Graph” source sink capacity 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 residual 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).
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…
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? tables with capacities team sizes
Subdividing Squares orchard gumbo getout sq circuses 9 smaller squares that cover a larger 7x7 square Input Output side length in the larger square # of problems smallest number of smaller squares covering the original
Java Geometry
Jotto… SophomoresJuniorsSeniorsMe fjord 3fjord 0fjord 1fjord 2 tempt 1 marks 1 tempt 1 marks 3 tempt 2 marks 0 tempt 0 marks 1
Ascii Geometry number of dimensions Input starting point and destination point edges in the graph next number of dimensions Output Maze #1 can be traveled Maze #2 cannot be traveled getout.X