Académique Documents
Professionnel Documents
Culture Documents
Contents
Analysis of algorithm: frequency count and its
importance in analysis of an algorithm,
Time complexity & Space complexity of an algorithm, Big
‘O’, ‘Ω’ and ‘θ’ notations, Best, Worst and Average case
analysis of an algorithm.
Algorithms
• An algorithm is simply a set of rules for carrying
out some task, either by hand, or more usually, on
a machine.
• Performance of software depends upon –
• The algorithm chosen
• The suitability and efficiency of various layers of
implementation
• Analysis of Algorithm –
• Algorithm heavily depend on organization of data.
• Analysis involves measuring the performance of an algorithm
• System dependent
Calculation of s/e.
2) Little Oh (o)
3) Omega (Ω)
5) Theta (θ)
Representation 0f “Big Oh”:
Function f (n) = O (g ( n) ) iff there exist
positive constants c and no such that
f (n) ≤c* g (n) for all n,n ≥ no
Function or Term Name
O (1) Constant
O (n) Linear
O (n2) Quadratic
O (n3) Cubic
O (2n) Exponential
O ( n log n) n log n
n! Factorial
O ( log n ) Logarithmic
Big Oh notation
• Set of all functions whose rate of growth is the same
as or lower than that of g(n).
f (n) ≤c* g (n) for all n,n ≥ no
c1*g(n)≤ f(n)≤c2*g(n)
for all n, n ≥no
• g(n) is an asymptotically
tight bound for f(n).
Relations Between
Theta,Oh,Omega
Comparison of Functions
f g a b
f (n) = O(g(n)) a b
f (n) = (g(n)) a b
f (n) = (g(n)) a = b
f (n) = o(g(n)) a < b
f (n) = w (g(n)) a > b
3. Graphical Representation
Time Complexity
• Linear Loops:
• Addition & Subtraction
• for (i=0; i<1000; i++)
application code
• f(n) = n
• for (i=0; i<1000; i+2)
application code
• f(n) = n/2
• Plotting points – straight line
Time Complexity
• Logarithmic Loops:
• Divide & Multiply
• for (i=0; i<1000; i*2)
application code
• f(n) = n
• for (i=0; i<1000; i/2)
application code
• f(n) = log n
• Controlling variable is multiplied or divided in each
iteration
Time Complexity
• Nested Loops:
• Loops inside loops
• Types -
• Linear Logarithmic
• Quadratic
• Dependent Quadratic
Time Complexity
• Nested Loops:
• Linear Logarithmic:
• for (i=0; i<10; i++)
for (j=0; i<10; j*2)
application code
• Inner loop = 10
• Outer loop = 10
• The number of times inner loop executes is the
same as the outer loop
• 10 X10
• f(n) = n2
Time Complexity
• Nested Loops:
• Dependent Quadratic:
• for (i=0; i<10; i++)
for (j=0; j<i; j++)
application code
• Inner loop depends on the outer loop for one of its
factor
• f(n) = n (n+1)/2
4. Analysis of Searching Algorithm
1. Sequential Search or Linear Search :
Function for Sequential Search:
void seq_search (int a[ ] , int key, int n)
{
int i, flag, loc;
i=1; flag=0;
for (i=0; i<n; i++) {
if (a[i] == key) {
loc=i;
flag=1; break;
}
}
if (flag== 1)
“Element found in array” .
else
“ Element not present in array ” .
}
Time Complexity:
1) Best Case : O (1)
2) Worst Case : O (n) as (n+1) key comparisons.
3) Average Case: O (n) as (n+1)/2 key comparisons.
Let Pi = Probability that element to be searched will
found at ith position.
i = number of comparisons required to search the ith
element.
Therefore, Total comparisons,
C = 1*P1+2*P2+3*P3+………+n* Pn.
As the element could be found at any location with
equal probability i.e. P1=P2=........... Pn =1/n
So, C=1/n +2/n +….n/n=a/n(1+2…+n)
=n(n+1)/2n
= (n+1)/2 = O (n)
2. Binary Search: Function for Binary Search:
int search (int a[ ] , int n, int x)
{ int first=0,last=n-1,mid;
while (first<=last)
{
mid= (first + last)/2;
if (a [mid] ==x)
return mid;
else if (x<a [mid])
last = mid-1;
else
first = mid + 1;
} return (0);
}
2. Binary Search: Worst case Behaviour
(unsuccessful search) :
• Assume number of array elements = n
• After 1 comparison maximum number of elements
= n/2.
• After 2 comparisons =n/22.
• After m comparisons =n/2h
• Lowest value of comparison will be h[1] i.e.
Maximum number of elements left =1
n/2h =1 or 2h = n
i.e. h = Log2n
= O(Log2n)
3. Binary Search: Best case Behaviour
}
1) Insertion Sort : Analysis
Best Case : O (n)
T (n) = 2T (n/2) + c * n
Divide by n:
T (n) / n = T (n/2) / (n/2) + c
Telescoping:
T (n/2) / (n/2) = T(n/4) / (n/4) + c
T(n/4) / (n/4) = T(n/8) / (n/8) + c
……
T(2) / 2 = T(1) / (1) + c
Add all equations:
T (n) / n + T (n/2) / (n/2) + T (n/4) / (n/4) + …. + T(2) / 2
= (n/2) / (n/2) + T(n/4) / (n/4) + … + T(1) / (1) + c.*log n
After crossing the equal terms:
T (n) /n = T(1) + c Log n = 1 + c * Log n
T (n) = n + n c * Log n
Therefore T (n) = O (n log n)
Worst case analysis
The pivot is the smallest element, Then one of the
partitions is empty, and we repeat recursively the
procedure for n-1 elements.