Professional Documents
Culture Documents
Complexity PDF
Complexity PDF
• A problem in a Data Structure may be solved by applying more than one Logic.
• Logic for a problem is often expressed in terms of Algorithms.
• There may be ‘n’ number of Algorithms (or n number of ways) to solve a
particular problem.
• Main Objective: To choose out the best Algorithm to solve a Problem by
comparing them on the basis of their Performance.
Performance of an Algorithm is measured in terms of two parameters :
• A)Time Complexity B) Space Complexity.
• Time Complexity:It measures the amount of time taken by an
algorithm to run as a function of the length of the input data.
Average Case:Average amount of time algorithm takes in terms of its Input size.
Worst Case: Maximum amount of time algorithm takes in terms of its Input size.
• Types of Asymptotic Notation:
• A) Ο Notation (Big O Notation) - measures the Worst Case Time Complexity
• B) Ω Notation (Omega Notation) - measures the Best Case Time Complexity
• C) θ Notation (Theta Notation) - measures the Average Case Time Complexity
How to calculate the Time Complexity of the given Code:
Assumptions :
A)All the arithmetic and logical operations takes 1 unit of time.
B)All the assignment operations takes 1 unit of time.
C)All the return statement takes 1 unit of time.
Number of Times Total Cost = 1.1+1.1 + 2.n+1 + 2. n + 1.1
int Sum_Numbers() Cost Number of Times =1 +1+ 2n +1+2n+1
= 4+ 4n is the running time complexity of the
{ algorithm whose value depends upon the size of
Sum:=0 //only assignment operation 1 1 input ‘n’.
If we take n = 5 , then 4+4.5 = 24units of time it
i=0 1 1
consumes but as we increase the size of the
for( ; i<n ; i++) 2 n+1 input n = 100 ,then 4 + 4.100 = 400units of time
it consumes .
{
As we increase the size of the input ,more will be
sum = sum + i; 2 n its Time Complexity.
4+4n is of form an + b where a and b are
} constants
return Sum; 1 1
} In Asymptotic Notation ,the lower order terms and
constant value are negligible as we increase the size
of n.
Consider the following Code: Cost Number of Times
i =1; 1 1
Sum = 0; 1 1
while(i<=n) 1 N+1
j = 1; 1 N
while(j<=n) 1 (N+1) * N
sum = sum+1; 2 N * N
j = j +1; 2 N *N
i = i + 1; 2 N
Neglecting all the lower or der ter ms and constants we left with N2 which is equal
to O (n 2).
• Time Complexity:It measures the amount of time taken by an
algorithm to run as a function of the length of the input data.
f(n) = 3n + 2
g(n) = n
f(n)<c.g(n) = 3n+2<c.n
H er e if we tak e
3n+2<=4.n
for(i=1;i<n-1;i++)
{
temp = a[i];
pos=i;
while(pos>0 && a[pos-1]>temp)
{
a[pos] = a[pos -1];
pos = pos-1;
}
a[pos] = temp;
}
• Best Case :
• When the elements are sorted
• 12345
• 1|2345
Pass 1 : 1>2(False, One Comparison) 1 2|3 4 5
Pass 2 : 2>3(False, One Comparison) 1 2 3 | 4 5
Pass 3 : 3 >4(False, One Comparison) 1 2 3 4 | 5
Pass 4: 4>5(False,One Comparison) 1 2 3 4 5| Unsorted List Empty
To sort 'n' elements we need 'n-1' passes and in each (n-1 ) passes
we have to calculate how many comparisons we perform.
Here in the Best Case,we go for (n-1) passes and in each pass we perform only 1
comparison ,so = 1 *(N-1) = N-1 , neglecting constants , best case = Ω(n)
• Worst Case
The elements are unsorted 5 4 3 2 1
5|4 3 2 1
In Pass 1, 5>4 only 1 comparison 4 5| 3 2 1
In Pass 2, 5>3 and 4> 3 ,two comaprisons 3 4 5| 2 1
In Pass 3 ,2>5,2>4,2>3, three comparisons , 2 3 4 5 | 1
:
:
:
For Pass ( N-1) there can be (N-1) comparisons
Adding the No of comparisons from each pass
1+2+3+................+(N-1) = N(N-1)/2 = O(N2)