Transcript X10c
Recursive Definitions
Rosen, 3.4
Recursive
(or inductive)
Definitions
• Sometimes easier to define an object in terms of itself.
• This process is called recursion.
– Sequences • {s 0 ,s 1 ,s 2 , …} defined by s 0 constants a and b and n = a and s n Z+ – Sets • 3 S and x+y S if x S and y S = 2s n-1 + b for – Functions • Example: f(n) = 2 n , f(n) = 2f(n-1) and f(0) = 1
Recursively Defined Functions
To define a function with the set of nonnegative integers as its domain 1. Specify the value of the function at zero (or sometimes, it first k terms).
2. Give a rule for finding its value at an integer from its values at smaller integers.
Examples of Recursively Defined Functions
• Factorial Function n!
n! = n(n-1)(n-2)….(1) f(0) = 1, f(n) = n(f(n-1)) • a n f(0) = 1, f(n+1) = f(n)*a • Fibonacci Numbers f 0 =0, f 1 =1, f n+1 = f n {0,1,1,2,3,5,8,13,...} + f n-1
Prove that the nth term in the Fibonacci sequence is 1 n 2 i 0 f i when n 2 Induction Proof: Basic Step: Let n = 2, then f 2 true for 2 n n = k+1, i.e., that f k+1 (1 = 1 = i 0 f i ) 1 i 2 2 0 f i =1+0 Inductive Step: Consider k 2 and assume that the expression is k. We must show that the expression is true for k 1 f k+1 = f k +f k-1 (1 by definition k 2 i 0 f i ) f k 1 by the inductive hypothesis
Fibonacci Proof
(cont.) (1 k 2 i 0 f i ) f k 1 (1 k 1 i 0 f i ) Since f 2 is true and [f true, then f n n is true for 2 n k f k+1 ] is is true for all positive integers n 2.
Recursively Defined Set Example
Let the set S be defined recursively as 3 and x+y S if x S and y S.
S Prove that S is equal to the set of positive integers divisible by 3 (set A).
Proof:
To show that S = A, we must show that A S and that S A.
Recursive Set Example
(cont.) First we will show that A S. To do this we must show that every positive integer divisible by 3 is in S. This is the same as saying that 3n is in S for n Z+. We will do an inductive proof.
Let p(n) be the statement that 3n S for n a positive integer.
Basis Step: p(1) = 3(1) = 3 Inductive Step: We will show that p(n) p(n+1) Let p(n) S. Then p(n+1) = 3(n+1) = 3n + 3. Since 3n S and 3 S, then 3n+3 S.
Therefore every element in A must be in S.
Recursive Set Example
(cont.) Now we will show that S A. To do this we must show that every element in S is divisible by 3, i.e., that it can be represented as 3(j) where j is a positive integer.
Basis Step: 3 A since 3 = 3(1) Inductive Step: Assume x,y are in A (where x,y S). x and y are divisible by 3 since they are both in A. Therefore x = 3j and y = 3k for j,k Z+. Then z = x+y = 3j+3k = 3(j+k), which is divisible by 3, so z A (and by definition, z S).
Therefore every element in S must be in A
Prove f
n
>
n-2
where
whenever n
3 = (1+
5)/2
Inductive Proof
Basis Step: First we will show f n > n-2 for f 3 and f 4 .
3-2 = 1 = (1+ 5)/2 < (1+ 9)/2 = 4/2 = 2 = f 3 4-2 = 2 = [(1+ 5)/2] 2 = (1+ 2 5 + 5)/4 = (6+ 2 5)/4 = 3/2 + 5/2 < 3/2 + 9/2 = 3 = f 4
f
n
>
n-2 (cont.) Inductive Step: Assume that f k > k-2 is true for all nonnegative integers 3 k We must show that f n+1 > n-1 .
n when n>4 . Lemma: 2 = [(1+ 5)/2] 2 = (1+2 5 +5)/4 = 3/2 + ( 5)/2 = 1 + (1+ 5)/2 = 1+
f
n
>
n-2 (cont.) n-1 = 2 n-3 = ( +1)( n-3 ) = n-2 + n-3 By our inductive hypothesis f f n-1 n+1 > = f n n-3 + f and f n n-1 > n-2 > n-2 + n-3 = n-1 Since f 3 and f 3 true, then f n are true and f k is true for 3 k is true for all positive integers n 3 n f n+1 is
Find a closed form solution to the recursive equation: T(n) = T(n-1) + c 1 . T(0) = c 0
One approach is to write down several terms, guess what the equation is, and then prove that your guess is correct (or not) using an induction proof.
T(0) = c 0 T(1) = T(0) + c 1 = c 0 T(2) = T(1) + c 1 = c 0 + c 1 + 2c 1 T(3) = T(2) + c 1 = c 0 T(4) = T(3) + c 1 = c 0 + 3c 1 + 4c 1
Guess that T(n) = c 0 + nc 1
Induction proof
Basis Step: for n = 0, T(0) = c 0 + (0)c 1 = c 0 Inductive Step: Assume that T(n) = c 0 we must show that T(n+1) = c 0 + nc + (n+1)c 1 1 .
, T(n+1) = T(n) + c 1 (n+1)c 1 .
= c 0 + nc 1 + c 1 = c 0 +
Find a closed form solution to T(1) = c
0
, T(n) = 2T(n-1)+c
1 T(1) = c 0 T(2) = 2T(1) + c 1 = 2c 0 + c 1 T(3) = 2T(2)+c 1 = 2(2c 0 +c 1 )+c 1 = 4c 0 +3c 1 T(4) = 2T(3)+c 1 = 2(4c 0 +3c 1 )+c 1 = 8c 0 +7c 1 T(5) = 2T(4)+c 1 = 2(8c 0 +7c 1 )+c 1 = 16c 0 +15c 1
Guess that T(n) = 2 n-1 c 0 + (2 n-1 -1)c 1
Prove that T(1) = c 0 , T(n) = 2T(n-1)+c 1 has closed form solution T(n) = 2 n-1 c 0 + (2 n-1 -1)c 1 Basis Step: T(1) = 2 1-1 c 0 + (2 1-1 -1)c 1 = c 0 Induction Step: Assume that T(n) = 2 n-1 c 0 (2 n-1 -1)c 1 . We must show that T(n+1) = + 2 (n+1)-1 c 0 + (2 (n+1)-1 -1)c 1 = 2 n c 0 + (2 n -1)c 1 .
T(n+1) = 2T(n) + c 1 c 1 = 2 n c 0 2 n c 0 + (2 n + (2 -1)c n 1 .
-2)c 1 = 2[2 n-1 c 0 + c 1 + (2 n-1 -1)c 1 ]+ = 2 n c 0 + 2 n c 1 - c 1 =