0% found this document useful (0 votes)
3 views

Analysis of Algorithm

Uploaded by

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

Analysis of Algorithm

Uploaded by

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

Analysis Of Algorithm

Sushma Prajapati
Assistant Professor
CO Dept
CKPCET,surat
Email:sushma.prajapati@ckpcet.ac.in
Outline
The efficient algorithm

Average, Best and worst case analysis

Asymptotic Notations

Analysing control statement


The efficiency of Algorithm
● Algorithm efficiency is a property of algorithm which relates to number of
computational resources used by algorithm
● Generally resources by means of
○ Space
○ Time
Analysis Of Algorithm
1. Performance Analysis
● Predicting the resources required(before estimate)
○ Which elements?
○ Whether algorithm is providing exact solution?
○ Easy to understand?
○ Easy to implement?
○ Space?
○ time? Asymptotic Analysis

Gives information about running time of algorithm.


Analysis Of Algorithm (Contd…)
1. Performance Measurement(After Estimate)
● By measuring performance of an algorithm we can determine which algorithm
is better than the other one.
Complexity Of Algorithm
● It is a measure of the amount of the time or space required by the algorithm for an
input of a given size
● Space Complexity
○ amount of space required by an algorithm.By computing it we can analyze whether an algorithm
takes more or less space.
○ Generally represented as:
■ S(p) = C + Sp
■ Where, C constant and Sp is variable part in the algorithm

Example
Complexity Of Algorithm(Contd…)
Space Comlexity Examples:

Iterative function for sum:


Requires space for a[1...n],i,s,n
S(p) = n+3
Complexity Of Algorithm(Contd…)
● Time Complexity:
○ Time complexity of an algorithm is the amount of time taken by an algorithm to run.By computing it,
we come to know whether algorithm is slow or fast.
● Generally our problem is either iterative or recursive
● We follow the frequency count method to calculate the time complexity
● Frequency Count
○ Syntactically or Semantically meaningful segment of a program that has an execution time that is
independent of the instance characteristics
○ Comments, Declaration - 0
■ Assignment Statement - 1
■ Return - 1
Complexity Of Algorithm(Contd…)
Time Complexity Example1
Complexity Of Algorithm(Contd…)
Time Complexity Example2
Average, Best and Worst case analysis
● Best case analysis
○ It is the function defined by the minimum number of steps taken on any instance of size n.
○ For example, while searching an element by using linear searching method we get desired element
at first place itself then it is called best case time complexity.
○ Example:-
○ List is:- 6,8,9,50,4 and we have to find 6.
○ Since only single comparison is made to search so the best case efficiency will be,
○ cbest(n) =1
Average, Best and Worst case analysis
● Worst case analysis
○ It is the function defined by the maximum number of steps taken on any instance of size n.
○ Example:-
○ Let we have to find 20 in list :- 2,5,6,8,20
○ Therefore we have to make 5 comparisons for searching so,
○ cworst(n) = n
○ If we want to find the number 3 in above list, we have to make 5 comparisons to search so the worst
case complexity will be,
○ cworst(n) = n
Average, Best and Worst case analysis
● Average case analysis
○ All possible cases/no. Of cases (very difficult)
○ Generally similar to worst case
○ For example, while searching an element by using linear searching method if desired element is not
present at the first or last position may be somewhere else in the list.
○ Consider A = 1,6,4,3,2,7,8,9.10 ….n then
■ Cn = All possible case time/No. Of cases
■ 1+2+3+4+......+n/n
■ =(n*(n+1)/2)/n
■ =(n+1)/2
Asmptotic Notations
● Term comes from mathematics
● Used to represent simple form of function whose domain are the set of natural
numbers.
● If so many algorithms for the same problem
○ How to find best one ?
○ How to Compare two algorithm by some means?
● Asymptotic notations is used to describe the running time of an algorithm in terms
of functions whose domains are N = {0,1,2,...}.
● It describes the algorithm efficiency and performance in a meaningful way.
Asymptotic Notations
● Most commonly used three notations are,
○ Big-oh - “O”
○ Big-omega - Ω
○ Big-theta - Θ
Asymptotic Notations(Big oh Notation)
The Big-0 notation gives an upper bound on the growth rate of a function

