0% found this document useful (0 votes)
17 views110 pages

Module 1 - 2021 Scheme

The document provides an introduction to algorithms including: - Defining an algorithm as a set of instructions that accomplish a task with defined criteria. - Specifying algorithms through pseudo code, flowcharts, or English statements. - Analyzing algorithms through asymptotic notations like Big-Oh for time and space complexity analysis. - Examples of non-recursive and recursive algorithms and how to mathematically analyze them.

Uploaded by

Mahesh R S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views110 pages

Module 1 - 2021 Scheme

The document provides an introduction to algorithms including: - Defining an algorithm as a set of instructions that accomplish a task with defined criteria. - Specifying algorithms through pseudo code, flowcharts, or English statements. - Analyzing algorithms through asymptotic notations like Big-Oh for time and space complexity analysis. - Examples of non-recursive and recursive algorithms and how to mathematically analyze them.

Uploaded by

Mahesh R S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 110

INTRODUCTION

CONTENTS
 What is an Algorithm?
 Algorithm Specification
 Analysis Framework
 Performance Analysis
 Space complexity
 Time complexity
 Asymptotic Notations
 Big-Oh notation (O)
 Omega notation (Ω)
 Theta notation (θ)
 Mathematical analysis of Non-Recursive and recursive Algorithms with
Examples
 Brute force design technique
 Selection sort
 Sequential search

03/18/2024
String matching algorithm with complexity Analysis
K. Balakrishnan, 18CS42 2
WHAT IS AN ALGORITHM?
“An Algorithm is a finite set of instructions that, if followed,
accomplishes a particular task. In addition, all algorithms must satisfy
the following criteria,
1. Input : Zero or more quantities are externally
supplied.
2. Output : At least one quantity is produced.
3. Definiteness : Each instruction is clear and
unambiguous.
4. Finiteness : The algorithm must terminate
after a finite number of steps for all
cases.
5. Effectiveness : Every instruction must be very
basic so that it can be carried out in principle by a person
using only
03/18/2024 pencil18CS42
K. Balakrishnan, and paper. ” 3
ALGORITHM SPECIFICATION
Pseudo code Conventions
 An algorithm can be written as
 A set of English statements
 Flowchart
 Pseudo code

Pseudo code notations


Notation Description
// Comments
{ } Block of code
; Delimiter
int, float, char, boolean Simple data types
<variable> := <expression> Assignment operator
03/18/2024 and, or, not Logical operator
K. Balakrishnan, 18CS42 4
ALGORITHM SPECIFICATION
Notation Description
node = record{
datatype_1 data 1;
datatype_2 data 2;
. Compound data type
.
datatype_n data n;
}
<, =, >, ≤, ≥, ≠ Relational operators
[ ] Array element access operator

 Looping statements
while <condition> do repeat
{ {
<statement 1> <statement 1>
. .
. .
<statement n> <statement n>
03/18/2024 } } until <condition>
K. Balakrishnan, 18CS42 5
ALGORITHM SPECIFICATION
for variable := value1 to value2 Step step_value do
{
<statement 1>
.
.
<statement n>
}

 Conditional statements
if <condition> then <statement >

if <condition> then <statement 1 > else <statement 2 >

case
{
:<condition 1>: <statement 1>
:<condition 2>: <statement 2>
:else: <statement n>
}
03/18/2024 K. Balakrishnan, 18CS42 6
ALGORITHM SPECIFICATION
 read and write keywords are the input and output statements.

 Header of an algorithm is written as


Algorithm algorithm_name(<parameter list>)

 Example Pseudo code


Algorithm Max(A, n)
// A is an array of size n
{
Result := A[1];
for i := 2 to n do
if A[i] > Result then Result := A[i];
return Result;
}

10 20 30 40 50
03/18/2024 K. Balakrishnan, 18CS42 7
ALGORITHM SPECIFICATION
We will now understand how a problem statement gets translated into an
algorithm taking Selection Sort as a case study.

Phase 1 – Identifying Important aspects of the problem


The basic philosophy of Selection Sort is,
“ From those elements that are currently unsorted, find the
smallest and place it next in the sorted list.”
25
14 57
25 57
14
25
39 39
57
42 42
57

SortedSorted
list Sorted
list list Sorted
Unsorted
list Unsorted
List
Unsorted
List List

We can now say that there are 2 important tasks in this problem:
 Finding the smallest element in the unsorted list.
 Bringing this smallest element to its appropriate position in the list
by swapping.
03/18/2024 K. Balakrishnan, 18CS42 8
ALGORITHM SPECIFICATION
Phase 2 – Converting identified tasks into basic pseudo code
 For a list of n elements, the identified tasks must be performed on all n
elements.

 With this, the basic pseudo code for Selection Sort is,
for i :=1 to n do
{
Examine a[i] to a[n] and suppose the smallest Task 1
element is at a[j];
Interchange a[i] and a[j]; Task 2
}
 The above however is not the final pseudo code as it still has some
English statements.

 Phase 3 is where the completeK. transformation


03/18/2024 Balakrishnan, 18CS42 takes place. 9
ALGORITHM SPECIFICATION
Phase 3 – Converting the tasks to proper pseudo code
 Task 1 involves finding the smallest element in the list.

 The pseudo code for this task is,


Result := A[1];
for i := 2 to n do
if A[i] < Result then Result := A[i];
return Result;

 Task 2 involves swapping of two integers. The pseudo code for


this task is,
t := A[i];
A[i] := A[j];
A[j] := t;
03/18/2024 K. Balakrishnan, 18CS42 10
ALGORITHM SPECIFICATION
 With pseudo codes ready for the primary tasks of the problem, we
now design the final pseudo code for Selection Sort as follows.
Algorithm SelectionSort(a,n)
// sort the array a[1:n] in increasing order
{
} for i := 1 to n do
{
} j := i;
} for k := i+1 to n do Task 1
if(a[k] < a[j]) then j:= k;
} t := a[i]; a[i] := a[j]; a[j] := t; Task 2
} }
}

i,i j i,ki j i,ki jj


