Title

advertisement
Chapter Four Notes
Induction and Recursion
Based on: Discrete Math & Its Applications - Kenneth Rosen
CSC125 - Spring 2010
Recursion & Mathematical Induction Compared
Recursive definition – basic structure
1. Base case
2. Recursive case
Recursive function – basic structure
1. Base case
2. Recursive call
Dominant control structure – if-then-else to check for base case
Each recursive call must bring us closer to the base case
Mathematical Induction – basic structure
1. Base case
2. Inductive step
Recursive Definitions and Functions
Recursive definition – Example 1
Example 2, p. 296: Factorial function
Recursive definition of factorial:
fact(n) = 1, if n = 0
n * fact(n), otherwise
Why is this not a circular definition?
Because:
1. n-1 < n, and therefore:
2. We are guaranteed to eventually reach the base case in the definition
Recursive definition – Example 2
Example 5/Definition 1, p. 297: Fibonacci function
Recursive definition of Fibonacci numbers:
fib(n) =
0, if n = 0
1
1, if n = 1
fib(n-1) + fib(n-2), otherwise
Note: There are other ways to define the Fibonacci numbers. This follows the
definition given in Rosen, p. 297.
Recursive definition – Example 3
Recursive definition of Ackermann's function:
A(m,n) = 2n, if m = 0
0,
if m ≥ 1 and n = 0
2,
if m ≥ 1 and n = 1
A(m-1,A(m,n-1))
otherwise
Note: Again, there are a number of definitions of Ackermann's function. This
follows the definition given in Rosen, p. 310.
Recursive Functions
Recursive function – Example 1
Algorithm 1, p. 312: Computing factorial
Recursive factorial function:
def fact(n):
if n == 0:
return 1
else:
return n*fact(n-1)
How do we know that this function will eventually terminate and give us an answer?
Because:
1. n-1 < n, and therefore:
2. With each function call we get closer and closer to the base case.
Recursive function – Example 2
Algorithm 7, p. 316: Recursive Fibonacci algorithm
Recursive Fibonacci function:
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
2
Recursive function – Example 3
Algorithm 4, p. 313: Recursive Euclidean algorithm {gcd}
Recursive gcd function:
def genGcd(a,b):
if b > a:
return gcd(b,a)
else:
return gcd(a,b)
def gcd(a,b):
if a == 0:
return b
else:
return gcd(b % a, a)
Recursive function – Example 4
Recursive Ackermann's function:
def ack(m,n):
if m == 0:
return 2*n
elif n == 0:
return 0
elif n == 1:
return 2
else:
return ack(m-1, ack(m,n-1))
More recursive algorithms - examples
Algorithm 6, p. 314: Recursive binary search
Algorithm 7, p. 316: Recursive Fibonacci algorithm
Algorithm 8, p. 316: Iterative Fibonacci algorithm
Mathematical Induction in detail
Mathematical Induction – proof structure
1. Prove base case
2. Assume true for k & prove that it then follows for k+1
M.I. – proof structure, Rosen's notation
3
P(n) denotes that the proposition is true for n. Thus,
Prove base case:
Prove P(i), for some integer i. Often i = 0.
Inductive step:
Assume: P(k), where k ≥ i
Prove: P(k+1)
In other words, we prove P(k)  P(k+1), for all k ≥ i.
Mathematical Induction – Example 1
Prove: The sum of the first n even nonzero integers is n(n+1)
In other words, prove: ni=1 i = n(n+1)
Prove base case:
1i=1 i = 2*1 = 2 
If n =1, then n(n+1) = 1*(1+1) = 2

Inductive step:
Assume: ki=1 i = k(k+1)
To prove: k+1i=1 i = (k+1)[(k+1) + 1] 
Proof: k+1i=1 i = ki=1 i + 2(k+1) = k(k+1) + 2(k+1)
= (k+1)(k+2) = (k+1)[(k+1) + 1]

Mathematical Induction – Example 2
Example 3, p. 269: Show 20 +21 +22 +23 + . . + 2n = 2n+1 – 1
To Prove: ni=1 2i = 2n+1 – 1
Prove base case:
0i=0 2i = 20 = 1 
Inductive step:
Assume: ki=0 2i = 2k+1 – 1
To prove: k+1i=0 2i = 2(k+1)+1 – 1
Proof: k+1i=0 2i = ki=02i + 2k+1 = 2k+1 – 1 + 2k+1 = 2k+1 + 2k+1 – 1
= 2*2k+1 – 1 = 2(k+1)+1 – 1 
Example 4, p. 270: Prove first formula of Table 2, p. 157:
nj=0 arj = a + ar + ar2 + . . + arn = (arn+1 – a)/(r – 1), when r ≠ 1.
Mathematical Induction – Example 3
4
Example 6, p. 271: Show n! < 2n, for all n > c, where c is some positive integer.
Computing n! and 2n for n = 0, 1, 2, 3 & 4, we find that4! > 24.
To Prove: n! > 2n, for all n > 3.
Prove base case:
4! = 24 > 16 = 24 
Inductive step:
Assume: k! > 2k, for all k > 3.
To prove: (k+1)!> 2k+1.
Proof: (k+1)! = (k+1)*k! > (k+1)* 2k 2k2k+1 
5
Download