KAUST CS204 Fall2022 Lecture8 AnalysisRecursive

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

CS204‐ Data 

structures and 
algorithms

Fall 2022

Lecture 8: Analysis of recursive 
algorithms

Malek Smaoui
What is recursion?
• A recursive algorithm builds the solution to a 
problem P instance of size  , using the 
solution(s) of some problem P instance(s) of 
size(s)  where  . 
– The function/procedure calls itself with a smaller‐size 
input instance and eventually performs additional 
operations before or after the self‐call.
• The algorithm solves the problem for instances of 
size  directly (without self‐call).
– This is an important requirement to avoid infinite 
recursion

Malek Smaoui
Example 0
• Problem: Given a positive integer n, print the numbers from n 
down‐to 0
• Recursion idea: to “print down from n”, we print n then we “print 
down from n‐1”, if we are not yet at 0

CountDown(n)
// prints integers starting from n down-to 0
// Input: a non-negative integer n

print n
if (n > 0)
CountDown(n-1)

Malek Smaoui
Need for recursion
• Recursion is never absolutely necessary
– An equivalent iterative form can be written for any 
recursive implementation of an algorithm
• Most of the time, recursion is used when it 
results from the algorithm design approach
– Decrease and conquer, divide and conquer, 
dynamic programming, etc …
– The iterative version, in this case, is actually 
harder to implement and understand.
Malek Smaoui
General framework for analyzing time 
efficiency of recursive algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation. 
3. Check whether the number of times the basic operation is executed can 
vary on different inputs of the same size; if it can, the worst‐case, 
average‐case, and best‐case efficiencies must be investigated separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the 
number of times the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its 
solution. Many ways to solve the recurrence:
– Backward substitutions or Recurrence tree to identify the solution
– Induction or Master’s theorem to prove it

Source: Introduction to the design and analysis of algorithms. Anany Levitin,


Pearson, 3rd edition, 2012. ISBN 978-0132316811

Malek Smaoui
Example 1: “Factorial”
F(n) More elegantly
//Computes n! recursively
//Input: A nonnegative integer n F(n)
//Output: The value of n! //Computes n! recursively
if n = 0 //Input: A nonnegative integer n
return 1 //Output: The value of n!
else if n = 0
t = F(n-1) return 1
return t ∗ n else
return F(n-1) ∗ n

Malek Smaoui
“Factorial” analysis
1. Input size: (the magnitude of) n 
2. Basic operation: F(n − 1) ∗ n (the multiplication only i.e. t * n)
3. Running time does not depend on the problem instance, so the analysis will 
be for the general case
4. Recurrence relation: 𝑀 𝑛 𝑀 𝑛 1 1
– 𝑀 𝑛 1 to compute F(n − 1)
– 1 to multiply F(n − 1) by n
– 𝑀 0 0 is the initial condition required to solve the recurrence
5. Finding the solution with backward substitutions: 𝑀 𝑛 𝑀 𝑛 1
1 𝑀 𝑛 2 2 𝑀 𝑛 3 3 ⋯ 𝑀 𝑛 𝑖 𝑖 ⋯
𝑀 𝑛 𝑛 𝑛 𝑀 0 𝑛 𝑛
Proving the solution with induction
– Assume/suppose 𝑀 𝑘 𝑘, ∀𝑘 𝑛.
– Prove it for 𝑛: 𝑀 𝑛 𝑀 𝑛 1 1. Since 𝑛 1 𝑛, by the assumption 
𝑀 𝑛 1 𝑛 1. So, 𝑀 𝑛 𝑛 1 1 𝑛
– Verify it for a base case: 𝑀 0 0

Malek Smaoui
Proof by induction steps
is the property/assertion to prove
is the recurrence relation  with some 
initial condition defined for 
1. Induction hypothesis:  is true 
2. Prove induction step using hypothesis 1. and 

3. Verify  for some 
The property is proved 

Malek Smaoui
Solving recurrences
• Solving with different methods will be 
explained with problems invoked in 
subsequent chapters
• For reference: Appendix B of Introduction to
the design and analysis of algorithms. Anany
Levitin, Pearson, 3rd edition, 2012. ISBN 978-
0132316811