k, k,k j
1 2 3 4

03/18/2024
20
10 10
30 K. Balakrishnan,
20 2018CS42
30
25 30
25
11
ALGORITHM SPECIFICATION
Recursive algorithms
 An algorithm is said to be Recursive if the same algorithm is invoked in
the body.

 An algorithm that calls itself is a Direct Recursive algorithm.

 An algorithm A is said to be Indirect Recursive if it calls another


algorithm B which in turn calls A.
Algorithm TowersOfHanoi(n, x, y, z){
// move the top n disks from tower x to y
if(n ≥ 1) then
{
TowersOfHanoi(n-1, x, z, y);
write(“move top disk from tower”, x, “to
tower”,y);
TowersOfHanoi(n-1, z, y, x);
03/18/2024 } K. Balakrishnan, 18CS42 12
ANALYSIS FRAMEWORK
 In this section, we will understand various aspects involved in
analyzing the performance of an algorithm.

 An algorithm's performance can be analyzed using two types of


efficiencies:
 Time Efficiency
 Space Efficiency

 Time efficiency indicates how fast an algorithm runs.

 Space efficiency indicates the amount of memory space required by


an algorithm.
03/18/2024 K. Balakrishnan, 18CS42 13
ANALYSIS FRAMEWORK
Measuring an Input’s size
 In most cases, size of the input decides how fast an algorithm runs.

 Almost all algorithms run longer on larger input size.

 For a list of size n, operations like searching, sorting, finding largest


and smallest element will take more time if n is large.

 For evaluation of a polynomial, the quickness of evaluation depends


on the number of terms n in the polynomial.

 For addition or product of matrices, the time taken to complete the


task will depend on the order of the matrices involved.
03/18/2024 K. Balakrishnan, 18CS42 14
ANALYSIS FRAMEWORK
Units for measuring running time
 The universal unit used to measure time is seconds. However, we avoid
this unit to measure running time.

 This is because, the time an algorithm takes to complete varies across


systems.

 As different systems have different configurations, performance time of


algorithms will also vary.

 Hence, we adopt a more reliable approach.

 We count the number of times the Basic Operation of an algorithm is


executed.
03/18/2024 K. Balakrishnan, 18CS42 15
ANALYSIS FRAMEWORK
 Basic operation of an algorithm is the algorithms primary task.

 For example, the basic operation of any sorting algorithm is comparison


and swapping.

 The basic operation of matrix multiplication is addition and


multiplication.

 Based on this, the running time of an algorithm T(n) is,


T(n) ≈ * C(n)

where,
- execution time of the basic operation op
03/18/2024
C(n) - number of times the basic operation executes
K. Balakrishnan, 18CS42 16
ANALYSIS FRAMEWORK
Orders of growth
These are the functions that represent the run time of an algorithm.

The various order of growth functions are,


Logarithmic() Linear(n) N-log-N()
Quadratic() Cubic() Exponential()
Factorial(n!)
03/18/2024 K. Balakrishnan, 18CS42 17
ANALYSIS FRAMEWORK
Worst - case, Best – case, Average – case efficiencies
 So far, we have seen that input size plays an important role in an
algorithms performance.

 However, it is not only the input size that influences the performance of
an algorithm.

 There are other factors as well. We will understand this with the help of
the following example.
Algorithm SequentialSearch(A[0…n-1], k){
i := 0; i i i
while i < n and A[i] ≠ k do 0 1 2
i := i + 1;
10 20 30
if i < n then return i;
else return -1;
}
03/18/2024 K. Balakrishnan, 18CS42 18
ANALYSIS FRAMEWORK
 Linear search is a classical example that shows that input size alone
does not determine the algorithms performance.

 We now define three efficiencies for the algorithm.

Best - Case efficiency


“It is the efficiency for the best case input of size n, which is an
input for which the algorithm runs the fastest.”

 Linear search algorithm runs the fastest when the key to be searched
is the first element in the list.

 Hence,
03/18/2024 K. Balakrishnan, 18CS42 19
ANALYSIS FRAMEWORK
Worst – Case efficiency
“It is the efficiency for the worst case input of size n, which is an
input for which the algorithm runs the longest.”

 Linear search algorithm runs the longest when the key to be searched
is the either the last element in the list or not present in the list.

 Hence,

Average – Case efficiency


 This case takes both the best case and worst case into account.

03/18/2024 K. Balakrishnan, 18CS42 20


ANALYSIS FRAMEWORK
 We now derive an expression for the average case efficiency.

 Let the probability of a successful search be p.

 For an input of size n,


 Probability of successful search at 1st position is p*1/n.
 Probability of successful search at 2nd position is p*2/n.
 Probability of successful search at position is p*i/n.
 Probability of successful search at position is p*n/n.

 Hence the probability of a successful search can be written as,

03/18/2024 K. Balakrishnan, 18CS42 21


ANALYSIS FRAMEWORK
 Also, if p is the probability of a successful search, then the probability
of an unsuccessful search is,
(1–p)

 An unsuccessful search occurs only after all the list elements are
compared and no match is found.

 Hence, the probability of an unsuccessful search for an input of size n


is given by,
n * (1 - p)

 With probability expressions for successful and unsuccessful searches


ready, we derive the average case efficiency of linear search.
03/18/2024 K. Balakrishnan, 18CS42 22
ANALYSIS FRAMEWORK
= prob. of successful search + prob. of unsuccessful
search
= + n(1 - p)

= [ 1 + 2 + .. + i + .. + n] + n(1 - p)

= + n(1 - p)

On solving the above RHS we have,


= + n(1 - p)

03/18/2024 K. Balakrishnan, 18CS42 23


ANALYSIS FRAMEWORK
 Suppose p = 0, which represents an unsuccessful search,
= + n(1 - p)
= + n(1 - 0)
=n
Hence n comparisons take place during an unsuccessful search.

 Suppose p = 1, which represents a successful search,


= + n(1 - 1)
=
This means, on an average n/2 comparisons takes place before a match
is found.

03/18/2024 K. Balakrishnan, 18CS42 24


PERFORMANCE ANALYSIS
 Analyzing the performance of an algorithm is based on two important
parameters: Space and Time.

“The Space Complexity of an algorithm is the amount of


memory it needs to run to completion.”

“The Time Complexity of an algorithm is the amount of


computer time it needs to run to completion”

Space Complexity
To determine the space complexity of an algorithm, the algorithm has to
be viewed in 2 parts,
Fixed part and Variable part
03/18/2024 K. Balakrishnan, 18CS42 25
PERFORMANCE ANALYSIS
 Fixed part corresponds to the section of the algorithm whose
memory requirement is fixed irrespective of the problem instance.

 Parameters and local variables belong to the fixed part.

 Variable part corresponds to the section of the algorithm whose


memory requirement depends on the problem instance.

 Based on this, the space complexity of an algorithm is given by,


S(P) = c +
where,
P – Algorithm, S(P) – Space complexity of P
c – space requirement of constant part
03/18/2024 K. Balakrishnan, 18CS42 26
PERFORMANCE ANALYSIS
Consider the following algorithm.
Algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
}

 To find the space complexity of this algorithm, we first identify the


