Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

DATA STRUCTURE

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

• Select a specific (typical) sample of inputs.

• Use physical unit of time (e.g., milliseconds)


or Count actual number of basic operation’s
executions.

• Analyze the empirical data.


Worst-case, Best-case, Average-case
Efficiencies
For some algorithms, efficiency depends on
form of input, so we need to do analysis for:
• Worst case Cworst(n): determine the kind
of inputs for which the count C(n) will be the
largest among all possible inputs of size n.
• Best case Cbest(n): determine the kind of
inputs for which the count C(n) will be the
smallest among all possible inputs of size n.
Average case:
• Cavg(n)
◦ NOT the average of worst and best cases
◦ Expected number of basic operations
considered as a random variable under some
assumption about the probability distribution of
all possible inputs.
Two approaches to writing
repetitive algorithms:
• Iteration
• Recursion

Recursion is a repetitive process in


which an algorithm calls itself.
Usually recursion is organized in such a way
that a subroutine calls itself or a function calls
itself

9
ANALYSIS OF RECURSIVE
ALGORITHMS
• Decide on a parameter indicating an input’s size.

• Identify the algorithm’s basic operation.

• 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.)

• Set up a recurrence relation with an appropriate initial condition


expressing the number of times the basic op. is executed.

• 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
...ni
i
1

11
Factorial: Iterative Algorithm

A repetitive algorithm is defined iteratively whenever the definition involves


only the algorithm parameter (parameters) and not the algorithm itself.

12
Factorial: Recursive Algorithm

A repetitive algorithm uses recursion whenever the algorithm appears


within the definition itself.

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,
bb ifa
=0

GC
Db
,am
od
bo
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.

You might also like