Session4&5 Analysis of Recursive Algorithm
Session4&5 Analysis of Recursive Algorithm
Session 4 and 5:
Analysing the algorithm:
Recursive algorithm and non-
recursive algorithm
What is the algorithm’s efficiency
• The algorithm’s efficiency is a function of the number
of elements to be processed. The general format is
f(n
) e
ffic
ien
cy
where n is the number of elements to be processed.
Analysis of Algorithms
• Issues:
- correctness
- time efficiency
- space efficiency
- optimality
• Approaches:
- theoretical analysis
- empirical analysis
Theoretical Analysis of Time Efficiency
• Time efficiency is analyzed by determining the
number of repetitions of the basic operation as
a function T(n) of input size n
• Basic operation: The operation that contributes
most towards the running time of the algorithm
T(n) ≈ cop C(n)
Number of times
running time execution time for
basic operation
basic operation
is executed
Input Size and Basic Operation
Examples
Problem Input size Basic operation
measure
Searching for Number of list’s Key comparison
key in a list of n items, i.e. n
items
Multiplication of Matrix Multiplication of
two matrices dimensions or two numbers
total number of
elements
Empirical Analysis of Time Efficiency
9
ANALYSIS OF RECURSIVE
ALGORITHMS
• Decide on a parameter indicating an input’s size.
• Check whether the number of times the basic op. is executed may
vary on different inputs of the same size. (If it may, the worst,
average, and best cases must be investigated separately.)
• Solve the recurrence (or, at the very least, establish its solution’s
order of growth) by backward substitutions or another method.
Factorial – a case study
• The factorial of a positive number is the product
of the integral values from 1 to the number:
n
!
n 2
1 3
...ni
i
1
11
Factorial: Iterative Algorithm
12
Factorial: Recursive Algorithm
13
Recursion: basic point
• The recursive solution for a problem involves
a two-way journey:
• First we decompose the problem from the top
to the bottom
• Then we solve the problem from the bottom to
the top.
14
Factorial (3):
Decomposition and solution
15
16
17
Designing recursive algorithms
• Each call of a recursive algorithm either
solves one part of the problem or it
reduces the size of the problem.
• The general part of the solution is the
recursive call. At each recursive call, the
size of the problem is reduced.
18
Designing recursive algorithms
• The statement that “solves” the problem is
known as the base case.
• Every recursive algorithm must have a
base case.
• The rest of the algorithm is known as the
general case. The general case contains
the logic needed to reduce the size of the
problem.
19
Designing recursive algorithms
• Once the base case has been reached, the
solution begins.
• We now know one part of the answer and can
return that part to the next, more general
statement.
• This allows us to solve the next general case.
• As we solve each general case in turn, we are
able to solve the next-higher general case until
we finally solve the most general case, the
original problem.
20
Designing recursive algorithms
• The rules for designing a recursive
algorithm:
1. First, determine the base case.
2. Then determine the general case.
3. Combine the base case and the general cases into an
algorithm
21
Example-Problem
• Determine the greatest common divisor (GCD)
for two numbers.
• Euclidean algorithm: GCD (a,b) can be
recursively found from the formula
a ifb
=0
G
C
D
a,
bb ifa
=0
GC
Db
,am
od
bo
t
her
wis
e
22
Pseudocode Implementation
23
C implementation
24
25
26
Example-Problem
• Generation of the Fibonacci numbers series.
• Each next number is equal to the sum of the previous two
numbers.
• A classical Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, …
• The series of n numbers can be generated using a
recursive formula
0 i
fn
=0
F
i
bo
na
c
c
i
n 1 i
fn
=1
Fi
bo
na
c
c
i
n
1Fi
bo
na
c
c
i
n
2o
th
er
wi
s
e
27
28
(Continued)
29
30
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Towers of Hanoi Puzzle
Recursive Algorithm
Analysis of non-recursive algorithms
• Decide on parameter n indicating input size
• Identify algorithm’s basic operation
• Determine worst, average, and best cases for
input of size n
• Set up a sum for the number of times the basic
operation is executed
• Simplify the sum using standard formulas and
rules.
Example: Sequential Search
Example: Sequential Search
• Worst case Cworst(n) = n The last element is
equal to a search key or unsuccessful
• Best case Cbest(n) = 1 The first element is
equal to a search key.
• Average case - We have the following
assumptions:
a. probability of successful search is p (0 ≤
p ≤ 1)
b. probability of first match occurring in ith
position is the same for every i.