fixed and variable parts.

 In the algorithm, 1 unit of memory is required for parameters a, b and


c.

 Also, 1 unit of memory is required to store the result of the


mathematical computation.
03/18/2024 K. Balakrishnan, 18CS42 27
PERFORMANCE ANALYSIS
 In total, 4 memory units are needed for the algorithm.

 This space requirement is constant and does not change with the
problem instance.

 This 4 units correspond to the fixed part c.

 Hence, the space complexity of the algorithm is,


S(P) = c +
S(P) = 4 + 0
S(P) = 4

 This algorithm has a contact space complexity.


03/18/2024 K. Balakrishnan, 18CS42 28
PERFORMANCE ANALYSIS
Now consider the following algorithm.
Algorithm Sum(a, n)
{
s := 0;
for i := 1 to n do
s := s + a[i];
return s;
}

 In this algorithm, 1 memory unit is needed for the parameter n and


local variables s and i.

 n, s and i form the fixed part, which means c = 3.

 The algorithm also has an array a[] as a parameter.


03/18/2024 K. Balakrishnan, 18CS42 29
PERFORMANCE ANALYSIS
 The number of memory units cannot be fixed for a[].

 It depends upon the instance of the problem.

 In one instance, the algorithm can be executed for a[] with size 5 and
in another instance the size of a[] can be 100.

 Hence, the memory requirement of a[] is variable and totally depends


on the array size n.

 With this, the space complexity of the given algorithm is,


S(P) = c +
S(P) = 3+ n
03/18/2024 K. Balakrishnan, 18CS42 30
PERFORMANCE ANALYSIS
Time Complexity
 The time taken by a program P to complete, denoted by T(P), is the
sum of its compile time and run (execution) time.

 However, we do not consider compile time to determine time


complexity.

 This is because a program that is complied once can be executed


several times.

 Hence, for determining time complexity, we focus only on the


execution time.

 The execution time of a program


03/18/2024
P is denoted by .
K. Balakrishnan, 18CS42 31
PERFORMANCE ANALYSIS
 for a program with input size n is given by,
=

where
n – problem instance
, , - time needed for addition, subtraction, multiplication
ADD, SUB, MUL – number of additions, subtractions, multiplications

 The summary of the above expression is,


The execution time of a program P is the number of additions,
subtractions, multiplications, divisions, compares, loads, stores and so
on, that would be made by the code for P.

03/18/2024 K. Balakrishnan, 18CS42 32


PERFORMANCE ANALYSIS
 There is a complication involved in the expression.

 For instance, the term corresponds to the product of time taken for
an addition operation and the total number of additions in the
algorithm.

 There can be multiple addition statements in the algorithm and all of


them might not take the same time to execute.

 Hence, it is not logically correct to represent execution time of all


additions in a single term .

 The same issue is applicable for other operations in the algorithm.


03/18/2024 K. Balakrishnan, 18CS42 33
PERFORMANCE ANALYSIS
 Hence, is not effective.

 A more reliable approach would be to count the number Program


Steps.

“A Program Step is loosely defined as a syntactically or


semantically meaningful segment of a program that has an execution
time that is independent of the instance characteristic.”
Components Program steps
// 0
: = , read, write, if 1
for, while, repeat - until Depends on the number of iterations
Program Step for some algorithm
components
03/18/2024 K. Balakrishnan, 18CS42 34
PERFORMANCE ANALYSIS
The count of program steps can be determined in one of the following 2
ways:
 COUNT variable
 Tabulation method

COUNT variable
 This is a global variable with initial value 0.

 Statements to increment count by the appropriate amount are


introduced into the program.

 When a statement in the original program executes, count is


incremented by the step count of that statement.
03/18/2024 K. Balakrishnan, 18CS42 35
PERFORMANCE ANALYSIS
Example 1
Algorithm Sum(a, n)
{
s := 0;
for i :=:=1 count
count to n do + 1; count = 1
for i := 1 tos :=
n sdo{
do+ a[i];
return s; count
s := s +:=a[i];
count + 1; count = n
} return s; s := s + a[i];
} } count := count + 1; count = n count = 2n + 3
}return s;
} return:=s;count + 1;
count count = 1
} return s;
} count := count + 1; count = 1
}

Step 1 – Identify the Program steps.


Step 2 – Introduce the Count variable.
Step 3 – Obtain the value of Count.
03/18/2024 K. Balakrishnan, 18CS42 36
PERFORMANCE ANALYSIS
Example 2
RSum(a,3)
60
Algorithm RSum(a, n)
{
if(n ≤ 0) then
return 0;
else RSum(a,2)
30 + 30 = +6030
return RSum(a, n-1) +
a[n];
}
1 2 3 RSum(a,1)
10 + 20 = +3020
a[3]
10 20 30

n=3 RSum(a,0) + 10
0 + 10 = 10

03/18/2024 K. Balakrishnan, 18CS42 37


