Download presentation
Presentation is loading. Please wait.
Published byGabriel Potter Modified over 9 years ago
1
ACM today This week's problems… New and improved :-) ACM table slacking! not slacking… missing entries? let me know…
2
Mock mock contest ? Lots of teams solved problems All problems were solved by some team honeymarblesbirthday
3
Mock mock contest ? Lots of teams solved problems All problems were solved by some team honeymarblesbirthday Keep track of the number of paths of length K that end in each cell go to K+1 … From 2d to 1d: compute the best horizontal and vertical displacements separately Create a "containment" graph of nesting boxes, and then find the LONGEST path in it.
4
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…
5
student projects: summer 2007 Kaylin Spitz Emma Liu measuring centrality in very large connection networks (of proteins) 4,519 nodes 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.
6
All-pairs shortest paths A B E D C 8 13 1 6 12 9 7 0 11 0813-1 -0-612 -90-- 7-00- ---110 A B C D E A BC DE from to “Floyd-Warshall algorithm” D 0 = (d ij ) 0 d ij = shortest distance from i to j through nodes {1, …, k} k d ij = shortest distance from i to j through no nodes 0 Adjacency Matrix
7
All-pairs shortest paths... D 0 = (d ij ) 0 0813-1 -0-612 -90-- 7-00- ---110 d ij = shortest distance from i to j through {1, …, k} k “Floyd-Warshall algorithm” Before A B C D E A BC DE D 1 = (d ij ) 1 0813-1 -0-612 -90-- 7 15 00 8 ---110 After A B C D E A BC DE
8
All-pairs shortest paths... D 0 = (d ij ) 0 0813-1 -0-612 -90-- 7-00- ---110 A B C D E D 1 = (d ij ) 1 d ij = shortest distance from i to j through {1, …, k} k 0813-1 -0-612 -90-- 7 15 00 8 ---110 “Floyd-Warshall algorithm” Before After A B C D E A BC DE A B C D E A BC DE + ) d ij = k d ij k-1, d ik k-1 d kj k-1 min(
9
All-pairs shortest paths... 0813 14 1 -0-612 -901521 715008 ---110 A B C D E D 2 = (d ij ) 2 0813141 -0-612 -901521 7 9 008 ---110 A B C D E D 3 = (d ij ) 3 0813141 130 6 612 22901521 79008 182011 0 A B C D E D 4 = (d ij ) 4 A B C D E D 5 = (d ij ) 5 to store the path, another matrix can track the last intermediate vertex 0812 1 1306612 22901521 79008 182011 0
10
Thanks to Wikipedia… Floyd Warshall… How would you change this to find longest paths? What has to be true about the graph?
11
Problem Set #2: Graph algorithms
12
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
13
Problem Set #2: Graph algorithms 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 Input undirected edges in the graph end of graph signal "single point of failure" first graph second graph
14
Problem Set #2: Graph algorithms 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 Input undirected edges in the graph end of graph signal Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes first graph second graph
15
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph end of input signal To generate: 10 2-bit numbers in a graph with 3 edges To generate: 5 2-bit numbers in a graph with 4 edges Graphs always have 2 bits nodes. 1 2 3 0 procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's #
16
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph end of input signal To generate: 10 2-bit numbers in a graph with 3 edges To generate: 5 2-bit numbers in a graph with 4 edges Graphs always have 2 bits nodes. 1 2 3 0 procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # step 1 step 2 step 3 00 11 10 11 start
17
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph To generate: 10 2-bit numbers in a graph with 3 edges Graphs always have 2 bits nodes. procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # Output No Yes "reasonably random" - all bits are between 25% and 75% likely to be the value 0 or 1 some bits are biased more than 75% 1 2 3 0
18
Jotto! SophomoresJuniorsSeniorsMe fjord 3fjord 0fjord 1fjord 2 tempt 1 marks 1 There are 2121 words scoring 0 vs. fjord 2682 words scoring 1 vs. fjord 1060 words scoring 2 vs. fjord 132 words scoring 3 vs. fjord 4 fjord: fords, frond, fordy, fardo, foder, dorje, tempt 1 marks 3 tempt 2 marks 0 tempt 0 marks 1
19
Next time - A second mock mock contest
20
Max Flow Max network 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 ?
21
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
22
Find a path in C 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 “Capacity Graph” source sink capacity C A B C D E ABCDE F F
23
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
24
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 --- --
25
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 !
26
The max flow algorithm A B E D C 13 A B C D E FROM ABCDE F -1211-- 4-99- 25-25 -37-1 --56- - - - 18 0 ---24- F F 16 104 9 12 14 7 20 4 TO “Residual Graph” source sink capacity 4 3 2 1 5 2 2 1 0 4 flow R 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. 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).
27
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…
28
Problem Set #2: Graph algorithms
29
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
30
Problem Set #2: Graph algorithms 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 Input undirected edges in the graph end of graph signal "single point of failure" first graph second graph
31
Problem Set #2: Graph algorithms 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 Input undirected edges in the graph end of graph signal Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes first graph second graph
32
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph end of input signal To generate: 10 2-bit numbers in a graph with 3 edges To generate: 5 2-bit numbers in a graph with 4 edges Graphs always have 2 bits nodes. 1 2 3 0 procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's #
33
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph end of input signal To generate: 10 2-bit numbers in a graph with 3 edges To generate: 5 2-bit numbers in a graph with 4 edges Graphs always have 2 bits nodes. 1 2 3 0 procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # step 1 step 2 step 3 00 11 10 11 start
34
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph To generate: 10 2-bit numbers in a graph with 3 edges Graphs always have 2 bits nodes. procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # Output No Yes "reasonably random" - all bits are between 25% and 75% likely to be the value 0 or 1 some bits are biased more than 75% 1 2 3 0
35
Jotto! SophomoresJuniorsSeniorsMe fjord 3fjord 0fjord 1fjord 2 ????? ? There are 2121 words scoring 0 vs. fjord 2682 words scoring 1 vs. fjord 1060 words scoring 2 vs. fjord 132 words scoring 3 vs. fjord 4 words scoring 4 vs. fjord ????? ?
36
Next time - A second mock mock contest
37
ACM today This week's problems… New and improved :-) ACM table slacking! not slacking… missing entries? let me know…
38
All-pairs shortest paths A B E D C 8 13 1 6 12 9 7 0 11 0813-1 -0-612 -90-- 7-00- ---110 A B C D E A BC DE from to “Floyd-Warshall algorithm” D 0 = (d ij ) 0 d ij = shortest distance from i to j through nodes {1, …, k} k d ij = shortest distance from i to j through no nodes 0 Adjacency Matrix
39
All-pairs shortest paths... D 0 = (d ij ) 0 0813-1 -0-612 -90-- 7-00- ---110 d ij = shortest distance from i to j through {1, …, k} k “Floyd-Warshall algorithm” Before A B C D E A BC DE D 1 = (d ij ) 1 0813-1 -0-612 -90-- 7 15 00 8 ---110 After A B C D E A BC DE
40
All-pairs shortest paths... 0813 14 1 -0-612 -901521 715008 ---110 A B C D E D 2 = (d ij ) 2 0813141 -0-612 -901521 7 9 008 ---110 A B C D E D 3 = (d ij ) 3 0813141 130 6 612 22901521 79008 182011 0 A B C D E D 4 = (d ij ) 4 A B C D E D 5 = (d ij ) 5 to store the path, another matrix can track the last intermediate vertex 0812 1 1306612 22901521 79008 182011 0
41
Thanks to Wikipedia… Floyd Warshall… How would you change this to find longest paths? What has to be true about the graph?
42
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
43
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
44
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 --- --
45
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…
46
Problem Set #2: Graph algorithms 1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 Input undirected edges in the graph end of graph signal Output Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes first graph second graph
47
Problem Set #2: Graph algorithms 10 2 3 0 3 1 3 2 3 5 2 4 0 1 0 3 1 2 2 3 0 0 0 Input undirected edges in the graph To generate: 10 2-bit numbers in a graph with 3 edges Graphs always have 2 bits nodes. procedure: Start at a random node, generate its node # Choose a random edge and follow it Generate the destination node's # Output No Yes "reasonably random" - all bits are between 25% and 75% likely to be the value 0 or 1 some bits are biased more than 75% 1 2 3 0
48
Jotto! SophomoresJuniorsSeniorsMe A word-guessing game similar to mastermind… fjord 3fjord 0fjord 1fjord 2 ????? ?
49
A B C D E ABCDE from to
50
Problem Set #1: multiple.java 2 6 3 19 0 integers ( N ) with 2 < N < 10000 0 marks the end of the input Input change!
51
What is this course about? A chance to “improve” your programming skills Algorithm analysis and insight Program design and implementation optimizing coding time ACM programming contest What Why Research/prototype programming Hands-on practice with algorithms and techniques Familiarity with C++’s STL or Java’s API (more options in the spring…)
52
What is this course about? A chance to “improve” your programming skills Algorithm analysis and insight Program design and implementation optimizing coding time ACM programming contest What Why Research/prototype programming Hands-on practice with algorithms and techniques Unofficial course name: CS -70 Familiarity with C++’s STL or Java’s API (more options in the spring…)
53
2006 ACM Results http://socalcontest.acm.org/ http://icpc.baylor.edu/icpc/ Site for the world finals Site for the regionals
54
Class Organization Feedback from prior semesters… more contest-like practice sessions lab sessions not considered best use of time better to have the course graded individually there should be opportunities to start coding “cold” continue to play jotto have better snacks <-- need more feedback here
55
Course Organization Sep 4 Welcome! (topic: dynamic programming) Sep 11 Practice session to work on problems (here - laptops?) Sep 18 Discussion session (topic: graph algorithms) Sep 25 Practice session (here…) Oct 2 Discussion session (topic: search problems) Oct 9 Practice session (here…) Oct 16 Discussion session (topic: geometry/parsing) Oct 23 No class - Fall break Oct 30 Mock ACM contest, 9pm – 1am, teams of 3, in CS labs Nov 6 Brief meeting for the ACM contest participants Nov 10 Regional ACM contest in Riverside Nov 13 Wrap-up session for everyone
56
Problems and grades alternating format discussion sessions lab sessions problem and program analysis strategy, coding tips 3 problems, count individually 3 problems can be done by team or individually -- you decide meet here in this room (?) - bring a laptop - I'll have some too. contest conditions: new problems one computer per team credit for the entire team Problems can be done any time during the term. Problems solved during the lab sessions count as 2 problems before 6pm
57
Course webpage reference links administrative info problem statements and example I/O people results!!
58
Grading CS 189 is graded individually... (it’s possible to take it P/F, too) Coding Guidelines problems can be done any time during the semester discussion of algorithms always OK coding should be within teams during lab "contests", you may only use C++ or Java API references anyother time, you may use any reference at all except an existing solution or partial solution… use /cs/ACM/acmSubmit to submit try things out ! the reason for ACM!
59
Java ! extensive library of data structures and algorithms available Not required (C++ is the alternative), but consider... I/O made simpler with 1.5’s Scanner and printf the C in CS! H.ni() anyone?
60
Jotto! SophomoresJuniorsSeniorsMe A word-guessing game similar to mastermind… vvvvv
61
Problem Set #1: Dynamic Programming
62
Problem Set #1: multiple.java 2 6 3 19 0 integers ( N ) with 2 < N < 10000 0 marks the end of the input Input 10 1110 111 11001 the smallest (decimal) multiple of N with only the digits 0 and 1 ! Output change!
63
Problem Set #1: multiple.java 2 6 3 19 0 integers ( N ) with 2 < N < 10000 0 marks the end of the input Input 10 1110 111 11001 the smallest (decimal) multiple of N with only the digits 0 and 1 ! Output change! Ideas? Most naïve solution… …how can we save time?
64
Dynamic programming Storing intermediate results in a table for fast look-up: multiple.java input N = 6 possible remainders upon dividing by N (6) # of digits in answer 012345 2 1 3 4
65
Dynamic programming Storing intermediate results in a table for fast look-up: input N = 6 possible remainders upon dividing by N (6) # of digits in answer 012345 2 1 3 4 1
66
Dynamic programming Storing intermediate results in a table for fast look-up: input N = 6 possible remainders upon dividing by N (6) # of digits in answer 012345 2 1 3 4 1 1011 1
67
Dynamic programming Storing intermediate results in a table for fast look-up: input N = 6 possible remainders upon dividing by N (6) # of digits in answer 012345 2 1 3 4 1 111110 1011 1 1011 1
68
Dynamic programming Storing intermediate results in a table for fast look-up: input N = 6 possible remainders upon dividing by N (6) # of digits in answer 012345 2 1 3 4 1 111110 1011 1 1011 1 1110 111110 1011 1
69
Problem Set #1: multiple.java 2 6 3 19 0 integers ( N ) with 2 < N < 10000 0 marks the end of the input Input 10 1110 111 11001 the smallest (decimal) multiple of N with only the digits 0 and 1 ! Output change! Java Classes BigInteger LinkedList int[]
70
Coding tips: multiple.java import java.util.Scanner; import java.util.LinkedList; import java.math.BigInteger; class multiple { public static void pl(String s) { System.out.println(s); } public static void pld(String s) { /* pl(s); */ ; }
71
Coding tips: multiple.java import java.util.Scanner; import java.util.LinkedList; import java.math.BigInteger; class multiple { public static void pl(String s) { System.out.println(s); } public static void pld(String s) { /* pl(s); */ ; } public static void main(String[] argv) { Scanner s = new Scanner(System.in); // for input int[] remainders; // the DP table LinkedList LL; // a queue
72
Coding tips: multiple.java while (true) // while there is input { LL = new LinkedList (); // new, empty queue LL.addLast("1"); // starting value while (LL.size() > 0) // more data left? { String next = LL.removeFirst(); // dequeue from front the LinkedList class is a ready-made Deque
73
Coding tips: multiple.java BigInteger r0_big = (new BigInteger(next0)).remainder(input); int r0 = r0_big.intValue(); // convert to an int if (r0 == 0) { // we're done! pl(next0); // print the answer break; // go to next input } the BigInteger constructor takes a String
74
Coding tips: multiple.java BigInteger r0_big = (new BigInteger(next0)).remainder(input); int r0 = r0_big.intValue(); // convert to an int if (r0 == 0) { // we're done! pl(next0); // print the answer break; // go to next input } the BigInteger constructor takes a String if (remainders[r0] == 0) { // have we seen this one? remainders[r0] = 42; // 42 marks it as seen pld(" " + next0 + "," + r0); // for debugging LL.addLast( next0 ); // put next0 on the queue } our table
75
Problem Set #1: bookcase.java 1 4 220 29 195 20 200 9 180 30 number of data sets Input number of books in this data set height and width of each book
76
Problem Set #1: bookcase.java 1 4 220 29 195 20 200 9 180 30 number of data sets Input number of books in this data set height and width of each book Fit the books tightly onto a rectangular, 3-shelf bookcase: 220 200 180 30 29 20 + 9 30
77
Problem Set #1: bookcase.java 1 4 220 29 195 20 200 9 180 30 number of data sets Input number of books in this data set height and width of each book Fit the books tightly onto a rectangular, 3-shelf bookcase: 220 200 180 30 29 20 + 9 30 Output What is the smallest total area for such a bookshelf?
78
Problem Set #1: bookcase.java 1 4 220 29 195 20 200 9 180 30 number of data sets Thoughts? number of books in this data set height and width of each book Fit the books tightly onto a rectangular, 3-shelf bookcase: 220 200 180 30 29 20 + 9 30 Output What is the smallest total area for such a bookshelf? Input
79
Tracking width, height, and books… Consider the books in order of height (decreasing) width of Shelf #1 # of books in top two shelves 012345 2 1 3 4 1 111110 1011 1 1011 1 1110 111110 1011 1 Key decisions will be when to start using the 2nd + 3rd shelves
80
Coaches’ Room
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.