Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop...

8
7/1/15 1 How does discrete math help us Helps create a solution (program) Helps analyze a program How does discrete math help (CS160)? Helps create a solution (program) Logic helps you understand conditionals Logic, functions, tuples, sets help with classes/ objects/methods, and later with databases (relational algebra and relational calculus) Loop invariants help you create iterative solutions Helps analyze a program Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? Helps create a solution (program) Induction helps you create recursive solutions Builds on logic, algebra, logical thinking, proof techniques from CS160 Helps analyze a program Prove program correctness Induction Performance (actually computational complexity – time and space) Counting, permutations and combinations Mathematical Induction Rosen Chapter 5

Transcript of Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop...

Page 1: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

1  

How does discrete math help us

n  Helps create a solution (program)

n  Helps analyze a program

How does discrete math help (CS160)?

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 (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) q  Induction helps you create recursive solutions

n  Builds on logic, algebra, logical thinking, proof techniques from CS160

n  Helps analyze a program q  Prove program correctness

n  Induction q  Performance (actually computational complexity –

time and space) n  Counting, permutations and combinations

Mathematical Induction

Rosen Chapter 5

Page 2: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

2  

Motivation

n  Many mathematical statements have the form: ∀n ∈ N, P(n)

n  Example: For every positive value of n, 1 + 2 + ,…, + n = n(n + 1)/2. n  Predicate – propositional function that depends

on a variable, and has a truth value once the variable is assigned a value.

n  Mathematical induction is a proof technique for proving such statements

P(n): Logical predicate

Mathematical Induction and the Domino Principle If

the 1st domino falls over and

the nth domino falls over implies that domino (n + 1) falls over

then domino n falls over for all n ∈ N.

image from http://en.wikipedia.org/wiki/File:Dominoeffect.png

A Geometrical interpretation

1: 2: 3: Put these blocks, which represent numbers, together to form sums: 1 + 2 =

1 + 2 + 3 =

n

n

Area is n2/2 + n/2 = n(n + 1)/2

A Geometrical interpretation

Page 3: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

3  

Proving P(3)

n  Suppose we know: P(1) ∧ (P(n) → P(n + 1)) ∀n ≥ 1. Prove: P(3)

n  Proof: 1. P(1). [premise] 2. P(1) → P(2). [specialization of premise] 3. P(2). [step 1, 2, & modus ponens] 4. P(2) → P(3). [specialization of premise] 5. P(3). [step 3, 4, & modus ponens]

We can construct a proof for every finite value of n n  Modus ponens: if p and p → q then q

Example: 1 + 2 + … + n = n(n + 1)/2.

n  Let F(n) = 1 + 2 + … + n n  P(n): F(n) = n(n + 1) / 2 n  Verify: P(1): 1(1 + 1)/2 = 1. n  Assume P(n), i.e. F(n) = n(n + 1)/2 n  Show P(n+1): F(n + 1) = (n + 1)(n + 2)/2.

F(n + 1) = 1 + 2 + . . . + n + (n + 1) = F(n) + n + 1 = n(n + 1)/2 + n + 1 [Induction hyp.] = n(n + 1)/2 + 2(n + 1)/2 = (n + 1)(n + 2)/2.

The Principle of Mathematical Induction

n  Let P(n) be a statement that, for each natural number n, is either true or false.

n  To prove that ∀n∈N, P(n), it suffices to prove: 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.

Proof by induction

n  3 steps: q  Prove P(1). [the basis] q  Assume P(n) [the induction hypothesis] q  Using P(n) prove P(n + 1) [the inductive step]

Page 4: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

4  

Example

Ø  Show that any postage of ≥ 8¢ can be obtained using 3¢ and 5¢ stamps.

Ø  First check for a few values: 8¢ = 3¢ + 5¢ 9¢ = 3¢ + 3¢ + 3¢ 10¢ = 5¢ + 5¢ 11¢ = 5¢ + 3¢ + 3¢ 12¢ = 3¢ + 3¢ + 3¢ + 3¢

Ø  How to generalize this?

Example n  Let P(n) be the statement “n cents postage can be

obtained using 3¢ and 5¢ stamps”. n  Want to show that

“P(k) is true” implies “P(k+1) is true” for all k ≥ 8.