PERFORMANCE ANALYSIS
Analysis of Example 2
 Let be the time complexity of RSum() algorithm.

 Introducing the count variable in the algorithm, we get,


Algorithm RSum(a, n)
{
if(n ≤ 0) then
count := return
count +0;1;
else return 0;
else return:=RSum(a,
count count +n-1)
1; +
a[n]; else return RSum(a, n-1) +
}
a[n]; return RSum(a, n-1) +
}
a[n];
} count := count + 1;
}
03/18/2024 K. Balakrishnan, 18CS42 38
PERFORMANCE ANALYSIS
 The given algorithm is a recursive algorithm.

 Time complexity of a recursive algorithm is obtained by constructing a


Recurrence Relation for the algorithm.

 In algorithms, a Recurrence Relation is a mathematical equation that


represents recursion.
Algorithm RSum(a, n)
{
if(n ≤ 0) then
count := count + 1;
return 0;
count := count + 1;
𝒕 𝑹𝑺𝒖𝒎
𝒕 𝑹𝑺𝒖𝒎
𝑹𝑺𝒖𝒎
(𝒏)=
(𝒏
(𝒏)=
)= 𝟐𝒊𝒇𝒊𝒇
( 𝒏−𝟏
𝒕 𝑹𝑺𝒖𝒎𝒊𝒇
𝒊𝒇 { {{
𝟐 𝒊𝒇
𝒏>
𝒏=𝟎
𝒏=𝟎
𝒏=𝟎
)+𝟎
𝒏 >𝟎 𝟐 𝒊𝒇 𝒏>𝟎
else
return RSum(a, n-1) + a[n];
count := count + 1;
}
03/18/2024 K. Balakrishnan, 18CS42 39
PERFORMANCE ANALYSIS
 We now solve the recurrence relation to get the time complexity of
RSum(a,n).

 A recurrence relation is solved using Back Substitution method which is


demonstrated below.

 We have,

.
.
.

03/18/2024 K. Balakrishnan, 18CS42 40


PERFORMANCE ANALYSIS
Example 3
Algorithm Add(a, b, c, m, n)
{
for i := 1 to m do
{ for j := 1 to n do
count := count + 1;c[i, j] := a[i, j] + b[i, count = m
j]; for j := 1 to n do
} {
count := count
c[i, j] +:=1;a[i, j] + b[i, count = mn
j]; c[i, j] := a[i, j] + b[i,
j]; } count = mn
} } count := count + 1;
} } } count = m
} } count
count:=:=count
count++1; 1;
} } count = 1
} count := count + 1;
}
Time complexity = 2mn+2m+1
03/18/2024 K. Balakrishnan, 18CS42 41
PERFORMANCE ANALYSIS
Tabulation method
 In this approach, we build a table in which we list the total number of
steps contributed by each statement.

 For each statement we determine its steps per execution (s/e).

“The s/e of a statement is the amount by which the count changes


as a result of the execution of that statement.”

 The table for this method is presented below.


Statements s/e Frequency Total Steps

03/18/2024 K. Balakrishnan, 18CS42 42


PERFORMANCE ANALYSIS
Example 1
Algorithm Sum(a, n)
{
s := 0;
for i := 1 to n do
s := s + a[i];
return s;
}

Statements s/e Frequency Total Steps


Algorithm Sum(a, n) 0 - 0
{ 0 - 0
s := 0; 1 1 1
for i := 1 to n do 1 (n+1) (n+1)
s := s + a[i]; 1 n n
return s; 1 1 1
} 0 - 0

Time Complexity 2n+3


03/18/2024 K. Balakrishnan, 18CS42 43
PERFORMANCE ANALYSIS
Example 2
Algorithm Add(a, b, c, m, n)
{
for i := 1 to m do
for j := 1 to n do
c[i, j] := a[i, j] + b[i, j];
}

Statements s/e Frequency Total Steps


Algorithm Sum(a, n) 0 - 0
{ 0 - 0
for i := 1 to m do 1 (m+1) (m + 1)
for j := 1 to n do 1 m(n+1) mn + m
c[i,
c[i,j]j]:=
:=a[i,
a[i,j]j]++b[i,
b[i,j];
j]; 1 mn mn
} 0 - 0

Time Complexity 2mn + 2m + 1


03/18/2024 K. Balakrishnan, 18CS42 44
ASYMPTOTIC NOTATIONS
 Several solutions/algorithms exist for a given problem.

 The best performing algorithm is chosen as the problem’s solution.

 To compare the performance of the algorithms and rank them


accordingly special notations are used.

 These notations are called Asymptotic Notations.

 There are 3 Asymptotic notations:


 Big – Oh(O)
 Big – Omega(Ω)
 Big – theta(θ)
03/18/2024 K. Balakrishnan, 18CS42 45
ASYMPTOTIC NOTATIONS
Informal Introduction
Big-Oh(O)
 Consider a function g(n) with some order of growth.

 All functions that have the same or smaller order of growth than
g(n) are said to belong to Big-Oh of g(n) denoted by O(g(n)).

 Let g(n) = .

 Functions that belong to O(g(n)) are log n, n, n log n, .

 This is represented as,


03/18/2024
,, K. Balakrishnan, 18CS42 46
ASYMPTOTIC NOTATIONS
Big-Omega(Ω)
 This is the opposite of O.

 For a function g(n), all functions having the same or greater order of
growth than g(n) are said to belong to Big-Omega of g(n) denoted by
Ω(g(n)).

 For g(n) = , functions that belong to Ω(g(n)) are , , , , n!.

 This is represented as,


,,

03/18/2024 K. Balakrishnan, 18CS42 47


ASYMPTOTIC NOTATIONS
Big-Theta(θ)
 For a function g(n), all functions having the same order of growth as
g(n) are said to belong to Big-Theta of g(n) denoted by θ(g(n)).

 For g(n) = , we can say that all quadratic functions belong to θ(g(n)).

 This is represented as,


,

03/18/2024 K. Balakrishnan, 18CS42 48