Malek Smaoui
Example 2: “Towers of Hanoi”
• Tower of Hanoi  is a simple puzzle invented by French 
mathematician Edouard Lucas in the 1880s
• You have n disks of varying sizes, stacked in a pole 
and ordered from the largest at the bottom to the 
smallest on the top. The goal is to move them from 
one pole to another using one extra intermediate 
pole and respecting:
– No disk can be moved outside a pole. But it can be moved 
from a pole to another. 
– No large disk can be placed on top of a smaller one at 
anytime.

Malek Smaoui
Example 2: “Towers of Hanoi”

Malek Smaoui
Example 2: “Towers of Hanoi”
MoveTower(n, start, finish, temp)
// input: n - the number of disks,
// start - the designation of the pole where the disks are
// originally stacked
// finish – the designation of the destination pole
// temp – the designation of the intermediary/auxiliary pole
// output: printout of disk movements
if (n == 1)
Print: "Move disk”, n, “from”, start, “to”, finish
else {
MoveTower (n-1, start, temp, finish)
Print: "Move disk”, n, “from”, start, “to”, finish
MoveTower(n-1, temp, finish, start)
}
}

Malek Smaoui
“Towers of Hanoi” analysis
• Input size: …………………
• Basic operation: …………………
• Recurrence relation: …………………
• Solving recurrence using backward 
substitution: …………………

Malek Smaoui
“Towers of Hanoi” analysis
• Input size: number of disks 𝑛
• Basic operation: print one disk movement
• Running time does not depend on the problem instance
• Recurrence relation: 
o ∀𝑛 1, 𝑃 𝑛 𝑃 𝑛 1 1 𝑃 𝑛 1
2𝑃 𝑛 1 1
o 𝑃 1 1
• Solving recurrence using backward substitution: 
o 𝑃 𝑛 2𝑃 𝑛 1 1 2 2𝑃 𝑛 2 1 1
2 𝑃 𝑛 2 2 1 2 𝑃 𝑛 3 2 2 1

2𝑃 𝑛 𝑖 2 , for i n

o For 𝑖 𝑛 1, 𝑃 𝑛 2 𝑃 𝑛 𝑛 1 ∑ 2
2 𝑃 1 2 1 2 1
Malek Smaoui
“Towers of Hanoi” analysis
• Recurrence tree or tree of recursive calls:

• The use of trees is more relevant when more than a single 
recursive call is made
• At each node of the tree, only one basic operation is 
performed, so the count of basic operations is the number 
of nodes in the tree (multiplied by 1):
 𝑃 𝑛 ∑ 2 : at each depth 𝑘 0. . 𝑛 1, there are 2
nodes. As a result, 𝑃 𝑛 2 1 Θ 2
Malek Smaoui
“Towers of Hanoi” analysis
• Proof by induction:
– Assume  in particular 

– We have 


We can then conclude that

Malek Smaoui
“Towers of Hanoi” analysis
• The algorithm is recursive yet running time is 
exponential
– Recursive does not mean logarithmic: factorial is 
linear, towers of Hanoi is exponential
• The algorithm is not inefficient: it’s the most 
efficient algorithm to solve this problem
– The problem is inherently difficult

Malek Smaoui
Example 3: “Recursive Binary”
Binary(n) Can you come up 
//Input: A positive decimal integer n with a recursive 
//Output: The number of binary digits solution to the 
in n’s problem?
//binary representation

count ←1
while n > 1 do
count ←count + 1
n← n/2
return count

Malek Smaoui
Example 3: “Recursive Binary”
RinRec(n)
// input: a positive decimal integer n
// output: the number of binary digits in n’s
// binary representation

if n == 1 return 1
else return BinRec( )+1

Malek Smaoui
Example 3: “Recursive Binary”
• Input size: magnitude of 𝑛
• Basic operation: sum BinRec( 𝑛/2 )+1
• Recurrence relation: 
 ∀𝑛 1, 𝐴 𝑛 𝐴 1
 𝐴 1 0
• Solving recurrence using backward substitution:
– Tricky if 𝑛 is not a power of 2 since sometimes  𝑛/2 𝑛/2 and other 
times  𝑛/2
– Use backward substitution with some specific problem sizes: 𝑛 2
to find a closed form 
 this  closed form is not necessarily valid for an arbitrary problem size 
𝑛 but can indicate the rate of growth of the general solution
 Prove (eventually, using induction) that the same rate of growth 
applies for the general case 

Malek Smaoui
Example 3: “Recursive Binary”
• Backward substitution for  :

• We can now guess  (for 
arbitrary  )
– Needs proving though
Malek Smaoui
Example 3: “Recursive Binary”
Proving  is proving that 
and  such that 
a‐
equivalent to 
b‐
equivalent to 