how fast a function grows with the input size

Given function f(n) and g(n), we say that f(n) is О(g(n)) if there are positive constants c >
0 and n0 ≥ 1 , such that

f(n) ≤ c*g(n) for all n , n ≥ n0


Asymptotic Notations(Big oh Notation)
Graphical representation of Big oh
Asymptotic Notations(Big oh Notation)
Example: f(n) = 3n+2, g(n) = n

To say that f(n) = O g(n),

We need to prove that, f(n) <= cg(n); where c > 0, n0 >= 1

3n+2 <= cn; If we substitute c = 4, then 3n+2 <= 4n.

If we simplify n >= 2.

Therefore for every n >= 2 and c = 4, f(n) <= c g(n). So f(n) = O g(n).
Asymptotic Notations(Big omega Notation)
The Big-Ω notation provides an asymptotic lower bound .

Given function f(n) and g(n), we say that f(n) is Ω(g(n)) if there are positive constants c >
0 and n0 ≥ 1 such that

f(n) ≥ c*g(n) for all n , n ≥ n0


Asymptotic Notations(Big omega Notation)
Graphical representation of Big - omega
Asymptotic Notations(Big omega Notation)
Example

f(n) = 3n+2, g(n) = n

Now check can this f(n) has lower bound as g(n) or not.

f(n) = Ω g(n), this will happen only if f(n) >= c g(n).

In our example, 3n+2 >= cn.

Here c = 1 and n0 >= 1.

This is satisfied so f(n) = Ω g(n).


Asymptotic Notations(Big theta Notation)
Let f(n) and g(n) be two asymptotically positive real-valued functions. We say that f(n) is
𝚯(g(n)) if there is an integer n0 and positive real constants c1 and c2 such that

c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0

By this method the running time is between upper bound and lower bound.
Asymptotic Notations(Big theta Notation)
Graphical representation of theta notation
Asymptotic Notations(Big theta Notation)
F(n) = 3n+2, g(n) = n

F(n) <= c g(n) where c = 4; That is 3n+2 <= 4n. This is valid for all n0>= 2.So we can say
g(n) is upper bound for f(n).

Now see it is as well as lower bound also.

F(n) >= c g(n); That is 3n+2 >= n. where n0 >= 1.So both the cases are valid for this.
Properties of Asymptotic Notations
Let f and g be the functions and a and b be the real numbers.

1. f(x) is O(g(x)) ⇔ a ≤ b

2. f(x) is Ω(g(x)) ⇔ a ≥ b

3. f(x) is Θ(g(x)) ⇔ a = b

4. f(x) is o(g(x)) ⇔ a < b

5. f(x) is ω(g(x)) ⇔ a > b


Analysing control statement
● To analyze an algorithm, we must notice that each instruction affects the overall
performance of the algorithm and therefore, each instruction must be analyzed
separately to analyze overall performance.
● However, there are some algorithm control structures which are present in each
programming code and have a specific asymptotic analysis.
Analysing control statement (Contd…)
● Various control statements are considered while analyzing the algorithm.Those are:
○ Sequencing
○ If-then-else
○ for loop
○ While loop
Analysing control statement - Sequencing
Suppose the algorithm consists of two parts A and B. A takes time tA and B takes
time tB for computation. According to maximum rule, this computation time is
(max (tA,tB)).

Computation Time = tA + tB

= (max (tA,tB)

= (max (O (n), θ (n2)) = θ (n2)


Analysing control statement - if-then-else
According to the maximum rule, this computation time is max (tA,tB).

Total Computation = (max (tA,tB))


= max (O (n2), θ (n2) = θ
(n2)
Analysing control statement - for loop
For i ← 1 to n For i ← 1 to n
{
{ For j ← 1 to n
P (i) {
} P (ij)
}
}
References
● Computer Algorithm By sartaj sahni
● Introduction to Algorithms,Second edition By Corman
● https://www.tutorialspoint.com

You might also like