ASYMPTOTIC NOTATIONS
Big-Oh(O) notation
“A function t(n) is said to be O(g(n)), denoted by,
t(n) Є O(g(n))
if there exists some positive constant c and some non-negative integer
such that,
t(n) ≤ c * g(n), for all n ≥ ”

The O–notation denotes upper bound


or worst case of a function.

03/18/2024 K. Balakrishnan, 18CS42 49


ASYMPTOTIC NOTATIONS
Example
Prove that 100n + 5 Є O().

Proof
 In the given problem
t(n) = 100n + 5, g(n) =

 According to O definition,
t(n) ≤ c * g(n), for all n ≥

 Applying the definition to our problem we get,


100n + 5 ≤ c * , for all n ≥
03/18/2024 K. Balakrishnan, 18CS42 50
ASYMPTOTIC NOTATIONS
 We now need to find appropriate values for c and to prove the
problem.

 Let us start with c. Consider c = 100. We have,


100n + 5 ≤ 100 * , for all n ≥

 Next we need to find an appropriate value of . For this we use the trial
and error method.

 We start with = 1. We have,


100n + 5 ≤ 100 * , for all n ≥

 Substituting the value of n = 1 in the expression, we have


03/18/2024
100(1)
105 ≤105
100
+≤ 5 -100≤FALSE
100 *
K. Balakrishnan, 18CS42 51
ASYMPTOTIC NOTATIONS
 We next consider = 2. We have,
100n + 5 ≤ 100 * , for all n ≥

 Substituting the value of n = 2 in the expression, we have


100
205*205
≤2 400
+≤5 400
-≤TRUE
100 *
 Since the equation holds good for n = 2, it will hold good for every
value of n higher than 2.

 Hence, the proof for the given problem is,


100n + 5 Є O()
when c = 100 and = 2

 This however is not the onlyK. Balakrishnan,


03/18/2024 solution to the problem.
18CS42 52
ASYMPTOTIC NOTATIONS
Big-Omega(Ω) notation
“A function t(n) is said to be Ω(g(n)), denoted by,
t(n) Є Ω(g(n))
if there exists some positive constant c and some non-negative integer
such that,
t(n) ≥ c * g(n), for all n ≥ .”

The Ω–notation denotes lower bound


or best case of a function.

03/18/2024 K. Balakrishnan, 18CS42 53


ASYMPTOTIC NOTATIONS
Example
Prove that Є Ω().

Proof
 In the given example,
t(n) = , g(n) =

 According to Ω definition,
t(n) ≥ c * g(n), for all n ≥

 Applying the definition to our problem we get,


≥ , for all n ≥
03/18/2024 K. Balakrishnan, 18CS42 54
ASYMPTOTIC NOTATIONS
 We can see that the given relation holds good as it is.

 Hence, we consider base values for c and , i.e.,


c = 1 and = 0

 The resulting relation is,


≥ , for all n ≥

 Substituting the respective values, we get


≥ - TRUE

 Hence,
Є Ω() when c = 1 and = 0
03/18/2024 K. Balakrishnan, 18CS42 55
ASYMPTOTIC NOTATIONS
Big-Theta(θ) notation
“A function t(n) is said to be θ(g(n)), denoted by,
t(n) Є θ(g(n))
if there exists some positive constants and and some non-negative
integer such that,
* g(n) ≤ t(n) ≤ * g(n), for all n ≥ .”

The θ–notation denotes average case


of a function.

03/18/2024 K. Balakrishnan, 18CS42 56


ASYMPTOTIC NOTATIONS
Example
Prove that Є θ().

Proof
 In the given example,
t(n) = , g(n) =
 According to θ definition,
* g(n) ≤ t(n) ≤ * g(n), for all n ≥

 Applying the definition to our problem we get,


* ≤ ≤ * , for all n ≥

03/18/2024 K. Balakrishnan, 18CS42 57


ASYMPTOTIC NOTATIONS
 Unlike the O and Ω, we need to find value of two constants, & .

 Hence, we break the θ expression into two halves and find the value
of & separately.

 Let’s start with . We have,


* ≤ , for all n ≥

 Processing this expression we get,


* ≤ , for all n ≥

03/18/2024 K. Balakrishnan, 18CS42 58


ASYMPTOTIC NOTATIONS
 We now find appropriate values of & .

 Let = 1/4. We have,


≤ , for all n ≥

 We now need to find an appropriate value of . This is done as follows.

Let = 0.
1. We have,
2.
3.
4.
≤≤ ---TRUE
FALSE
TRUE
TRUE

 Hence,
* ≤ , for and all n ≥
03/18/2024 K. Balakrishnan, 18CS42 59
ASYMPTOTIC NOTATIONS
 We now find . We have,
≤ * , for all n ≥

 Processing this expression we get,


≤ * , for all n ≥

 Let = 1/2. We have,


≤ , for all n ≥

 We next start the quest for a proper value.

03/18/2024 K. Balakrishnan, 18CS42 60


ASYMPTOTIC NOTATIONS
Let = 3.
0. We have,
1.
2.
3≤≤≤--TRUE
TRUE

 Hence,
≤ * , for all n ≥

 We also have,
* ≤ , for and all n ≥

 Therefore the solution is,


Є θ()
when, , and

03/18/2024 K. Balakrishnan, 18CS42 61


ASYMPTOTIC NOTATIONS
Useful Property Involving the Asymptotic Notations
Theorem
If and , then,

 This theorem is called the Section Theorem.

Proof
 To prove the theorem, we take help of the following mathematical
property.
if ≤ and ≤ , then, ≤ 2 * max{, } (1)

03/18/2024 K. Balakrishnan, 18CS42 62


ASYMPTOTIC NOTATIONS
 We have which can be expressed as,
(2)

 We also have which can be expressed as,


(3)

 Let us consider another constant such that,

 Out of and we do not know which is the greater constant.

 can be greater than or vice versa.


03/18/2024 K. Balakrishnan, 18CS42 63
ASYMPTOTIC NOTATIONS
 Since both and have equal chances of being greater than the other,
we replace both and with .

 This results in,


(4)
(5)

 Applying property (1) on the above equations we have,

 Bringing the constant outside,