n  2 cases: 1) P(k) is true and the k cents contain at least one 5¢. 2) P(k) is true and the k cents do not contain any 5¢.

Example

15

Case 1: k cents contain at least one 5¢ stamp.

Case 2: k cents do not contain any 5¢ stamp. Then there are at least three 3¢ stamp.

5¢ 3¢ 3¢

Replace 5¢ stamp by two 3¢ stamps k cents k+1 cents

3¢ 3¢

3¢ 5¢ 5¢

Replace three 3¢ stamps by two 5¢ stamps k cents k+1 cents

Example

n  Show that 1 + 3 + 5 + ... + (2n+1) = (n+1)2

Page 5: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

5  

Now prove the basis step Now complete the proof

n  Given P(k), prove P(k+1), i.e. n  Given

n  1 + 2 + 22 + … + 2k = 2k + 1 – 1 n  Prove

q  1 + 2 + 22 + … + 2k+1 = 2k + 2 – 1

Examples

n  Show that 1 + 3 + 5 + ... + (2n+1) = (n+1)2

n  Show that 1 + 2 + 22 + … + 2n = 2n + 1 – 1 n  Show that for n≥4 2n < n! n  Show that n3–n is divisible by 3 for every

positive n. n  Prove that a set with n elements has 2n

subsets

Strong induction

n  Induction: q  P(1) is true. q  ∀n ∈N, P(n) → P(n + 1). q  Implies ∀n ∈N, P(n)

n  Strong induction: q  P(1) is true. q  ∀n ∈N, (P(1) ∧ P(2)∧…∧P(n)) → P(n + 1). q  Implies ∀n ∈N, P(n)

Page 6: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

6  

Example

n  Prove that all natural numbers ≥ 2 can be

represented as a product of primes.

n  Basis: 2: 2 is a prime.

n  Assume that 1, 2,…, n can be represented as a

product of primes.

Example

n  Show that n+1 can be represented as a product of primes. q  If n+1 is a prime: It can be represented as a

product of 1 prime, itself. q  If n+1 is composite: Then, n + 1 = ab, for some

a,b < n + 1. n  Therefore, a = p1p2 . . . pk & b = q1q2 . . . ql, where the pis

& qis are primes. n  Represent n+1 = p1p2 . . . pkq1q2 . . . ql.

Breaking chocolate Theorem: Breaking up a chocolate bar with n “squares” takes n-1 breaks.

All horses have the same color

n  Base case: If there is only one horse, there is only one color. 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?

Page 7: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

7  

All horses have the same color

n  The inductive step requires that k >= 3, otherwise there is not intersection! So P(2) should be the base case, which is obviously incorrect.

n  In the book there is a similar example.

Induction and Recursion

n  Induction is useful for proving correctness/design of recursive algorithms

n  Example // Returns base ^ exponent. // Precondition: exponent >= 0 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 public static int pow(int x, int n) { if (n == 0){ return 1; } else { return x * pow(x, n-1); } } Claim: the algorithm correctly computes xn. Proof: By induction on n Base case: n = 0: it correctly returns 1 Inductive step: assume that for n the algorithm correctly

returns xn. Then for n+1 it returns x xn = xn+1.

Induction and Recursion

n  n! of some integer n can be characterized as: n! = 1 for n = 0; otherwise n! = n (n - 1) (n - 2) ... 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: public static int factorial(int n) { if (n == 0) { return 1;

} else { return n * factorial(n-1);

} }

Page 8: Mathematical Induction - Colorado State University" Prove program correctness (e.g., using loop invariants) How does discrete math help (CS161)? ! Helps create a solution (program)

7/1/15  

8  

Hanoi

Recall the recursive solution 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); } }

from via to

•  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.

Induction in CS

n  Induction is a powerful tool for showing algorithm correctness – not just for recursive algorithms (CS320)

More induction examples

n  Let n be a positive integer. Show that every 2n x 2n chessboard with one square removed can be tiled using right triominoes, each covering three squares at a time.

Celebrity problem n  A celebrity is a guest at a party who is known to

every other guest, but knows none of them n  At most one celebrity at a party. n  To find out who is a celebrity, you can ask any

guest Gi a question of the form “Do you know Gj?” 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?