1. Program: BE (Computer Engineering & Information Technology)
Class: TE
Course: Design and Analysis of Algorithm
Unit 01: Introduction to DAA
Lecture 02: Analysis of Algorithm and Asymptotic Notations
2. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
In designing of Algorithm, complexity analysis of an algorithm is
an essential aspect.
Mainly, algorithmic complexity is concerned about its
performance, how fast or slow it works.
There are many criteria upon which the performance of an
Algorithm can be decided like,
Does it do what we want it to do?
Does it work correctly according to the original specifications
of the task?
Is there documentation that describes how to use it & how it
works?
Are procedures creates in such a way that they perform logical
sub functions?
Is the code readable?
04
3. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
These criteria are all vitally important when it comes to write or
design the software’s for a large system.
But the most important factors for measure the performance of an
Algorithm by computing two factors.,
Amount of time required by an Algorithm to execute i. e. Time
Complexity.
Amount of space required by an Algorithm i. e. Space
Complexity.
05
4. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
1. Time Complexity
The time complexity of an Algorithm is the amount of computer
time required by an Algorithm to run to completion.
It is difficult to compute the time complexity in terms of physical
clocked time.
For instance in multiuser system, executing time depends on many
factors such as,
System Load.
Number of other programs running.
Instruction set used.
Speed of underlying hardware.
The time complexity is therefore given of frequency count.
06
5. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
1. Time Complexity
Frequency count is a count denoting number of time of execution of
statement.
If the input size is longer, then usually Algorithm runs for a longer
time.
Time Complexity denoted by,
T(n) = Cop * C(n)
Where,
T(n) = Running Time of basic operation.
Cop = Time taken by the basic operation to execute.
C(n) = Number of times the operation needs to be executed.
07
6. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
2. Space Complexity
The space complexity of an Algorithm is the amount
memory required by an Algorithm to run.
It’s a function describing the amount of memory an
algorithm takes in terms of the size of input to the
algorithm. We often speak of "extra" memory needed,
not counting the memory needed to store the input itself.
Space complexity is sometimes ignored because the
space used is minimal and/or obvious, however
sometimes it becomes as important an issue as time.
Space complexity depends upon two factors,
Constant Characteristics.
Instance Characteristics.
08
Space Complexity denoted by,
S(p) = C + Sp
Where,
S(p) = Space Complexity.
C = Constant.
Sp = space dependent upon instance
characteristics.
7. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
Best Case, Worst Case, & Average Case Analysis
Best Case
If an Algorithm takes minimum amount of time to run to
completion for a specific set of input then it is called as Best
Case Time complexity.
Best Case Time Complexity denoted as,
Cbest = 1
Example: While searching a particular element by using
sequential search we get the desired element at first place
itself then it is the best case.
09
8. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
Best Case, Worst Case, & Average Case Analysis
Worst Case
If an Algorithm takes maximum amount of time to run to
completion for a specific set of input then it is called as Worst
Case Time complexity.
Worst Case Time Complexity denoted as,
Cworst = n
Example: While searching an element by using liner searching
methods if desired element is placed at the end of the list then
we get the Worst Case Time Complexity.
10
9. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Analysis of Algorithm
Best Case, Worst Case, & Average Case Analysis
Average Case
The Time Complexity for certain set of inputs is as a average
same. Then for corresponding input such a time complexity is
called Average Case Time Complexity.
For calculating Average Case Time Complexity we have to
consider probability of getting success of the operation.
Any operation in the Algorithm is heavily dependent on input
elements.
Thus computing Average Case Time Complexity is difficult
than computing Worst Case & Best Case Time Complexity
Average Case Time Complexity denoted as,
Cavg (n) = (n+1)/2
11
10. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Asymptotic Notations
Execution time of an algorithm depends on the instruction set,
processor speed, disk I/O speed, etc. Hence, it estimate the efficiency
of an algorithm asymptotically.
To choose the best Algorithm, we need to check the efficiency of
each Algorithm. The efficiency can be measured by computing Time
Complexity of each Algorithm.
Asymptotic notation is a shorthand way to represent the Time
Complexity.
Using the Asymptotic notations we can give Time Complexity as
“Fastest Possible”, “Slowest Possible”, or Average Time”.
Following asymptotic notations are used to calculate the running time
complexity of an algorithm.
1. Big Oh (O)
2. Omega (Ω)
3. Theta (θ)
12
11. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Asymptotic Notations
1. Big Oh (O) : Asymptotic Lower Bound
It is a method of representing the upper bound of algorithm’s running
time.
Big oh notation is used to find the longest amount of time taken by
the Algorithm to complete.
Let f(n) & g(n) be two non-negative functions. Let n0 & constant C
are two integers, such that n0 denotes some value of input & n > n0.
Similarly C is some constant such that C > 0.
We can write,
f(n) ≤ C * g(n)
Then f(n) is Big Oh of g(n). it is also denoted as f(n) € O(g(n)).
In other words f(n) is less than g(n), if g(n) is multiple of some
constant C.
13
12. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Asymptotic Notations
2. Omega (Ω)
It is used to represent the lower bound of Algorithm’s running time.
It also denotes the shortest amount of time taken by Algorithm.
A function f(n) is said to be in Ω(g(n)), if f(n) is bounded by some
positive constant multiple of g(n) such that,
f(n) ≥ C * g(n) for all n > n0
It is denoted as f(n) € Ω (g(n)).
14
13. www.sandipuniversity.edu.in www.sandipfoundation.org
Department of Computer Engineering & Information Technology, SITRC, Nashik
Asymptotic Notations
3. Theta (θ)
It shows the running time is between upper bound and lower bound.
Let f(n) and g(n) be two non negative functions, there are two
constants namely c1 and c2 such that,
c2 g(n) ≤ f(n) ≤ c1 g(n)
It is denoted as f(n) € (g(n)).
15