7/1/15 ¡ How does discrete math help us How does discrete math help (CS160)? n Helps create a solution (program) n Helps create a solution (program) q Logic helps you understand conditionals q Logic, functions, tuples, sets help with classes/ objects/methods, and later with databases n Helps analyze a program (relational algebra and relational calculus) q Loop invariants help you create iterative solutions n Helps analyze a program q Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? n Helps create a solution (program) Mathematical Induction q Induction helps you create recursive solutions n Builds on logic, algebra, logical thinking, proof techniques from CS160 n Helps analyze a program Rosen Chapter 5 q Prove program correctness n Induction q Performance (actually computational complexity – time and space) n Counting, permutations and combinations 1 ¡
7/1/15 ¡ Motivation Mathematical Induction and the Domino Principle n Many mathematical statements have the form: If ∀ n ∈ N , P(n) P(n): Logical predicate the 1 st domino falls over n Example: For every positive value of n, and 1 + 2 + ,…, + n = n(n + 1)/2. the n th domino falls over implies that domino ( n + 1) falls over n Predicate – propositional function that depends on a variable, and has a truth value once the then variable is assigned a value. domino n falls over for all n ∈ N. n Mathematical induction is a proof technique for proving such statements image from http://en.wikipedia.org/wiki/File:Dominoeffect.png A Geometrical interpretation A Geometrical interpretation 1: 2: n 3: Put these blocks, which represent numbers, together to form sums: n 1 + 2 = Area is n 2 /2 + n/2 = n(n + 1)/2 1 + 2 + 3 = 2 ¡
7/1/15 ¡ Proving P(3) Example : 1 + 2 + … + n = n ( n + 1)/2. n Let F(n) = 1 + 2 + … + n n Suppose we know: P(1) ∧ (P( n ) → P( n + 1)) ∀ n ≥ 1. Prove: P(3) n P(n): F(n) = n(n + 1) / 2 n Proof: n Verify: P(1): 1(1 + 1)/2 = 1 . 1. P(1). [premise] n Assume P(n), i.e. F(n) = n(n + 1)/2 2. P(1) → P(2). [specialization of premise] n Show P(n+1): F(n + 1) = (n + 1)(n + 2)/2 . 3. P(2). [step 1, 2, & modus ponens] F(n + 1) = 1 + 2 + . . . + n + (n + 1) 4. P(2) → P(3). [specialization of premise] 5. P(3). [step 3, 4, & modus ponens] = F(n) + n + 1 = n(n + 1)/2 + n + 1 [Induction hyp.] We can construct a proof for every finite value of n = n(n + 1)/2 + 2(n + 1)/2 n Modus ponens: if p and p → q then q = (n + 1)(n + 2)/2. The Principle of Mathematical Induction Proof by induction n Let P(n) be a statement that, for each natural n 3 steps: number n, is either true or false. q Prove P(1). [the basis] q Assume P(n) [the induction hypothesis] n To prove that ∀ n ∈ N , P(n) , it suffices to prove: q Using P(n) prove P(n + 1) [the inductive step] q P(1) is true. (base case) q ∀ n ∈ N , P(n) → P(n + 1) . (inductive step ) n This is not magic. n It is a recipe for constructing a proof for an arbitrary n ∈ N . 3 ¡
7/1/15 ¡ Example Example Ø Show that any postage of ≥ 8¢ can be n Let P(n) be the statement “n cents postage can be obtained using 3¢ and 5¢ stamps”. obtained using 3¢ and 5¢ stamps. n Want to show that Ø First check for a few values: “P(k) is true” implies “P(k+1) is true” 8¢ = 3¢ + 5¢ for all k ≥ 8. 9¢ = 3¢ + 3¢ + 3¢ n 2 cases: 1) P(k) is true and 10¢ = 5¢ + 5¢ the k cents contain at least one 5¢. 11¢ = 5¢ + 3¢ + 3¢ 2) P(k) is true and 12¢ = 3¢ + 3¢ + 3¢ + 3¢ the k cents do not contain any 5¢. Ø How to generalize this? Example Example Case 1: k cents contain at least one 5¢ stamp. n Show that 1 + 3 + 5 + ... + (2n+1) = (n+1) 2 Replace 5¢ stamp by two k cents 3¢ stamps k+1 cents 5¢ 3¢ 3¢ Case 2: k cents do not contain any 5¢ stamp. Then there are at least three 3¢ stamp. Replace three 3¢ stamps by k cents k+1 cents two 5¢ stamps 3¢ 3¢ 3¢ 5¢ 5¢ 15 4 ¡
7/1/15 ¡ Now prove the basis step Now complete the proof n Given P(k), prove P(k+1), i.e. n Given n 1 + 2 + 2 2 + … + 2 k = 2 k + 1 – 1 n Prove q 1 + 2 + 2 2 + … + 2 k+1 = 2 k + 2 – 1 Examples Strong induction n Show that 1 + 3 + 5 + ... + (2n+1) = (n+1) 2 n Induction: n Show that 1 + 2 + 2 2 + … + 2 n = 2 n + 1 – 1 q P(1) is true. q ∀ n ∈ N, P(n) → P(n + 1). n Show that for n ≥ 4 2 n < n! q Implies ∀ n ∈ N, P(n) n Show that n 3 –n is divisible by 3 for every n Strong induction: positive n. q P(1) is true. n Prove that a set with n elements has 2 n q ∀ n ∈ N, (P(1) ∧ P(2) ∧ … ∧ P(n)) → P(n + 1). subsets q Implies ∀ n ∈ N, P(n) 5 ¡
7/1/15 ¡ Example Example n Show that n+1 can be represented as a n Prove that all natural numbers ≥ 2 can be product of primes. represented as a product of primes. q If n+1 is a prime: It can be represented as a product of 1 prime, itself. n Basis: 2: 2 is a prime. q If n+1 is composite: Then, n + 1 = ab, for some n Assume that 1, 2, … , n can be represented as a a,b < n + 1. product of primes. n Therefore, a = p 1 p 2 . . . p k & b = q 1 q 2 . . . q l , where the p i s & q i s are primes. n Represent n+1 = p 1 p 2 . . . p k q 1 q 2 . . . q l. Breaking chocolate All horses have the same color Theorem : Breaking up a n Base case: If there is only one horse, there is chocolate bar with n “squares” only one color. takes n-1 breaks. n Induction step: q Assume as induction hypothesis that within any set of n horses, there is only one color. q Now look at any set of n + 1 horses. Number them: 1, 2, 3, ..., n, n + 1. Consider the sets {1, 2, 3, ..., n} and {2, 3, 4, ..., n + 1}. Each is a set of only n horses, therefore within each there is only one color. But the two sets overlap, so there must be only one color among all n + 1 horses. n This is clearly wrong, but can you find the flaw? 6 ¡
7/1/15 ¡ All horses have the same color Induction and Recursion n Induction is useful for proving correctness/ n The inductive step requires that k >= 3, otherwise there is not intersection! So P(2) design of recursive algorithms should be the base case, which is obviously n Example incorrect. // Returns base ^ exponent. // Precondition: exponent >= 0 n In the book there is a similar example. public static int pow(int x, int n) { if (n == 0) { // base case; any number to 0th power is 1 return 1; } else { // recursive case: x^n = x * x^(n-1) return x * pow(x, n-1); } } Induction and Recursion Induction and Recursion public static int pow(int x, int n) { n n! of some integer n can be characterized as: if (n == 0){ n! = 1 for n = 0; otherwise return 1; } else { n! = n (n - 1) (n - 2) ... 1 return x * pow(x, n-1); n Want to write a recursive method for computing it. We } notice that n! = n (n – 1)! } n This is all we need to put together the method: Claim: the algorithm correctly computes x n . public static int factorial(int n) { Proof: By induction on n if (n == 0) { Base case: n = 0: it correctly returns 1 return 1; } else { Inductive step: assume that for n the algorithm correctly return n * factorial(n-1); returns x n . } Then for n+1 it returns x x n = x n+1 . } 7 ¡
7/1/15 ¡ Hanoi Induction in CS via to n Induction is a powerful tool for showing from algorithm correctness – not just for recursive Recall the recursive solution algorithms (CS320) public void hanoi(int n, int from, int to, int via) { if (n>0) { hanoi(n-1,from, via, to); System. out.println("move disk " + n + " from " + from + " to " + to); hanoi(n-1,via, to, from); } } • If you assume the inductive hypothesis that you can move k=n-1 disks, then adding one more disk is easy. • For the k+1 case (where k+1 = n), move the top k disks, move the bottom disk, and move the top k disks. More induction examples Celebrity problem n A celebrity is a guest at a party who is known to n Let n be a positive integer. Show that every 2 n x every other guest, but knows none of them 2 n chessboard with one square removed can be n At most one celebrity at a party. tiled using right triominoes, each covering three squares at a time. n To find out who is a celebrity, you can ask any guest G i a question of the form “Do you know G j ?” n Prove that you need at most 3(n-1) questions. n P(1) is vacuously true! You don’t need to ask. If you did ask, it can become a philosophical question! Do you know yourself? 8 ¡
Recommend
More recommend