Chapter 3: Recursion, Recurrence Relations, and Analysis of Algorithms
Chapter 3: Recursion, Recurrence Relations, and Analysis of Algorithms
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
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.
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
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
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
• 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