Malek Smaoui
Example 3: “Recursive Binary”
Let’s prove b‐ by induction:
*First, we assume ∀ 1 𝑘 𝑛, 𝐴 𝑘 𝑐 log 𝑘 for some  𝑐 0 (*)
Based on the algorithm, we have 𝐴 𝑛 𝐴 1

Since  𝑛, we can apply our assumption (*) on 𝐴

i.e. 𝐴 𝑐 log

Consequently, 𝐴 𝑛 𝑐 log 1

Since  and log is a monotonically increasing function

So, 𝐴 𝑛 𝑐 log 1 𝑐 log 1


Thus, 𝐴 𝑛 𝑐 log 𝑛 𝑐 log 2 1 𝑐 log 𝑛 𝑐 1
If we pick 𝑐 1, then we get 𝐴 𝑛 𝑐 log 𝑛
**𝐴 1 0 and 𝑐 log 1 0 for any 𝑐 0 i.e. 𝐴 1 𝑐 log 1 for any 𝑐 1
*and ** allow us to conclude that ∀𝑛 1, 𝐴 𝑛 𝑐 log 𝑛, with 𝑐 1 (e.g. 𝑐 2
i.e. 𝐴 𝑛 𝑂 log 𝑛
Example 3: “Recursive Binary”
Let’s prove a‐ by induction:
*First, we assume ∀ 1 𝑘 𝑛, 𝐴 𝑘 𝑐 log 𝑘 for some  𝑐 0 (*)
Based on the algorithm, we have 𝐴 𝑛 𝐴 1
Since  𝑛, we can apply our assumption (*) on 𝐴
i.e. 𝐴 𝑐 log
Consequently 𝐴 𝑛 𝑐 log 1 𝑐 log 1; since 
So, 𝐴 𝑛 𝑐 log 𝑛 1 𝑐 1
But, we want 𝐴 𝑛 𝑐 log 𝑛
Thus, we need to find 𝑐 (and 𝑛 ) such that 𝑐 log 𝑛 1 𝑐 1 𝑐 log 𝑛
Solving gives: 
𝑐 ……
𝑛 …..
Base case verification: 

Malek Smaoui
Example 3: “Recursive Binary”
Other approach: If we prove that ∃𝑐 0 such that 𝐴 𝑛 𝑐 log 𝑛 1 ($)
A consequence would be 𝐴 𝑛 𝑐 log 𝑛
So, let’s ($) prove by induction: 
*We assume that ∀ 1 𝑘 𝑛, 𝐴 𝑘 𝑐 log 𝑘 1 for some  𝑐 0
In particular, 𝐴 𝑐 log 1
Thus, 𝐴 𝑛 𝑐 log 1 1, ∀𝑛 1, by the recurrence relation.
since  then  1 1 1 i.e.  1
So, we can write 𝐴 𝑛 𝑐 log 1 𝑐 log 𝑛 1 𝑐 1
If we pick 𝑐 1 then we have 𝐴 𝑛 𝑐 log 𝑛 1
**𝐴 1 0 and 𝑐 log 1 1 𝑐 but 𝑐 0
so we don’t have 𝐴 1 𝑐 log 1 1 thus we need to verify the property for another 
base case
𝐴 2 𝐴 1 1 1 and 𝑐 log 2 1 𝑐 log 3
so if we pick  𝑐 1 we can verify that 𝐴 2 𝑐 log 2 1
and thus conclude based on * and ** that 𝐴 𝑛 𝑐 log 𝑛 1 , ∀𝑛 2
As a consequence 𝐴 𝑛 𝑐 log 𝑛 , ∀𝑛 2 i.e. 𝐴 𝑛 Ω log 𝑛
Malek Smaoui
Other example
• Using backward substitutions, establish an exact 
formula of  for a properly chosen subset of 
natural numbers,

where 

• From the formula you found, suggest a tight 
bound on  , for arbitrary  .
• Prove this tight bound using a proof by induction.

Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Malek Smaoui
Important summation formulas

Source: Introduction to the design and analysis of algorithms. Anany Levitin, Pearson, 3rd
edition, 2012. ISBN 978-0132316811
Malek Smaoui
Sum manipulation rules

Source: Introduction to the design and analysis of algorithms. Anany Levitin, Pearson, 3rd
edition, 2012. ISBN 978-0132316811

Malek Smaoui

You might also like