350 likes | 438 Views
CS 173: Discrete Mathematical Structures. Cinda Heeren heeren@cs.uiuc.edu Siebel Center, rm 2213 Office Hours: W 12:30-2:30. CS 173 Announcements. Homework #10 available, due 11/20, 8a. Final exam 12/14, 8-11a. Email Cinda with conflicts.
E N D
CS 173:Discrete Mathematical Structures Cinda Heeren heeren@cs.uiuc.edu Siebel Center, rm 2213 Office Hours: W 12:30-2:30
CS 173 Announcements • Homework #10 available, due 11/20, 8a. • Final exam 12/14, 8-11a. Email Cinda with conflicts. Cs173 - Spring 2004
We understand how abstract this is. Some of us think cs125 should be a prerequisite for this course. The only algorithms you have as examples are mergesort and binary search. CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) What do the algorithms look like? • Divide the problem into a subproblems of size n/b. • Solve those subproblems (recursively). • Conquer the solution in time f(n). Cs173 - Spring 2004
f(n) a f(n/b) f(n/b) f(n/b) f(n/b) a a a a a f(n/b2) f(n/b2) f(n/b2) Total running time is sum of the values in the pink rectangles. We don’t have simple recipes for solving these in all cases, though sometimes we do… CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) To solve a problem of size n, we require time f(n), plus the time it takes to solve a subproblems of size n/b. Cs173 - Spring 2004
f(n) a f(n/b) f(n/b) f(n/b) f(n/b) a a a a a f(n/b2) f(n/b2) f(n/b2) Sum over levels… n b logbn no clue How many? CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) Cs173 - Spring 2004
f(n) a f(n/b) f(n/b) f(n/b) f(n/b) a a a a a f(n/b2) f(n/b2) f(n/b2) a ai i·a no clue How many blocks at level i? CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) Cs173 - Spring 2004
f(n) a f(n/b) f(n/b) f(n/b) f(n/b) a a a a a f(n/b2) f(n/b2) f(n/b2) n/bi n log b i·b no clue ? CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) Cs173 - Spring 2004
We no longer have recursive terms, but we do have a sum to deal with. a = 1, b = 2, f(n) = 1. CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) Consider binary search, and write a recurrence for the # of comparisons: T(n) = T(n/2) + 1 Cs173 - Spring 2004
We no longer have recursive terms, but we do have a sum to deal with. a = 2, b = 2, f(n) = n-1. CS 173 Divide and Conquer Recurrences General form: T(n) = aT(n/b) + f(n) How about our old favorite merge sort? T(n) = 2T(n/2) + n Cs173 - Spring 2004
A beautiful problem from your text. • P(n,2) • C(n,2) • 2n • 2n • No clue Can we do better? C(n,2) comparisons means O(n2) algorithm. CS 173 Divide and Conquer Recurrences The closest pair problem. Suppose I ask you to find the closest pair of points among n points in the plane. How many comparisons would you need to do if you have to compare every pair of points? Cs173 - Spring 2004
A beautiful problem from your text. How do we check the pairs that span the line? CS 173 Divide and Conquer Recurrences We hope to be able to divide the problem in half, solve the subproblems, and use them to finish the task. The solution to the problem is given by a pair of pts on the Left, or on the Right, or spanning the line. Suppose we have solved the subproblems, and that dL and dR are the min distances on the left and right, respectively. The closest pair of points will be the minimum distance pair among those on the left with distance dL or those on the right with distance dR or some pair spanning the line. Cs173 - Spring 2004
How do we check the pairs that span the line? Assume we have stored the points in two lists: one sorted by x coord and the other by y coord. CS 173 Divide and Conquer Recurrences First of all, we only care about points that are nearer each other than the best pair on left or right. Let d = min{dL, dR}. Only points within distance d of the line need to be considered. How many of those points might there be? For each of those points, how many distances must be computed? We are in trouble if we must compute all pairs! For each point we must check only the next 7 points in the list. Cs173 - Spring 2004
CS 173 Divide and Conquer Recurrences Any pair of points closer than d and spanning the line will live in a d x 2d rectangle: Thus, for any point P, only points whose y value is less than d away need to be considered. How many of these points can there be? There will be at most 3 on P’s side of the line, and 4 on the other side of the line, for a total of 7. For each of the 7 points pi succeeding P in the list of candidate points we ask “is pi on the opposite side of the line?” and, if yes, then we ask, “is the distance from pi to P less than d?” Cs173 - Spring 2004
CS 173 Divide and Conquer Recurrences The algorithm: • Create list Lx of the points sorted by x coordinate. • Create list Ly of the points sorted by y coordinate. • Divide the set of points into two parts by line L and recursively find nearest pair on left and nearest pair on right (distances dL and dR). • Let d be min{dL, dR}. • Filter Ly (creating Ly’ to include only points within distance d from line L. • For each point in Ly’, compute distances to each of next 7 points in Ly’. • If any of those are on opp side of L, and are closer than d, then update the nearest pair and d. Cs173 - Spring 2004
Total running time: O(n log n) + T(n), T(n) = 2T(n/2) + 8n O(n log n) CS 173 Divide and Conquer Recurrences O(n log n) O(n log n) 2T(n/2) • Create list Lx of the points sorted by x coordinate. • Create list Ly of the points sorted by y coordinate. • Divide the set of points into two parts by line L and recursively find nearest pair on left and nearest pair on right (distances dL and dR). • Let d be min{dL, dR}. • Filter Ly (creating Ly’ to include only points within distance d from line L. • For each point in Ly’, compute distances to each of next 7 points in Ly’. • If any of those are on opp side of L, and are closer than d, then update the nearest pair and d. O(1) O(n) 7n O(1) Cs173 - Spring 2004
You should check that this works for the recurrences we’ve seen here. CS 173 Master Theorem The recurrence T(n) = aT(n/b) + f(n) can be solved as follows: If a·f(n/b) = kf(n) for some constant k < 1, then T(n) = (f(n)) If a·f(n/b) = Kf(n) for some constant K > 1, then T(n) = (nlogb a) If a·f(n/b) = f(n), then T(n) = (f(n)logb n) Cs173 - Spring 2004
CS 173 Relations • Recall the definition of the Cartesian (Cross) Product: The Cartesian Product of sets A and B, A x B, is the set A x B = {<x,y> : xA and yB}. • A relation is just any subset of the CP!! R AxB • Ex: A = students; B = courses. R = {(a,b) | student a is enrolled in class b} Cs173 - Spring 2004
Yes, a function is a special kind of relation. CS 173 Relations • Recall the definition of a function: f = {<a,b> : b = f(a) , aA and bB} • Is every function a relation? • Draw venn diagram of cross products, relations, functions Cs173 - Spring 2004
CS 173 Properties of Relations • Reflexivity: A relation R on AxA is reflexive if for all aA, (a,a) R. • Symmetry: A relation R on AxA is symmetric if (x,y) R implies (x,y) R . Cs173 - Spring 2004
CS 173 Properties of Relations • Transitivity: A relation on AxA is transitive if (a,b) R and (b,c) R imply (a,c) R. • Anti-symmetry: A relation on AxA is anti-symmetric if (a,b) R implies (b,a) R. Cs173 - Spring 2004
1 2 3 A “short cut” must be present for EVERY path of length 2. 4 CS173Properties of Relations - techniques… How can we check for transitivity? Draw a picture of the relation (called a “graph”). • Vertex for every element of A • Edge for every element of R Now, what’s R? {(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)} Cs173 - Spring 2004
1 2 3 Loops must exist on EVERY vertex. 4 CS173Properties of Relations - techniques… How can we check for the reflexive property? Draw a picture of the relation (called a “graph”). • Vertex for every element of A • Edge for every element of R Now, what’s R? {(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)} Cs173 - Spring 2004
1 2 3 EVERY edge must have a return edge. 4 CS173Properties of Relations - techniques… How can we check for the symmetric property? Draw a picture of the relation (called a “graph”). • Vertex for every element of A • Edge for every element of R Now, what’s R? {(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)} Cs173 - Spring 2004
1 2 3 No edge can have a return edge. 4 CS173Properties of Relations - techniques… How can we check for the anti-symmetric property? Draw a picture of the relation (called a “graph”). • Vertex for every element of A • Edge for every element of R Now, what’s R? {(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)} Cs173 - Spring 2004
? 2 1 2 1 1 ? ? 3 Yes Yes No No CS173Properties of Relations - techniques… Let R be a relation on People, R={(x,y): x and y have lived in the same country} Is R transitive? Is it symmetric? Is it reflexive? Is it anti-symmetric? Cs173 - Spring 2004
Yes Definition of “divides” Is (x,z) in R? CS173Properties of Relations - techniques… Let R be a relation on positive integers, R={(x,y): 3|(x-y)} Suppose (x,y) and (y,z) are in R. Then we can write 3j = (x-y) and 3k = (y-z) Can we say 3m = (x-z)? Add prev eqn to get: 3j + 3k = (x-y) + (y-z) 3(j + k) = (x-z) Is R transitive? Cs173 - Spring 2004
Yes Yes Definition of “divides” Yes, for k=0. CS173Properties of Relations - techniques… Let R be a relation on positive integers, R={(x,y): 3|(x-y)} Is (x,x) in R, for all x? Does 3k = (x-x) for some k? Is R transitive? Is it reflexive? Cs173 - Spring 2004
Yes Yes Yes Definition of “divides” Yes, for k=-j. CS173Properties of Relations - techniques… Let R be a relation on positive integers, R={(x,y): 3|(x-y)} Suppose (x,y) is in R. Then 3j = (x-y) for some j. Does 3k = (y-x) for some k? Is R transitive? Is it symmetric? Is it reflexive? Cs173 - Spring 2004
Yes Yes Yes No Definition of “divides” Yes, for k=-j. CS173Properties of Relations - techniques… Let R be a relation on positive integers, R={(x,y): 3|(x-y)} Suppose (x,y) is in R. Then 3j = (x-y) for some j. Does 3k = (y-x) for some k? Is R transitive? Is it symmetric? Is it reflexive? Is it anti-symmetric? Cs173 - Spring 2004
CS173More than one relation Suppose we have 2 relations, R1 and R2, and recall that relations are just sets! So we can take unions, intersections, complements, symmetric differences, etc. There are other things we can do as well… Cs173 - Spring 2004
B C A R S 1 x s 2 y t 3 z u 4 v CS173More than one relation Let R be a relation from A to B (R AxB), and let S be a relation from B to C (S BxC). The composition of R and S is the relation from A to C (SR AxC): SR = {(a,c): bB, (a,b) R, (b,c) S} SR = {(1,u),(1,v),(2,t),(3,t),(4,u)} Cs173 - Spring 2004
CS173More than one relation Let R be a relation on A. Inductively define R1 = R Rn+1 = Rn R A A A R R1 1 1 1 2 2 2 3 3 3 4 4 4 R2 = R1R = {(1,1),(1,2),(1,3),(2,3),(3,3),(4,1), (4,2)} Cs173 - Spring 2004
… = R4 = R5 = R6… CS173More than one relation Let R be a relation on A. Inductively define R1 = R Rn+1 = Rn R A A A R R2 1 1 1 2 2 2 3 3 3 4 4 4 R3 = R2R = {(1,1),(1,2),(1,3),(2,3),(3,3),(4,1),(4,2),(4,3)} Cs173 - Spring 2004
CS173Relations - A Theorem: If R is a transitive relation, then Rn R, n. Aside: notice that this theorem allows us to conclude that the previous relation was NOT transitive. Recall: “if p then q” “if not q then not p.” We saw that Rn was not a subset of R (it was growing on every iteration). Therefore, R is not transitive. Cs173 - Spring 2004
Typical way of proving subset. CS173Relations - A Theorem: If R is a transitive relation, then Rn R, n. Proof by induction on n. Base case (n=1): R1 R because by definition, R1= R. IH: if R is transitive, then Rn R. Prove: if R is transitive, then Rn+1 R. We are trying to prove that Rn+1 R. To do this, we select an element of Rn+1 and show that it is also an element of R. Let (a,b) be an element of Rn+1. Since Rn+1 = Rn R, we know there is an x so that (a,x) R and (x,b) Rn. By IH, since Rn R, (x,b) R. But wait, if (a,x) R, and (x,b) R, and R is transitive, then (a,b) R. Cs173 - Spring 2004