(6)

03/18/2024 K. Balakrishnan, 18CS42 64


ASYMPTOTIC NOTATIONS
 Close observation of (6) results is,
𝒕 𝟏 ( 𝒏) + 𝒕 𝟐 ( 𝒏 ) ≤𝟐 ∗ 𝑪 𝟑 ∗ 𝒎𝒂𝒙 {𝒈 𝟏 ( 𝒏 ) , 𝒈𝟐 ( 𝒏) }

𝒕 (𝒏) 𝒄𝒐𝒏𝒔𝒕𝒂𝒏𝒕 𝒈 ( 𝒏)
 The above expression can be rewritten as,
𝒕 𝟏 ( 𝒏) + 𝒕 𝟐 ( 𝒏 ) ∈ 𝑶( 𝒎𝒂𝒙 { 𝒈 𝟏 ( 𝒏 ) , 𝒈𝟐 ( 𝒏 ) })

Hence proved.

03/18/2024 K. Balakrishnan, 18CS42 65


ASYMPTOTIC NOTATIONS
Practice Examples
Example 1
Use the informal definitions of O, Ω and θ to determine whether the
following assertions are true or false.
1. n(n + 1)/2 ∈ O() 2. n(n + 1)/2 ∈ O()
3. (n + 1)/2 ∈ θ() 4. n(n + 1)/2 ∈ Ω(n)

Solution
1. n(n + 1)/2 ∈ O() – TRUE
2. n(n + 1)/2 ∈ O() – TRUE
3. (n + 1)/2 ∈ θ() – FALSE
4. n(n + 1)/2 ∈ Ω(n) – TRUE
03/18/2024 K. Balakrishnan, 18CS42 66
ASYMPTOTIC NOTATIONS
Example 2
List the following functions according to their order of growth from the
lowest to the highest:
, , , , ,,

Solution
Function Order of Growth function Rank
n! (Factorial) 7
log n (Logarithmic) 1
(Exponential) 6
4
log n (Logarithmic) 2
3
(Exponential) 5
03/18/2024 K. Balakrishnan, 18CS42 67
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 In this section, we take up several non-recursive algorithms and analyze


their performance.

 The first step in analysis of any algorithm is identification of Basic


Operation.

 Once the basic operation is identified, certain computations are


performed to determine the algorithms efficiency.

 Efficiency of an algorithm includes its Best case, Worst case and


Average case efficiencies.

 There are some algorithms that have the same Best case, Worst case
and Average case efficiencies.K. Balakrishnan, 18CS42
03/18/2024 68
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS
 Consider the following example,
Algorithm ADD(a, b)
// Input: Two non-negative integers a & b.
//Output: The value of the sum of a & b.
c := a + b;
return c;

 The basic operation of this algorithm is addition.

 This operation executes only once irrespective of the values of a and


b.

 Hence we can conclude that the efficiency(best, worst and average)


of this algorithm is a constant.
03/18/2024 K. Balakrishnan, 18CS42 69
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

General Plan for Analysing the Time Efficiency of Non-


Recursive Algorithms
STEP 1 - Decide on a parameter (or parameters) indicating an input’s
size.

STEP 2 - Identify the algorithm’s basic operation. (As a rule, it is


located in the innermost loop.)

STEP 3 - Check whether the number of times the basic operation is


executed depends only on the size of an input. If it also depends on
some additional property, the worst, average and best case efficiencies
have to be investigated separately.

03/18/2024 K. Balakrishnan, 18CS42 70


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

STEP 3 - Check whether the number of times the basic operation is


executed depends only on the size of an input. If it also depends on
some additional property, the worst, average and best case efficiencies
have to be investigated separately.

STEP 4 - Set up a sum expressing the number of times the


algorithm’s basic operation is executed.

STEP 5 - Using standard formulas and rules of sum manipulation,


either find a closed form formula for the count or, at the very least,
establish its order of growth.

 We will now analyze the performance of several iterative algorithms using


the above general plan.
03/18/2024 K. Balakrishnan, 18CS42 71
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

Finding the largest of ‘n’ numbers


ALGORITHM MaxElement(A[0..n − 1])
//Determines the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers
//Output: The value of the largest element in A
maxval := A[0];
for i := 1 to n − 1 do
if A[i] > maxval
maxval := A[i];
return maxval;

Analysis
STEP 1 : Input size = n.

STEP 2 : Basic Operation is Comparison


03/18/2024 K. Balakrishnan, 18CS42 72
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

ALGORITHM MaxElement(A[0..n − 1])


//Determines the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers
//Output: The value of the largest element in A
maxval := A[0];
for i := 1 to n − 1 do
if A[i] > maxval
maxval := A[i];
return maxval;

STEP 3 – Basic operation depends only on input size.

STEP 4 – Let C(n) be the number of times the basic operation


executes. We have,

03/18/2024 K. Balakrishnan, 18CS42 73


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

STEP 5 – The summation is solved as follows.


The summation is,

 We have the summation property,

 Applying this to the summation


C(n) = (n – 1) – 1 + 1
C(n) = n – 1
C(n) = θ(n)

03/18/2024 K. Balakrishnan, 18CS42 74


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

Element Uniqueness Problem


ALGORITHM UniqueElements(A[0..n − 1])
//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct and “false”
otherwise
for i := 0 to n − 2 do
for j := i + 1 to n − 1 do
if A[i]= A[j] then return false;
return true;

i ij ij j
0 1 2 3
5 6 7 7

03/18/2024 K. Balakrishnan, 18CS42 75


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

ALGORITHM UniqueElements(A[0..n − 1])


//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct and “false”
otherwise
for i := 0 to n − 2 do
for j := i + 1 to n − 1 do
if A[i]= A[j] then return false;
return true;

Analysis
STEP 1 – Input size is n.

STEP 2 – Basic operation is comparison.

STEP 3 – The Basic operation does K.not


03/18/2024
depend on input size alone.
Balakrishnan, 18CS42 76
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 This algorithm does not execute for the entire input size for all
instances.

 The execution depends on the position of occurrence of same elements.

 For one instance the algorithm may execute for entire input size while
