Data Structures and
Algorithms
Module -2
Algorithm Analysis
Dr. R. Jothi, SCOPE
VIT Chennai
Outline
Time and space Complexity (already discussed, see
ppt Complexity)
Asymptotic notations
Algorithm Analysis
Dr. R. Jothi, VIT Chennai
Outline
Time and space Complexity
Asymptotic notations
Algorithm Analysis
Dr. R. Jothi, VIT Chennai
Running Time
Running time depends on the input
Interested in upper bounds on the running time
in terms of input size
Guarantee that algorithm wont take more than
O(n) time for input size n !!!
Dr. R. Jothi, VIT Chennai
Asymptotic Analysis
Ignore machine dependent constants
The running time of an algorithm T(n) as input size
approaches infinity is called the asymptotic running time
Look at growth of T(n) as n→ ∞
Dr. R. Jothi, VIT Chennai
Asymptotic Notations
Big- O O(n) - Asymptotic upper bound
Bubble sort needs atmost O(n2) time
Theta Θ(n) - Asymptotic tight bound
Push in stack needs exactly Θ(1) time
Omega Ω(n) - Asymptotic lower bound
Insertion sort needs atleast Ω(n) time in best case
Dr. R. Jothi, VIT Chennai
Big O Notation
O(g(n)) = { f (n) : there exist positive constants c and n0 such that
0 ≤ f (n) ≤ c g(n) for all n >= n0 }
f(n)=O(g(n))
Dr. R. Jothi, VIT Chennai
c *g(n)>f(n)
Dr. R. Jothi, VIT Chennai
Θ Notation
Θ(g(n)) = { f (n) : there exist positive constants c1 , c2 , and n0 such
that 0 ≤ c1 g(n) ≤ f (n) ≤ c2 g(n) for all n >= n0 }
Dr. R. Jothi, VIT Chennai
Omega: Ω Notation
Θ(g(n)) = { f (n) : there exist positive constants c1 , c2 , and n0 such
that 0 ≤ c g(n) ≤ f (n) for all n >= n0 }
Dr. R. Jothi, VIT Chennai
Common growth rates
Dr. R. Jothi, VIT Chennai
Analysis may be
Worst-case (most of the time)
T(n) = maximum time of algorithm on any input of size n
Average-case (sometimes)
T(n) = expected time of algorithm on any input of size n
Best-case (rarely)
Luckily works fast for some input
Dr. R. Jothi, VIT Chennai
Example
Dr. R. Jothi, VIT Chennai
Dr. R. Jothi, VIT Chennai
Insertion Sort Analysis
Dr. R. Jothi, VIT Chennai
Example
Suppose input seq is
5 4 3 2 1
While loop runs for n-1 times
Dr. R. Jothi, VIT Chennai
O(1) constant time
Algorithm requires the same fixed number of steps regardless of
the size of the task.
One arithmetic operation (eg., +, *)
One assignment
One test (eg., x==0) i=i+1;
One read(accessing an element from an array) Y=i**I;
A[i]=Y
Total time O(1)
Dr. R. Jothi, VIT Chennai
O(n) linear time
Algorithm requires a number of steps proportional to the size of
the task
Traversing an array.
Sequential/Linear search in an array.
Best case time complexity of Bubble sort (i.e when the elements of
array are in sorted order).
for(i=0;i<n;i++)
V[i]=V[i]*5;
for (i = 0; i < N; i++) { Total time O(n)
sequence of statements of O(1)
}
Dr. R. Jothi, VIT Chennai
O(n2) Quadratic time
Algorithm requires a number of operations which is proportional
to the size of the task squared
Bubble sort
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
sequence of statements of O(1)
}
}
Dr. R. Jothi, VIT Chennai
#Bubble sort
for (i = 0; i < N; i++) {
for (j = i+1; j < N; j++) {
sequence of statements of O(1)
} T(N) = N(N-1)/2
O(N2)
} Value of i No. of iterations of
inner loop
0 N-1
1 N-2
N-2 1
N-1 0
Dr. R. Jothi, VIT Chennai
f(n)= log2n.
22
Exercises
Dr. R. Jothi, VIT Chennai