Introduction
There are numerous prove techniques used to prove a statement. However, mathematical induction has wide applicability in computer science.
How it works
Let P(n) be a predicate. We need to prove that for all integers n≥1, P(n) is true. We can accomplish the proof by induction as follows.
- (Induction Base) Prove P(1) is true
- (Induction Step) Prove that ∀n≥1,
P(n)⟹P(n+1)
The anology is similar to that of dominoes. Suppose they are aranged close together such that if you knock out block 1, then it will knock out block2 and so on and on....
Example
use induction to prove that all integers of the type
P(n)=4n−1 are divisible by 3, for all integers n≥1
Proof :
- for the base case, P(1) = 4-1 = 3, which is indeed divisble by three
- now suppose for some n≥1,
P(n)=4n−1 is divisible by three(This is the hypothesis) We will prove that will imply thatP(n+1)=4n+1−1 is also divisible by three(This is the conclusion) - Now to prove the conclusion:
p(n+1)=4n+1−1=4∗4n−1=4∗4n−4+3=4(4n−1)+3 By hpothesis, 4n−1 is divisble 3. So, 4n−1=3k for some integer k. So,P(n+1)=4∗3k+3=3(4k+1) - Therefore, P(n+1) is divisible by three.
Use induction to prove the Arithmetic-Sum formula.
S(n)=1+2+3+...+n=2n(n+1) Proof:
- For the base base where n = 1,
S(1)=21(2)=1 so the base is correct. - Suppose that for some n≥1, the formula is correct:
S(n)=1+2+3+...+n=2n(n+1)Hypothesis we will prove that the formual is also correct for n+1:S(n1)=1+2+3...+n+(n+1)=2(n+1)(n+2) - To prove the conclusion S(n+1)=S(n)+(n+1)
=2n(n+1)+(n+1)By Hypothesis=2n(n+1)+2(n+1)=2(n+1)(n+2)
Prove log algorithm
def log(n:int)->int:
p:int = 0
m:int = n
while(m > 1):
p = p+1
m = m/2
return p
We want to prove the following program computes and returns log2n, assuming that the input parameter n is an integer power of 2.
Let pk and mk denote the resulting values of the variables p and k after iteration k of the while loop, where k=0,1,2,.... Here, k=0 represents the values after the initialization and just before the first execution of the while loop; k=1 represents the values after the first execution of the while loop, and so on.
- First use induction to prove that after the kth iteration of the while loop,
pk=k,mk=2kn(Loop Invariant) (These relations are called loop-invariant.) - Then conclude that the return value of the program is log2n.
Proof: The induction is on the number of iteration k. For the induction base, k=0, the results after the initialization and just before the first iteration is p0=0, m0=20n=n. So the base is correct.
Now suppose after iteration k of the while loop, for some k≥0,
pk=k,mk=2kn Then the next iteration will increment p by 1, and divide m by 2. So the result after iteration k+1 will be:
pk+1=pk+1=k+1,mk+1=2mk=2k+1n This completes the induction proof. So, the Loop Invariant is proved for all k. At the end, when the while loop terminates, m=1. So, p=k, 1=2kn.
This means n=2k=2p. Therefore, p=log2n. And this is the value returned by the program.
Recurrence Equation
Let's consider a recurrence equation
T(n)={2T(⌊2n⌋)+11when n≥2otherwise n=1 (The symbol ⌊⌋ is the floor operation, which rounds down a real number to its nearest integer. For example, ⌊3.28⌋=3.)
The following table shows the result of T(n) for values of n=1,2,3,4,....,16
n | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
---|
T(n) | 1 | 3 | 3 | 7 | 7 | 7 | 7 | 15 | 15 | 15 | 15 | 15 | 15 | 15 | 15 | 31 |
Now let us use induction to prove T(n) has the following lower and upper bounds.
n≤T(n)≤2n−1,n≥1 proof of the upper bound: Prove by induction on n that
T(n)≤2n−1,n≥1 in this case, we only need one base case, n=1
T(1)=1≤2n−1=2⋅1−1=1, so the base is correct
Now, to prove the upper bound for any n≥2, suppose the bound is true for all smaller values of n. That is, T(m)≤2m−1,∀m<n. In particular, for m=⌊2n⌋, the hypothesis is:
T(⌊2n⌋)≤2⌊2n⌋−1 Then,
T(n)=2T(⌊2n⌋)+1 Start with the recurrence equation≤2(2⌊2n⌋−1)+1Use the hypothesis≤2(2⋅2n−1)+1Observe that ⌊2n⌋≤2n≤2(n−1)+1≤2n−1
proof of the lower bound: It is similar to the upper-bound.
Hint: Use the relation ⌊2n⌋≥2n−1