for another instance the algorithm stops after the very first iteration.

 Therefore this algorithm has different best, worst and average case
efficiencies.

 We focus on the worst case efficiency.

03/18/2024 K. Balakrishnan, 18CS42 77


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 Worst case occurs in the algorithm in the following instances:


 No duplicates in the array.
 Duplicate elements are the last two elements.

 In both these instances, the algorithm executes for the entire input
size.

STEP 4 - Based on this, we define the worst case efficiency of the


algorithm, denoted by as,
𝒏−𝟐 𝒏−𝟏
𝑪 𝒘𝒐𝒓𝒔𝒕 (𝒏 ) = ∑ ∑ 𝟏
𝒊= 𝟎 𝒋=𝒊 +𝟏

03/18/2024 K. Balakrishnan, 18CS42 78


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

STEP 5 - The summation is solved as follows.

03/18/2024 K. Balakrishnan, 18CS42 79


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 We have the mathematical property,


= 0+1+2+……+n =

 Applying this property to the expression, we get,

( 𝒏−( 𝒏− 𝟏𝟏 𝟐 𝒏 −𝟐
) [𝟐)𝟐[ 𝟐
(−(𝒏
𝒏
𝒏−𝟏𝒏−𝟐)(𝒏−
(𝒏−𝟐(𝒏−
(𝒏 𝟐)(𝒏−𝟏)
𝒏− 𝟐
(𝒏−𝟐)(𝒏−
)− 𝟐𝟏)
(𝒏 −𝟐
−𝒏+𝟐]
− 𝟏 𝟏)
))]
𝑪
𝑪
𝑪
𝑪
𝑪 𝑪 (𝒏
(𝒏
(()𝒏
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
(𝒏
𝒘𝒐𝒓𝒔𝒕 )=
=𝒏())𝒏−𝟏
=
=((=
)=(𝒏
𝒏
𝟐
𝒏−𝟏(𝒏 ) =𝑶 (𝒏 )
() =
𝒏
(𝒏−𝟏)
)( 𝒏−)()𝒏−
−𝟏)
( 𝒏−𝟏𝟐−
=𝟎+𝟏 𝟏) ∑

𝒊=𝟎𝟐𝟐 𝟐
−𝟐)(𝒏−
) − ) −𝟏 − ∑ 𝒊
𝟐𝟐𝟐
𝒊=𝟎
𝟏)

03/18/2024 K. Balakrishnan, 18CS42 80


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

Matrix Multiplication
ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1], B[0..n − 1, 0..n − 1])
//Multiplies two square matrices of order n by the definition-based algorithm
//Input: Two n × n matrices A and B
//Output: Matrix C = AB
for i := 0 to n − 1 do
for j := 0 to n − 1 do
C[i, j ] := 0;
for k := 0 to n − 1 do
C[i, j ] := C[i, j ]+ A[i, k] ∗ B[k, j];
return C;

Analysis
STEP 1 – Input size is n X n.

STEP 2 – Basic operation is multiplication.


03/18/2024 K. Balakrishnan, 18CS42 81
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1], B[0..n − 1, 0..n − 1])


//Multiplies two square matrices of order n by the definition-based algorithm
//Input: Two n × n matrices A and B
//Output: Matrix C = AB
for i := 0 to n − 1 do
for j := 0 to n − 1 do
C[i, j ] := 0;
for k := 0 to n − 1 do
C[i, j ] := C[i, j ]+ A[i, k] ∗ B[k, j];
return C;

STEP 3 – The basic operation depends only on the input size.

STEP 4 – Let M(n) be the number of times the multiplication


operation executes. We have ,

03/18/2024 K. Balakrishnan, 18CS42 82


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

STEP 5 – The summation is solved as follows.

03/18/2024 K. Balakrishnan, 18CS42 83


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 Although we have found out the complexity of the problem, we need


to remember that the basic operation is a combination of addition and
multiplication.

03/18/2024 K. Balakrishnan, 18CS42 84


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 Let be the time required to perform one multiplication and be the time
required to perform one addition.

 Then the total time spent in performing the basic operation, denoted by
T(n), is given by,

where, A(n) – Total number of times addition operation executes.

 In the algorithm, the number of additions is same as the number of


multiplications. Hence,
𝑻( (𝒏𝒏
𝑻
𝑻 ( )𝒏
=𝒄)=
) =𝒄∗
𝒎 𝒎𝑴
𝜽∗(𝒏
) (
𝒏 𝒏𝟑
) +¿
+𝒄𝒄¿ ∗
𝒂𝒂 𝑨
𝟑)
∗ (
𝒏 𝒏
¿𝟑
)

03/18/2024 K. Balakrishnan, 18CS42 85


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

Finding the number of binary digits in an integer


ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count := 1;
while n > 1 do
count := count + 1;
n := ;
return count;

n=8 n = 4128

Binary representation = 1000 count = 4321

03/18/2024 K. Balakrishnan, 18CS42 86


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count := 1;
while n > 1 do
count := count + 1;
n := ;
return count;

Analysis
STEP 1 – Input size is n.

STEP 2 – Basic operation is statements within the while loop.

03/18/2024 K. Balakrishnan, 18CS42 87


MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count := 1;
while n > 1 do
count := count + 1;
n := ;
return count;

STEP 3 – The basic operation depends on the input size only.

STEP 4 –
 Setting up a summation for this algorithm is not possible.

 This is because , the input size is halved in each iteration.


03/18/2024 K. Balakrishnan, 18CS42 88
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS

 In general, we can say that the input size is getting reduced by a


constant factor in every iteration.

 All algorithms in which the input size gets reduced by a constant


factor in each iteration belong to the Logarithmic class.

 Based on this, we can conclude that the run time of this algorithm
denoted by T(n) is,
T(n) =

03/18/2024 K. Balakrishnan, 18CS42 89


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

General Plan for Analysing the Time Efficiency of Recursive


Algorithms
STEP 1 - Decide on a parameter (or parameters) indicating an input’s size.

