analysisofAlgorithms-Basics
analysisofAlgorithms-Basics
Algorithm
Algorithm
An algorithm is a finite set of instructions which if followed accomplish a
particular task. Every algorithm must satisfy the following criteria
1) Input: There are zero or more quantities which are externally
supplied
2) Output: At least one quantity is produced
3) Definiteness: Each instruction must be clear & unambiguous
4) Finiteness: If we trace out the instructions of an algorithm, then for
all cases the algorithm will terminate after a finite number of steps
5) Effectiveness: Every instruction must be sufficiently basic
Analysis of Algorithms
Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before implementation and
after implementation. They are the following −
Algorithm Complexity
Time Complexity
Space Complexity
Time Complexity
Time Complexity
Time Complexity
Frequency count
sum(int n)
{ sum=0;
for (i=1;i<=n;i++)
{
sum=sum+i;
}
print sum;
}
Analysis of Algorithms
Frequency count
sum(int n)
{ sum=0;
for (i=1;i<=n;i++)
{
sum=sum+i;
}
print sum;
}
• Frequency count , the number of times the statement is executed in the
program.
Analysis of Algorithms
Frequency count
sum(int n)
{ sum=0; : 1 time
for (i=1;i<=n;i++) : n+1 times
{
sum=sum+i; : n times
}
print sum; : 1 time
}
• Total: 2n+3
• O(n)
Analysis of Algorithms
Asymptotic Notations
O - Big Oh notation
Ω - Big omega notation
Θ - Theta notation
o - Little oh notation
ω - Little omega notation
Analysis of Algorithms
Big O notation
O(1) - O(1) describes an algorithm that will always execute in the same
time (or space) regardless of the size of the input data set.
Big O notation
For example, when analyzing some algorithm, one might find that the
time (or the number of steps) it takes to complete, a problem of size n
is given by T(n)=4n2-2n+2.
Big O notation
O(nc) and O(c n) are very different. The latter grows much,much faster,
no matter how big the constant c is.
Big Oh Notation, Ο
g(n) = n2
eg. Ω(n2)
Analysis of Algorithms
g(n) = n2
f(n)
3n
2n
n3
n2
Analysis of Algorithms
Big omega notation
g(n) = n 2
3 n
Nn2
2N
n 3
Logn
n12
Analysis of Algorithms
Little-o notation
f(n) = o(g(n)) means for all c > 0 there exists some k > 0 such that
0 ≤ f(n) < cg(n) for all n ≥ k.
Little- ω notation
f(n) = ω (g(n)) means that for any positive constant c, there exists a
constant k, such that 0 ≤ cg(n) < f(n) for all n ≥ k.
Analysis of Algorithms
Thank You