490 likes | 609 Views
Proof by Induction. CSC 172 SPRING 2002 LECTURE 7. Lectures on Line. http://www.cs.rochester.edu/u/pawlicki/lectures/CSC172. Proof by induction. If I know “something is true for the value 1” And I know that “if it is true for x, then it must also be true for x+1” Is it true for 7? Why?.
E N D
Proof by Induction CSC 172 SPRING 2002 LECTURE 7
Lectures on Line http://www.cs.rochester.edu/u/pawlicki/lectures/CSC172
Proof by induction • If I know “something is true for the value 1” • And I know that “if it is true for x, then it must also be true for x+1” • Is it true for 7? • Why?
Review : Sorting an array • Write a Java method that takes an array of ints and sorts it in place • Describe this algorithm in “words” • Describe “selection sort”
Selection Sort public static void selsort(int [] a){ for (j=0;j<a.length;j++){ int maxIndex = j; for (int k = j;k<a.length;k++) if (a[k]>a[maxIndex]) maxIndex = k; int temp = a[j]; a[j] = a[maxIndex]; a[maxIndex] = temp; } }// How many comparisons, as a function of a.length?
Total number of comparisons N + (N-1) + (N-2) + . . . + 1 Reversing 1 + 2 + 3 + . . . + N =
In order to calculate work • Prove
Simple Induction • Three Pieces • A statementS(n) to be proved • The statement must be about an integer parameter n • A basis for the proof • The statement S(b) for some specific integer b • Often b==0 or b==1 • An inductive step for the proof • Show that “If S(n) is true, then S(n+1) must also be true” • Prove the statement “S(n) implies S(n+1)” for any n. • For this part, you get to “suppose” S(n) is true • “For the sake of argument” • Aka the inductive hypothesis
Prove: 1. Statement: S(n) : For any n>=1
Prove: 2. Basis Select n == 1
Prove: 2. Alternate Basis Select n == 2
Prove: 2. Alternate Basis Select n == 3
Prove: 3. Inductive Step Assume: To show:
Inductive Step We know, by definition: Rewrite it:
Inductive Step “By the Induction hypothesis” (we can make the following substitution)
Inductive Step • The rest is just algebra
So, what did we do • We showed that it worked for 1 • And that if it worked for n, it must work for n+1 • So, is it true for n==7? • Why? • Is it true for n==984375984237598437594373457?
Template for Simple Induction • State what S(n) is. • Explain what n represents. “any positive integer” or “length of the string” • Tell what the value of n is for the basis case n==b • Prove S(b) • State that you are assuming n>=b and S(n) • Prove S(n+1) using the assumptions (say: “B.T.I.H.”) • State that due to (4) and (6) you conclude S(n) for all n>=b
Interesting Aside: Visual Proof • Proof is “convincing prose” • Not all proof is “mathematical”
Visual Proof n 3 2 1 0 1 2 3 . . n
Visual Proof n 3 2 1 0 1 .. n/2 . . n
Visual Proof n 3 2 1 0 1 .. n/2
Visual Proof n 3 2 1 0 1 .. n/2
Visual Proof n 3 2 1 0 1 .. n/2
Visual Proof n+1 n 3 2 1 0 1 .. n/2
More General Induction • More than one basis case • Complete (Strong) indcution • Proof that S(n+1) is true uses any of S(b),S(b+1),…,S(n) • Where b is the lowest basis case
Example • Multiple Basis cases & Strong Induction • Every integer >= 24 can be written as 5a+7b for non-negative integers a and b . • Is this true for 16? For 19? For 23?
Statement • S(n): n ==5a+7b for some a>=0, and b>=0
Basis We need 5 24 == 5x2+7x2 25 == 5x5+7x0 26 == 5x1+7x3 27 == 5x4+7x1 28 == 5x0+7x4
Inductive Step Let (n+1) >= 29 Then (n-4) >= 24, which is the lowest basis case So, assume S(n-4) is true We can write: n-4 == 5a+7b for some a and b Thus: n+1 == 5(a+1)+7b, proving S(n+1)
An Interesting Sequence 1 + 2 + 4 + 8 + . . . + n What is the pattern? What is it’s value? Why is it interesting?
Why is it interesting? • How many nodes at each level of a binary tree? So, how many nodes, all told? 0 1 2 4 8
Why is it interesting? What does 11112 “really mean?” 1×23 + 1×22 + 1×21 + 1×20 == 23 + 22 + 21 + 20 == 8 + 4 + 2 + 1 == 1 + 2 + 4 + 8
Binary numbers How much is 100002 ? How does a=100002 relate to b=11112? Consider the relationship between the binary number formed by n 1’s to the binary number formed by one 1 followed by n 0’s Describe a & b, mathematically
Proof S(n)
Basis S(0)
Inductive Step S(n+1)
B.T.I.H S(n+1)
So S(n+1)
Visual Proof n 3 2 1 0 1 (n+1)/2 .. . n .
Visual Proof n+1 n 3 2 1 0 1 (n+1)/2 .. . n .
Visual Proof n/2 n+1 n 3 2 1 0 1 (n+1)/2 .. . n .
Visual Proof n+1 n 3 2 1 0 1 (n+1)/2 .. . n .
Visual Proof n+1 n 3 2 1 0 1 (n+1)/2 .. . n .
Visual Proof n+1 n 3 2 1 0 1 (n+1)/2 .. . n .