Module-1 Part-2
Module-1 Part-2
Course objectives:
● To learn the methods for analyzing algorithms and evaluating
their performance.
● To demonstrate the efficiency of algorithms using asymptotic
notations.
Contents:
Part-1: Introduction
Part-2: Fundamentals of the Analysis of Algorithm Efficiency
Part-3: Brute Force Approaches
Vandana U
Assistant Professor
Department of AI-DS
MODULE-1
PART-2
FUNDAMENTALS OF THE
ANALYSIS OF ALGORITHM
EFFICIENCY
2.1 Analysis Framework: Computing Best,Worst Measuring Input Size
Measuring Space and Average Case
Complexity Efficiencies Measuring
Running Time
Measuring Time Computing Order of
Analysis of Algorithms
Complexity Growth of Algorithms
Efficiency of an Algorithm can be in terms of Time and Space. Thus, checking whether the algorithm is
efficient or not means analyzing the algorithm. There is a systematic approach that has to be applied for
analyzing any given application. This systematic approach is modelled by a framework called Analysis
Framework.
The efficiency of an algorithm can be divided by measuring the performance of an algorithm. We can
measure the performance of an algorithm by computing two important factors:
1.Amount of time required by an algorithm to execute : Time Complexity or Running Time or Time
Efficiency
2.Amount of storage required by an algorithm or the amount of memory units required by the algorithm,
including the memory needed for Input & Output: Space Complexity or Memory Space or Space
Efficiency
The reason for selecting these two criteria are:Simplicity, Generality, Speed, Memory
Point 1. Space Complexity: Amount of memory required by an algorithm to run.
To compute the space complexity, we use two factors: Constant and Instance
characteristics
The space requirement S(P) can be given as: S(P)=C+Sp
where,
•C is a constant, i.e., fixed part, and it denotes: The space of inputs and outputs and
Amount of space taken by instructions, variables, and identifiers
•Sp is a space-dependent part based on instance characteristics, i.e., a variable part,
where space requirements depend on a particular problem instance.
Example: The control statements Such as for, do, while, choice, switch, Recursion
stack for handling recursive calls.
For example: add(a+b) return a+b
S(P)=C+ Sp =C+0
=2 + 0 //a,b occupy one word size then total size comes to 2
=2
Point 2: Time Complexity Amount of time required by an algorithm to run to
completion.
•It is difficult to compute time complexity in terms of physically clocked time.
•For instance, in a multitasking system, execution time depends on many factors, such as:
System load, Number of other programs running, Instruction set used and Speed of
underlying hardware
•Therefore, time complexity is given in terms of frequency count.
•Frequency count is a count that denotes how many times a particular statement is
executed or the number of times a statement runs.
Example 1 : To print a number
void fun()
{
int a; -------------------------------------------- (1)
a=10; -------------------------------------------- (1)
printf("%d",a); -------------------------------------------- (1)
} Therefore, The Frequency Count is 3
Example 2 : To print a number
void fun() Example 3 : Calculating Sum of ‘n’ numbers
{ Frequency
Statement
Count
int a; --------------------------- (1)
for(i=0;i<n;i++) i=0 1
a=0; --------------------------- (1)
{ i<n n+1
for(i=0;i<n;i++) -------------------- (n+1)
sum=sum+a[i]; i++ n
a=a+i; ------------------ (n)
} Sum=sum+a[i] n
printf("%d",a); -------------------------- (1)
Total 3n+2
} Frequency count is 1+1+(n+1)+n+1 =
2n+4
Note: Time complexity normally denotes
Note: The ‘for’ loop in the snippet is executed
in terms of Oh Notation(O). Hence if we
‘n’ times when the condition is true and one
neglect the constants then we get the
more time when the condition is false. Hence
time complexity to be O(n)
the frequency count for ‘for’ loop is n+1.
Example 4 : Matrix Addition Statement Frequency Count
for(i=0;i<n;i++) i=0 1
{
i<n n+1
for(j=0;j<n;j++)
i++ n
{
j=0 n*1=n
c[i][j]=a[i][j]+b[i][j];
Initialization of ‘j’
}} Outer Loop
j<n n * (n+1) = n2 + n
j++ n * n = n2
c[i][j]=a[i][j]+b[i][j]; n * n = n2
There are many algorithms for which running time depends not only on an input size, but
also on the specifics of a particular input.
•The Worst case efficiency of an algorithm is its efficiency for the worst-case input of size n,
which is an input (or inputs) of size n, for which the algorithm runs the longest among all
possible inputs of that size.
•If an algorithm takes the maximum amount of time to run to completion for a specific set of
inputs, then it is called worst-case time complexity.
Example: While searching for an element using the linear search method, if the desired
element is placed at the end of the list, then we get worst-case time complexity. Cworst(n)=n
•The best-case efficiency of an algorithm is its efficiency for the best-case input of size n,
which is an input (or inputs) of size n, for which the algorithm runs the fastest among all
possible inputs of that size. OR
If an algorithm takes the minimum amount of time to run to completion for a specific set of
inputs, then it is called best-case time complexity.
Example: While searching for an element using linear search, if the desired element is
placed at the first position itself, then we get best-case time complexity. Cbest(n)=1
•The Average Case Efficiency: Neither the worst-case nor the best-case analysis provides
complete information about an algorithm's behavior on a "typical" or "random" input. To
analyze an algorithm's average-case efficiency, we must make some assumptions about
possible inputs of size n.
ie., Cavg(n) = Probability of successful search + Probability of unsuccessful search
•If P=1(The Search is successful), the average number of key comparisons made by sequential
search is (n+1)/2 , ie, the algorithm will input, on average about half of the elements.
•If P=0 (The search must be unsuccessful, the average number of key comparisons made will e
n, because the algorithm will input all n elements on all such inputs.
•Time space tradeoff is basically a situation where either space efficiency can be achieved at
the cost of time or time efficiency can be achieved at the cost of memory
Asymptotic Notations and Basic Efficiency Classes
Vandana U
Assistant Professor
Dept. of AI-DS
1. Important Rules of Sum Manipulation:
2. Summation Formulas
General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms
1. Decide on parameters indicating input size.[Input’s size].
2. Identify the basic operation. [Basic Operation].
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-case, average-case, and, if necessary, best-case efficiencies have to be
investigated separately. [Check if basic operation depends on specifics of the
input. If so, check for best, worst and average time efficiency.]
4. Setup a sum expressing the number of times the algorithm’s basic operation
is executed. [Sum of algorithm’s basic operations].
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.
[Use formulas and sums to establish order of growth].
Let us start with a very simple example that demonstrates all the
principal steps typically taken in analyzing such algorithms.
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-case, average-case, and, if
necessary, best-case efficiencies have to be investigated separately.
Since no matter what the product of the matrix will be done for all the indexes, we can say
that there are no best, average and worst case efficiency for this algorithm. So third step can be
omitted
Now we will analyse the efficiency of an
algorithm :
4. Setup a sum expressing the number of times
the algorithm’s basic operation is executed.
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.
EXAMPLE 4 The following algorithm finds the number of binary digits in the binary
representation of a positive decimal integer.
1. Decide on parameters indicating input size:
In this algorithm , the input size is ‘n’
2. Identify the basic operation :
Here, basic operation is comparison .
First, notice that the most frequently executed
operation here is not inside the while loop but rather
the comparison n>1 that determines whether the
loop’s body will be executed.Since the number of
times the comparison will be executed is larger than
the number of repetitions of the loop’s body by
exactly 1, the choice is not that important.
Point 3,4 and 5 cant be applied directly since all values will not be accepted.
Therefore, we have to calculate it in an alternate way.
Mathematical Analysis of
Recursive Algorithms
Vandana U
Assistant Professor
Dept. of AI-DS
1. Important Rules of Sum Manipulation:
2. Summation Formulas
General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms
1. Decide on parameters indicating input size. [Input’s size].
2. Identify the basic operation. [Basic Operation].
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-case, average-case, and, if
necessary, best-case efficiencies have to be investigated separately. [Check if basic operation
depends on specifics of the input. If so, check for best, worst and average time efficiency.]
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.
EXAMPLE 1 : Compute the factorial functionF(n)=n! foran arbitrary non-
negative integer n.
Since n!=1,…..,(n −1).n = (n−1)!.n for n≥1 and 0!=1 by definition,
we can compute F(n)=F(n−1).n with the following recursive algorithm.
Now we will analyse the efficiency of an
algorithm :
1. Decide on parameters indicating input size:
In this algorithm , the input size is ‘n’
2. Identify the basic operation :
Here, basic means the instruction that gets
executed maximum number of times.
The basic operation of the algorithm is multiplication
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-case, average-case, and,
if necessary, best-case efficiencies have to be investigated separately. [Check if basic
operation depends on specifics of the input. If so, check for best, worst and average time
efficiency.]
Since the algorithm works for every iteration until ‘n’, there is no need to check
best,average and best case efficiency.
Now we will analyse the efficiency of an algorithm :