You are on page 1of 23

Chapter 3: Recursion, Recurrence Relations, and

Analysis of Algorithms
Tannaz R.Damavandi
Cal Poly Pomona
Credit for some of the slides in this lecture goes to Dr.Fang Tang
Outline
2
• 3.1– Recursive Definitions
• 3.2- Recurrence Relations
• 3.3- Analysis of Algorithms (Not covered in this class)
3

Section 3.1 – Recursive Definitions


Recursion
• Recursive definitions: define an entity in terms of itself.
• Recursive definition has two parts:
1. A basis, where some simple cases of the item being defined are explicitly given.
2. An inductive or recursive step, where new cases of the item being defined are
given in terms of pervious cases.
• Basis step provides some simple, concrete cases to start with.
• Inductive step allows to construct new cases from simple ones and then to construct still
other cases from new ones.
• Similar to induction proof.
• Also called inductive definition.
• Recursion is important idea that can be used to define sequences of objects, and
4
operations on objects.
Recursive Sequences
•A sequence is an ordered list of objects, which is potentially infinite and repetitions are
allowed. s(k) denotes the kth object in the sequence.
•A sequence is defined recursively by explicitly naming the first value (or the first few
values) in the sequence and then define later values in the sequence in terms of earlier
values.

5
Example
The Sequence S is defined recursively by :
1. S(1) = 2 basis case
2. S(n) = 2 S(n-1) for n >= 2 recursive case
what does the sequence look like?
• By statement 1, S(1), the first object in S, is 2.
• Then by statement 2, the second object in S is S(2)= 2S(1) = 2(2) = 4
• By statement 2 again, S(3) = 2S(2)= 2(4)=8.
• Continuing in this fashion, we can see that S is the sequence

(2,4,8,16,32,….)
6
Practice #1
The Sequence T is defined recursively as follows :
T(1) =1
T(n) = T(n-1) + 3, for n >= 2
Write the first 5 values in the sequence T.

By statement 1, T(1), the first object in T, is 1. Then by statement 2, the second


object in T is T(2)= T(1)+3 = 1+3 = 4
By statement 2 again, T(3) = T(2)+3= 4+3=7.
Continuing in this fashion, we can see that T is the sequence

7
(1,4,7,10,13,16,….)
Fibonacci Sequence
• The Fibonacci Sequence of numbers, introduces in the thirteenth century by an Italian
merchant and mathematician called Fibonacci as follows:
• F(1) = 1, F(2) = 1 basis cases
• F(n) = F(n-2) + F(n-1), for n > 2 recursive case
Hence the first two values of the sequences are given, and the recurrence relation defines the
nth values for n>2 in terms of the two preceding values.
In general, F at any value – except 1 and 2- is the sum of F at the two previous values.
Fibonacci numbers: (1,1,2,3,5,8,13,21,34,55,…)

8
Proofs Related To Recursively Defined Sequence
• Proofs of properties about recursively defined entities are
typically inductive proofs.
• Prove directly from the definition

9
Example
Use Fibonacci definition to prove that in the Fibonacci sequence
F(n+4) = 3F(n+2) –F(n) for all n≥ 1

Based on Fibonacci sequence definition :


F(n+2) =F(n)+F(n+1)
Which can be rewritten as:
F(n+1) =F(n+2)-F(n) (1)
Then
F(n+4) =F(n+3)+F(n+2)
= F(n+2)+F(n+1)+ F(n+2)
= F(n+2)+ F(n+2)-F(n) + F(n+2) based on (1) 10
= 3 F(n+2)-F(n)
Practice #2
Prove the given property of the Fibonacci numbers directly from the definition.

F(n) = 5F(n-4) + 3F(n-5) for all n≥ 6

F(n) = F(n-1) + F(n-2) = F(n-2)+ F(n-3)+ F(n-3)+ F(n-4)


= F(n-4)+ F(n-3)+2 F(n-3)+ F(n-4)
= 2 F(n-4) +3 F(n-3)
= 2 F(n-4)+3[F(n-4) + F(n-5)]
= 5 F(n-4) +3 F(n-5)

11
Example
Use induction proof to prove that in the Fibonacci sequence
F(n+4) = 3F(n+2) –F(n) for all n≥ 1
Since the value of F(n) depends on both F(n-1) and F(n-2), we need to use second principal of induction .
1. For the basis step, we must prove two cases, n=1 and n=2. Why?
n=1 F(5) = 3F(3) –F(1) 5 = 6-1 = 5 true
n=2 F(6) = 3F(4) –F(2) 8 = 9 -1 = 8 true
2. Assume that for all r , 1≤ r ≤ k:
F(r+4) = 3F(r+2) –F(r).
3. Show that :
F(k+1+4) = 3F(k+1+2) –F(k+1) or F(k+5) = 3F(k+3) –F(k+1)
From Fibonacci sequence we have: F(k+5) = F(k+3) + F(k+4)
From inductive hypothesis with r =k we have: F(k+4) = 3F(k+2) –F(k)
From inductive hypothesis with r =k-1 we have: F(k+3) = 3F(k+1) –F(k-1)
F(k+5) = F(k+3) +F(k+4)
= [3F(k+1) –F(k-1)]+[3F(k+2) –F(k)]
= 3[F(k+1) +F(k+2)]-[F(k-1) +F(k)]
= 3F(k+3) – F(k+1) (using the recurrence relation again)
This completes the inductive proof. 12
Recursively Defined Operations
• Certain operations performed on objects can be defined recursively
• Examples:
• A recursive definition of multiplication:
• m*1=m
• m * n = m * (n-1) + m, for n >= 2
• A recursive definition of exponentiation:
• a0 = 1
• an = (an-1)*a, for n > 0
• A recursive definition of factorial operation:
• F(0) = 1 13