STEP 2 - Identify the algorithm’s basic operation.

STEP 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.

STEP 4 - Set up a recurrence relation, with an appropriate initial condition, for the
number of times the basic operation is executed.

STEP 5 - Solve the recurrence or, at least, ascertain the order of growth of its
solution.

03/18/2024 K. Balakrishnan, 18CS42 90


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Factorial of a given non negative integer


ALGORITHM F(n)
//Computes n! recursively F(3)
6
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 then return 1;
else return F(n − 1) ∗ n; 2F(2)
* 3 *= 36

1F(1)
* 2 *= 2

1F(0)
* 1 *= 11
03/18/2024 K. Balakrishnan, 18CS42 91
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Analysis
ALGORITHM F(n)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 then return 1;
else return F(n − 1) ∗ n;

STEP 1 – Input size is n.

STEP 2 – Basic operation is Multiplication.

STEP 3 – Basic operation depends only on the input size.


03/18/2024 K. Balakrishnan, 18CS42 92
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

STEP 4 – We build the recurrence for the basic operation as follows.

 let M(n) be the number of multiplications for n.

 The number of multiplications required for different n values are


presented below.
 n=5
5! = 5 * 4 * 3 * 2 * 1, M(5) = 4
 n=4
4! = 4 * 3 * 2 * 1, M(4) = 3
 n=3
3! = 3 * 2 * 1, M(3) = 2
 n=2
2! = 2 * 1, M(2) = 1
03/18/2024 K. Balakrishnan, 18CS42 93
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

 With this, we can conclude that,


M(n) = M(n-1) + 1

 Also, when n=0, there is no multiplication needed. Hence,


M(0) = 0

 With this, the recurrence relation for F(n) is,

STEP 5 – The above recurrence is now solved using backward


substitution method.

03/18/2024 K. Balakrishnan, 18CS42 94


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
 We have,
M(n) = M(n-1) + 1
M(n) = { M(n-2) + 1 } + 1
M(n) = M(n-2) + 2
M(n) = { M(n-3) + 1 }+ 2
M(n) = M(n-3) + 3
.
.
M(n) = M(n-i) + i
.
.
M(n) = M(n-n) + n
M(n) = M(0) + n
03/18/2024 M(n) = K.θ(n)
Balakrishnan, 18CS42 95
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Tower of Hanoi
Step 1 − Move n-1 disks from source to temp
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from temp to dest

Algorithm T(n, S, D, A)
if n = 1 then
move disk from source to dest
else {
T(n - 1, S, A, D);
move disk from source to dest
T(n - 1, A, D, S);
}
03/18/2024 K. Balakrishnan, 18CS42 96
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
 T(3,A,C,B) T(1,A,C,
AC
B)

T(2,A,B,
AB
C)

T(1,C,B,
CB
A)

T(3,A,C, AC
B)
T(1,B,A,
BA
C)

T(2,B,C,
BC
A)

T(1,A,C,
AC
03/18/2024 B)K. Balakrishnan, 18CS42 97
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
 The sequence of moves are,
A C, A B, C B, A C, B A, B C, A C

A B C
03/18/2024 K. Balakrishnan, 18CS42 98
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Analysis
Algorithm T(n, S, D, A)
if n = 1 then
move disk from source to dest
else {
T(n - 1, S, A, D);
move disk from source to dest
T(n - 1, A, D, S);
}

STEP 1 – Input size is the number of disks n.

STEP 2 – Basic operation is Moving the disks.

STEP 3 - Basic operation depends


03/18/2024 only on
K. Balakrishnan, n.
18CS42 99
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Algorithm T(n, S, D, A)
if n = 1 then
move disk from source to dest
else {
T(n - 1, S, A, D);
move disk from source to dest
T(n - 1, A, D, S);
}

STEP 4 – Let M(n) be the number of moves required for n disks. We


have the recurrence,

03/18/2024 K. Balakrishnan, 18CS42 100


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

STEP 5
M(n) =
M(n) =
M(n) =
M(n) =

M(n) =
M(n) =
M(n) =

M(n) =
M(n) =
03/18/2024
M(n) =
K. Balakrishnan, 18CS42 101
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Continuing this way we get,


M(n) =

Substituting the value of i = n – 1, we get,


M(n) =
M(n) =
M(n) =
M(n) =
M(n) =
M(n) =

M(n) =
03/18/2024 K. Balakrishnan, 18CS42 102
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

Finding the number of binary digits in an integer


ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 then return 1;
else return BinRec() + 1;

BinRec(4) BinRec(2) BinRec(1)


4
BinRec(8) 3+1=4
+1
2+1=3
+1
1+1=2
+1

03/18/2024 K. Balakrishnan, 18CS42 103


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 then return 1;
else return BinRec() + 1;

Analysis
STEP 1 – Input size is n.

STEP 2 – Basic operation is addition.

STEP 3 – Basic operation depends only on n.

03/18/2024 K. Balakrishnan, 18CS42 104


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 then return 1;
else return BinRec() + 1;

STEP 4 – Let A(n) be the number of additions performed for input


n. The recurrence for the algorithm is,

03/18/2024 K. Balakrishnan, 18CS42 105


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS

STEP 5
 One issue with the recurrence is the presence of the fraction n/2.

 This fraction will create problem when value of n is not powers of 2.

 In order to get a smooth solution for the recurrence, we adopt the


Smoothness Rule which states,
“The order of growth observed for n = gives a correct answer
about the order of growth for all values of n.”

 Hence, we have
n=

03/18/2024 K. Balakrishnan, 18CS42 106


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
 The generated recurrence is

 Substituting n = in the above recurrence,

03/18/2024 K. Balakrishnan, 18CS42 107


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
 We now solve the updated recurrence.

.
.

Substituting i = k, we get,

03/18/2024 K. Balakrishnan, 18CS42 108


MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
 After processing the recurrence, we have got,

 We also know that,

 Substituting k in the above equation,

03/18/2024 K. Balakrishnan, 18CS42 109


END OF
MODULE 1
03/18/2024 K. Balakrishnan, 18CS42 110

You might also like