comp106_5_induction_and_recursion
comp106_5_induction_and_recursion
comp106_5_induction_and_recursion
Is it n2?
Proof by induction:
1. Basis step P(1) is shown to be true
2. Inductive step P(n) → P(n+1) is shown to be true nZ + Assume P(n) is true,
show P(n+1) is also true
Inductive step:
Remark: Here, "P(n) is true for a fixed arbitrary n” is our inductive hypothesis.
e.g.
Prove n < 2n nZ +
Basis step:
Inductive step:
Harmonic number:
Hk = 1 + 1/2 + 1/3 + ∙∙∙ +1/k, k =1, 2, 3…
Basis step:
P(0) is true, since H20 = H1 = 1 1 + 0/2 = 1.
Inductive step:
Assume P(n) is true H2n 1 + n/2
Basis step:
Inductive step:
2. Inductive step:
Show that [P(1) P(2) … P(n)] → P(n+1) is true nZ + .
e.g. Show that if n > 1 integer, then n is either prime or can be written as a product of primes.
P(n): n is either prime or can be written as product of primes.
Basis step: P(2) is true since 2 is prime itself. This means the
Inductive step: Assume P(k) is true for all 2 k n, for a fixed arbitrary n 2. statement is true for
all integers up to n.
Show P(n+1) is also true.
There are two cases to consider: (hence, use proof by cases)
ii) If (n+1) is composite then n+1 = a·b s.t. 1< a b < n+1.
We know that P(a) and P(b) are true by inductive hypothesis since a b n.
Each of a and b is either prime or can be written as product of primes.
a∙b can also be written as product of primes.
P(n+1) is also true.
generates a sequence
e.g. of numbers a0 a1 a2 a3 a4
an = 2n n = 0, 1, 2,… 1, 2, 4, 8, 16,…
e.g.
F(n) = n! can be defined recursively:
F(0) = 1
F(n+1) = F(n)(n+1) n = 0, 1, 2, 3,….
e.g.
Fibonacci numbers: fn1 1 5
f0 = 0, f1 = 1 lim Golden Ratio
n fn 2
fn +1 = fn + fn-1 n = 1, 2, 3, …
(so that we can generalize the statement to all n by strong induction starting from n = 3 and n = 4)
5.4 Recursive Algorithms
Definition:
An algorithm is called recursive if it solves a problem by reducing it to an instance of
the same problem with smaller input.
Initially, i = 1 and j = n.
e.g. Compute n!, nN
0! = 1, (n+1)! = (n+1)n!
Recursive:
Iterative:
e.g. Computing factorial Let T(n) be the time complexity of the recursive
solution (in terms of comparison and arithmetic
Recursive: operations).
Iterative solutions are usually faster than recursive solutions, though in this example
both algorithms are of O(n) complexity.
e.g. Computing Fibonacci numbers
Recursive:
procedure fibonacci(n: nonnegative integer)
if n = 0 then return 0
else if n = 1 then return 1 ?
else return fibonacci(n − 1) + fibonacci(n − 2)
{output is fibonacci(n)}
Iterative:
procedure iterative fibonacci(n: nonnegative integer)
if n = 0 then return 0
else
x := 0
y := 1
for i := 1 to n − 1 O(n) complexity
z := x + y
x := y
y := z
return y
{output is the nth Fibonacci number}
Complexity analysis for recursive algorithms:
Recursive solution for computing Fibonacci numbers seems more clever and compact;
however the iterative solution is much more efficient!!!
Let T(n) be the time complexity of the recursive solution (in terms of comparison
and arithmetic operations).
Then T(n) = T(n1) + T(n2) + 5 for n ≥ 2 with T(0) = 1 and T(1) = 2.
(The term 5 counts for 2 comparisons plus 1 addition plus 2 subtractions).
Let T(n) be the time complexity of the recursive solution (in terms of comparison
and arithmetic operations).
Then T(n) = T(n1) + T(n2) + 5 for n ≥ 2 with T(0) = 1 and T(1) = 2.
(The term 5 counts for 2 comparisons plus 1 addition plus 2 subtractions).
Since ƒn = ƒn-1 + ƒn-2 with ƒ0 = 0 and ƒ1 = 1, n T(n) ƒn
Also ƒn > αn-2 n ≥ 3, α = (1+ 5)/2 (see previous slides)
T(n) > αn-2 n ≥ 3
Exponential complexity (αn) or worse.
(Or you can simply use big-Omega notation: T(n) is (αn).)
We have to show that the recursive code works correctly for all non-negative n, i.e.,
P(n) is true n, where P(n): “The output of the algorithm is n!”
Basis step:
P(0): “ The output is 1” (True) 0! = 1 by definition of factorial
Inductive step:
Show P(n) → P(n+1) n ≥ 0, or equivalently show P(n1) → P(n) n ≥1.
We assume factorial(n1) correctly returns (n1)! and show factorial(n) returns (n!).
For n, the output is, as seen from the code, n ⋅ factorial(n − 1) = n·(n1)! by inductive hypothesis
= n! P(n) is T
By induction P(n) is true n ≥0
e.g. Show that the following recursive function correctly computes the n-th Fibonacci
number (Exercise: use strong induction).
Let P(n): The function GCD(a, n) correctly computes gcd(a, n) whenever a < n, n > 0.
Basis step: P(1) is true because the only possible case for a is a = 0 (note a < n). Looking
into the code, we see that the algorithm returns 1 in this case, as expected.
Inductive step: Assume P(k) for all 1≤ k ≤ n and check whether GCD(a, n+1) returns
gcd(a, n+1) for any a < n+1.
We see from the code that if a = 0, the function returns n+1, which is correct.
Else, the function calls itself with input ((n+1) mod a, a), and this call generates gcd((n+1)
mod a, a) correctly because P(a) is true by inductive hypothesis for a < n+1.
Since gcd(a, n+1) = gcd((n+1) mod a, a) from what we’ve learned in number theory,
GCD(a, n+1) returns gcd(a, n+1).
Hence, by strong induction, we conclude that P(n) is true for all n; that is, GCD(a, n)
returns gcd(a, n) whenever a < n, n>0.
8.1 & 8.2 Recurrence Relations
Definition:
A recurrence relation for the sequence {an} is an equation that expresses an in terms of one or more
previous terms of the sequence a0, a1, ..., an-1. A sequence is called a solution of a recurrence
relation if its terms satisfy the recurrence relation.
The recurrence relation together with the initial conditions uniquely determine a sequence, i.e., a
solution.
fn = fn-1 + fn-2 , n ≥ 2, n Z
where f0 = 0 and f1 = 1.
e.g., Suppose you deposit $10000 to a savings account at a bank yielding 11% interest per year,
compounded annually. How much money will you get back after 30 years?
an = 1.11 an-1
a0 = 10000
If we iterate:
a1 = 1.11 a0
a2 = 1.11 a1 = (1.11)2 a0
…
an = (1.11)n a0
a0 = 1 pair
a1 = 1 pair
a2 = 2 pairs (1 adult, 1 baby)
a3 = 3 pairs (1 adult, 2 baby)
a4 = 5 pairs (2 adult, 3 baby) WHY??
an = an-1 + an-2 Fibonacci numbers
Rabbits from last month continue to live, and rabbits from 2 months ago breed and bring that
many new rabbits.
e.g., Towers of Hanoi
See example 2 in Section 8.1.2 of your book.
This is a good example of a very common and important problem type.
Your computers won’t even be able to solve it for n = 64.
8.5 Inclusion-Exclusion
e.g., How many positive integers not exceeding 1000 are divisible by 7 OR 11 ?
Divisible by 7: |A| = 1000/7 = 142
Divisible by 11: |B| = 1000/11 = 90
Divisible by 7 AND 11: |A B| = 1000/(7*11) = 12
Divisible by 7 OR 11: |A B| = 142 + 90 – 12 = 220
Remarks: