800 likes | 939 Views
ACM today. missing entries? let me know…. This week's problems…. slacking!. not slacking…. New and improved :-) ACM table. Mock mock contest ?. Lots of teams solved problems. All problems were solved by some team. honey. marbles. birthday. Mock mock contest ?.
E N D
ACM today missing entries? let me know… This week's problems… slacking! not slacking… New and improved :-) ACM table
Mock mock contest ? Lots of teams solved problems All problems were solved by some team honey marbles birthday
Mock mock contest ? Lots of teams solved problems All problems were solved by some team honey marbles birthday From 2d to 1d: compute the best horizontal and vertical displacements separately Keep track of the number of paths of length K that end in each cell go to K+1 … Create a "containment" graph of nesting boxes, and then find the LONGEST path in it.
Graph algorithms Always at least one contest problem… • all-pairs shortest paths • maximum flow from a "source" to a "sink" The source of all graph problems…
student projects: summer 2007 Kaylin Spitz Emma Liu measuring centralityin very large connection networks (of proteins) The tollgate centrality of a node, V, anchored on nodes A and B, all in graph G, is the betweenness centrality of V in the A,B shortest-path subgraph of G. 4,519 nodes
0 8 13 - 1 - 0 - 6 12 - 9 0 - - 7 - 0 0 - - - - 11 0 All-pairs shortest paths “Floyd-Warshall algorithm” B Adjacency Matrix 8 9 to A D E B C A 13 C A 6 B 0 from C 7 1 12 D D E E 11 0 D0 = (dij ) 0 dij = shortest distance from i to j through no nodes k dij = shortest distance from i to j through nodes {1, …, k}
0 8 13 - 1 - 0 - 6 12 - 9 0 - - 7 - 0 0 - - - - 11 0 All-pairs shortest paths... “Floyd-Warshall algorithm” D E B C A Before After D E B C A A 0 8 13 - 1 A B - 0 - 6 12 B C - 9 0 - - C D 7 15 0 0 8 D E - - - 11 0 E 0 D0 = (dij ) 1 D1 = (dij ) k dij = shortest distance from i to j through {1, …, k}
0 8 13 - 1 - 0 - 6 12 - 9 0 - - 7 - 0 0 - - - - 11 0 All-pairs shortest paths... “Floyd-Warshall algorithm” k k-1 k-1 k-1 dij = dik dkj + ) min( dij , D E B C A Before After D E B C A A A 0 8 13 - 1 A B B - 0 - 6 12 B C C - 9 0 - - C D D 7 15 0 0 8 D E E - - - 11 0 E 0 D0 = (dij ) 1 D1 = (dij ) k dij = shortest distance from i to j through {1, …, k}
All-pairs shortest paths... A 0 8 13 14 1 A 0 8 13 14 1 B 13 0 6 6 12 B - 0 - 6 12 C 22 9 0 15 21 C - 9 0 15 21 D 7 9 0 0 8 D 7 15 0 0 8 E 18 20 11 11 0 E - - - 11 0 4 D4 = (dij ) 2 D2 = (dij ) A 0 8 12 12 1 A 0 8 13 14 1 B 13 0 6 6 12 B - 0 - 6 12 C 22 9 0 15 21 C - 9 0 15 21 D 7 9 0 0 8 D 7 9 0 0 8 E 18 20 11 11 0 E - - - 11 0 5 3 D5 = (dij ) D3 = (dij ) to store the path, another matrix can track the last intermediate vertex
Thanks to Wikipedia… Floyd Warshall… How would you change this to find longest paths? What has to be true about the graph?
Problem Set #2: Graph algorithms Input 2 0 0 2 2 0 0 0 1 0 1 0 2 0 2 1 2 1 2 2 2 -1 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 -1 0 number of dimensions starting point and destination point edges in the graph next number of dimensions 1 2 2 2 0 2 0 1 Output Maze #1 can be traveled Maze #2 cannot be traveled 0 0
Problem Set #2: Graph algorithms Input "single point of failure" 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 undirected edges in the graph end of graph signal first graph second graph
Problem Set #2: Graph algorithms Input 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 undirected edges in the graph end of graph signal first graph Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes second graph
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph To generate:52-bit numbers in a graph with 4 edges 2 1 3 end of input signal Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure:
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph To generate:52-bit numbers in a graph with 4 edges 2 step 3 step 2 1 3 end of input signal step 1 00 11 10 11 Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure: start
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph Output 2 No Yes some bits are biased more than 75% 1 "reasonably random" - all bits are between 25% and 75% likely to be the value 0 or 1 3 Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure:
Jotto! Sophomores Juniors Seniors Me fjord 3 fjord 0 fjord 1 fjord 2 tempt 1 tempt 1 tempt 2 tempt 0 marks 1 marks 3 marks 0 marks 1 2121 words scoring 0 vs. fjord 2682 words scoring 1 vs. fjord There are 1060 words scoring 2 vs. fjord 132 words scoring 3 vs. fjord 4 fjord: fords, frond, fordy, fardo, foder, dorje,
Next time - A second mock mock contest
Max Flow Max network flow… 12 B D 20 16 sink F A 10 4 9 7 source 4 13 C E 14 capacity The problem how much traffic can get from the source to the sink ?
- 16 13 - - - - - 10 12 - - - 4 - - 14 - - - 9 - - 20 - - - 7 - 4 - - - - - - Max Flow The problem how much traffic can get from the source to the sink ? 12 B D 20 16 sink F A 10 4 9 7 source 4 13 “Capacity Graph” C E 14 TO C capacity A B C D E F A B C FROM D E F
- 16 13 - - - - - 10 12 - - - 4 - - 14 - - - 9 - - 20 - - - 7 - 4 - - - - - - Find a path in C The problem how much traffic can get from the source to the sink ? 12 B D 20 16 sink F A 10 4 9 7 source 4 13 “Capacity Graph” C E 14 TO C capacity A B C D E F A B C FROM D E F
- 12 0 - - - -12 - 0 12 - - - 0 - - 0 - - -12 0 - - 12 - - - 0 - 0 - - - -12 - - Create F Create a FLOW GRAPH with the minimum weight from that path 12 B D 12 20 16 sink 12 12 F A 10 4 9 7 source 4 13 “Flow Graph” C E 14 TO F capacity A B C D E F A B flow in one direction is negative flow in the other direction C FROM D E F
- 4 13 - - - 12 - 10 0 - - - 4 - - 14 - - 12 9 - - 8 - - - 7 - 4 - - - 12 - - R = C - F Create a RESIDUAL GRAPH with the rest of the capacity after that flow 0 B D 12 8 4 sink 12 12 F A 10 4 9 7 source 4 13 “Residual Graph” C E 14 TO R capacity A B C D E F A B reverse edges allow the "undoing" of previous flow! C FROM D E F
Keep going! Continue running this on the residual capacity until BFS fails… 12/12 B D 19/20 11/16 sink F A 0/10 1/4 0/9 7/7 source 4/4 12/13 C E 11/14 There is no longer a path from A to F !
1. Set F to all 0 - 12 11 - - - 4 - 9 9 - - 2 5 - 2 5 - - 3 7 - 1 18 - - 5 6 - 0 - - - 2 4 - The max flow algorithm 2. Compute R. 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). Floyd-Fulkerson 12 B D 3 20 16 sink 2 4 1 0 F A 10 4 2 9 1 7 2 source 4 4 13 C 5 E 14 TO R capacity A B C D E F flow A residual B 3 B D 12 18 C 9 FROM 4 2 D A 1 9 2 5 6 F 7 2 E 5 C E 4 11 F “Residual Graph” 9
Pseudo-code (Python) 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 A little bit of name contention… edmonds_karp is really Floyd-Fulkerson, but with other people getting the credit…
Problem Set #2: Graph algorithms Input 2 0 0 2 2 0 0 0 1 0 1 0 2 0 2 1 2 1 2 2 2 -1 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 -1 0 number of dimensions starting point and destination point edges in the graph next number of dimensions 1 2 2 2 0 2 0 1 Output Maze #1 can be traveled Maze #2 cannot be traveled 0 0
Problem Set #2: Graph algorithms Input "single point of failure" 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 undirected edges in the graph end of graph signal first graph second graph
Problem Set #2: Graph algorithms Input 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 undirected edges in the graph end of graph signal first graph Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes second graph
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph To generate:52-bit numbers in a graph with 4 edges 2 1 3 end of input signal Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure:
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph To generate:52-bit numbers in a graph with 4 edges 2 step 3 step 2 1 3 end of input signal step 1 00 11 10 11 Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure: start
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph Output 2 No Yes some bits are biased more than 75% 1 "reasonably random" - all bits are between 25% and 75% likely to be the value 0 or 1 3 Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure:
Jotto! Sophomores Juniors Seniors Me fjord 3 fjord 0 fjord 1 fjord 2 ????? ? ????? ? ????? ? ????? ? ????? ? ????? ? ????? ? ????? ? 2121 words scoring 0 vs. fjord 2682 words scoring 1 vs. fjord There are 1060 words scoring 2 vs. fjord 132 words scoring 3 vs. fjord 4 words scoring 4 vs. fjord
Next time - A second mock mock contest
ACM today missing entries? let me know… This week's problems… slacking! not slacking… New and improved :-) ACM table
0 8 13 - 1 - 0 - 6 12 - 9 0 - - 7 - 0 0 - - - - 11 0 All-pairs shortest paths “Floyd-Warshall algorithm” B Adjacency Matrix 8 9 to A D E B C A 13 C A 6 B 0 from C 7 1 12 D D E E 11 0 D0 = (dij ) 0 dij = shortest distance from i to j through no nodes k dij = shortest distance from i to j through nodes {1, …, k}
0 8 13 - 1 - 0 - 6 12 - 9 0 - - 7 - 0 0 - - - - 11 0 All-pairs shortest paths... “Floyd-Warshall algorithm” D E B C A Before After D E B C A A 0 8 13 - 1 A B - 0 - 6 12 B C - 9 0 - - C D 7 15 0 0 8 D E - - - 11 0 E 0 D0 = (dij ) 1 D1 = (dij ) k dij = shortest distance from i to j through {1, …, k}
All-pairs shortest paths... A 0 8 13 14 1 A 0 8 13 14 1 B 13 0 6 6 12 B - 0 - 6 12 C 22 9 0 15 21 C - 9 0 15 21 D 7 9 0 0 8 D 7 15 0 0 8 E 18 20 11 11 0 E - - - 11 0 4 D4 = (dij ) 2 D2 = (dij ) A 0 8 12 12 1 A 0 8 13 14 1 B 13 0 6 6 12 B - 0 - 6 12 C 22 9 0 15 21 C - 9 0 15 21 D 7 9 0 0 8 D 7 9 0 0 8 E 18 20 11 11 0 E - - - 11 0 5 3 D5 = (dij ) D3 = (dij ) to store the path, another matrix can track the last intermediate vertex
Thanks to Wikipedia… Floyd Warshall… How would you change this to find longest paths? What has to be true about the graph?
- 16 13 - - - - - 10 12 - - - 4 - - 14 - - - 9 - - 20 - - - 7 - 4 - - - - - - Max Flow The problem how much traffic can get from the source to the sink ? 12 B D 20 16 sink F A 10 4 9 7 source 4 13 “Capacity Graph” C E 14 TO C capacity A B C D E F A B C FROM D E F
- 12 0 - - - -12 - 0 12 - - - 0 - - 0 - - -12 0 - - 12 - - - 0 - 0 - - - -12 - - Create F Create a FLOW GRAPH with the minimum weight from that path 12 B D 12 20 16 sink 12 12 F A 10 4 9 7 source 4 13 “Flow Graph” C E 14 TO F capacity A B C D E F A B flow in one direction is negative flow in the other direction C FROM D E F
- 4 13 - - - 12 - 10 0 - - - 4 - - 14 - - 12 9 - - 8 - - - 7 - 4 - - - 12 - - R = C - F Create a RESIDUAL GRAPH with the rest of the capacity after that flow 0 B D 12 8 4 sink 12 12 F A 10 4 9 7 source 4 13 “Residual Graph” C E 14 TO R capacity A B C D E F A B reverse edges allow the "undoing" of previous flow! C FROM D E F
Pseudo-code (Python) 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 A little bit of name contention… edmonds_karp is really Floyd-Fulkerson, but with other people getting the credit…
Problem Set #2: Graph algorithms Input 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 undirected edges in the graph end of graph signal first graph Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes second graph
Problem Set #2: Graph algorithms Input Graphs always have 2bits nodes. 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 To generate:102-bit numbers in a graph with 3 edges undirected edges in the graph Output 2 No Yes some bits are biased more than 75% 1 "reasonably random" - all bits are between 25% and 75% likely to be the value 0 or 1 3 Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # 0 procedure:
Jotto! A word-guessing game similar to mastermind… Sophomores Juniors Seniors Me fjord 3 fjord 0 fjord 1 fjord 2 ????? ? ????? ? ????? ? ????? ? ????? ? ????? ? ????? ? ????? ?
to A B C D E A B from C D E
Problem Set #1: multiple.java 2 6 3 19 0 Input 0 marks the end of the input integers (N) with 2 < N < 10000 change!