• F(n) = n * F(n-1) for n >= 1


Example
Let x be a string over some alphabet. Give a recursive definition for
the operation xn ( concatenation of x with itself n times) for n≥1.

x1 = x
xn = x.xn-1 for n > 1

14
Practice #3
• Give a recursive definition of the following sequences of numbers:
• 1, 3, 9, 27, 81, …
• 1. S(1) =1
• 2. S(n) = 3*S(n-1)

• 2, 1, ½, ¼, 1/8, …
• 1. S(1) =2
• 2. S(n) = S(n-1) /2

• 1,2,4,7,11,16,22,…
• 1. S(1) =1 15
• 2. S(n) = S(n-1) + (n-1)
Recursively Defined Algorithms
• If a recurrence relation exists for an operation, then the algorithm for such a relation can be
written either iteratively or recursively.
• Example: The sequence S is defined recursively by:
1) S(1)=2
2) S(n) = 2S(n-1) for n ≥ 2

Iterative Recursive
Calculate S(n):
if n = 1 then output 2 and return Calculate S(n):
j=2 if n = 1 then
S=2 return 2
while j <= n else
S = S*2 return 2*S(n-1)
j=j+1 endif
end while
output S
16
Recursively Defined Algorithms (Cont’d)
• Iterative:
• Sometimes hard to implement.
• Needs to manage loop(s) computation.
• Normally more lines of code.
• Recursive :
• Complex execution.
• All steps are carried out automatically. (No one need to be aware of what is
happening internally).
• Relatively shorter and less coding.
• Long series use a lot of memory (stack overflow problem).
• Relatively slow.
17
18

Section 3.2 – Recurrence Relations


Solving Recurrence Relations
An equation is said to be a closed-form solution if it solves a given problem in terms of finite
number of functions and mathematical operations from a given generally-accepted set. For
example, an infinite sum would generally not be considered closed-form[1].
• Example
S(1) = 2
S(n) = 2*S(n-1) for n >= 2
Since
S(1) = 2 = 21
S(2) = 4 = 22
S(3) = 8 = 23
and so on. We can see S(n) = 2n
An equation such as S(n) = 2n where we can substitute a value and get the output value back is
called closed-form solution to the recurrence relation. Finding a closed-form solution is called
solving recurrence relation 19
[1].http://mathworld.wolfram.com
Solving Recurrence Relations(Cont’d)
• General method: Expand, Guess and Verify
1. Repeatedly use the recurrence relation until you can guess a pattern.
2. Decide what that pattern will be when n-k=1.
3. Verify resulting formula by induction.
In a first-order recurrence relation , the nth term depends only on the
previous term, but in a second – order recurrence relation the nth term
depends on the two previous terms.

• Another methods: Solution formula and divide and conquer (will not be
covered here) 20
Example
Find a closed-form solution for the recurrence relation, subject to the basis step, for sequence S.
1. S(1) = 2
2. S(n) = 2S(n-1) for n >= 2 (1)
• Expand:
S(n) = 2S(n-1)
= 2[2S(n-2)] = 22S(n-2)
= 22[2S(n-3)] = 23S(n-3)
• Guess: after k such expansions, the equation has the form:
S(n) = 2kS(n-k)
• it stops when n-k = 1, that is when k = n-1, replace k with (n-1)
S(n) = 2n-1S(n-(n-1)) = 2n-1S(1) = 2n
• Verify: proof by induction
S(1)=21 true
Assume S(k)=2k
Show S(k+1)=2k+1
S(k+1) = 2S(k) by (1)
= 2(2k ) by the inductive hypothesis
= 2k+1
21
Practice #4
Find a closed-form solution for the recurrence relation, subject to the basis step, for sequence T.
1. T(1) = 1
2. T(n) = T(n-1)+3 for n >= 2 (1)
• Expand:
T(n) = T(n-1)+3
= [T(n-2)+3]+3 = T(n-2) +2*3
= ([T(n-3)+3]+3)+3 = T(n-3) +3*3
• Guess: after k such expansions, the equation has the form:
T(n) = T(n-k) +3*k

it stops when n-k = 1, that is when k = n-1, replace k with (n-1)


T(n) = T(n-n+1) +3*(n-1)= T(1)+3*(n-1)
T(n) = T(1)+3*(n-1)= 1+3*(n-1)
• Verify: proof by induction
T(1)=1 true
Assume T(k)=1+3*(k-1)
Show T(K+1)= 1+3*(k+1-1) = 1+3*(k)
T(k+1)= T(k)+3 by (1)
= 1+3*(k-1) +3 by the inductive hypothesis
= 1+3*k 22
Practice #5
Find a closed-form solution for the recurrence relation, subject to the basis step, for sequence F.
1. F(1) = 1
2. F(n) = n*F(n-1) for n >= 2 (1)
• Expand:
F(n) = n*F(n-1)
= n*(n-1)*F(n-2)
= n*(n-1)*(n-2)F(n-3)
• Guess: after k such expansions, the equation has the form:
F(k) = n*(n-1)*(n-2)*…*F(n-k)
it stops when n-k = 1, that is when k = n-1, replace k with (n-1)
F(n) =n*(n-1)*(n-2)*…*F(1)= n*(n-1)*(n-2)*…*1
F(n) = n!
• Verify: proof by induction
F(1)=1 true
Assume F(k)= k!
Show F(k+1)= (k+1)!
F(k+1)= (k+1)F(k) by (1)
= (k+1) k! by the inductive hypothesis
= (k+1)! 23

You might also like