You are on page 1of 10

Algorithm Analysis and

Complexity

P. P. Chakrabarti

1 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Analysis of Algorithms
? Quantifying the resources required.
? Measures of resource utilization (efficiency):
? Execution time ? time complexity
? Memory space ? space complexity
? Observation :
? The larger the input data the more the resource
requirement: Complexities are functions of the
amount of input data (input size).

2 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Selection Sort on two computers

Array Size Computer 1 Computer 2


n ms ms
125 12.5 2.8

250 49.3 11.0

500 195.8 43.4

1000 780.3 72.9

2000 3114.9 690.5

3 08-04-03 P.P.Chakrabarti, IIT Kharagpur


What do we use for a yardstick?
?The same algorithm will run at different speeds
and will require different amounts of space when
run on different computers, different
programming languages, different compilers.
?Algorithms usually consume resources in some
fashion that depends on the size of the problem
they solve.
?Need a machine independent complexity
measure that is a function of input size, n. (Also
we are interested at asymptotic behaviour, that
is, when n becomes large.

4 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Analyzing SelSort
int max_loc(int x[], int k, int size)
{ int j, pos; ? Computer 1:
pos = k; f1(n) = 0.0007772 n2 + 0.00305 n +
for (j=k+1; j<size; j++) 0.001
if (x[j] > x[pos]) ? Computer 2:
pos = j; f2(n) = 0.0001724 n2 + 0.00040 n +
return pos; 0.100
}
int selsort (int x[], int size) {
Note: Both are quadratic functions
of n
int k, m, temp;
for (k=0; k<size-1; k++) { ? The shape of the curve that
m = max_loc(x, k, size); expresses the running time as a
temp = x[k]; function of the problem size
x[k] = x[m]; stays the same.
x[m] = temp; We say that Selection Sort is of
} complexity Order n 2 or O(n2)
}

5 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Complexity classes
? The running time for different algorithms
fall into different complexity classes.
? Each complexity class is characterized by a
different family of curves.
? All curves in a given complexity class share
the same basic shape. (In the Asymptotic
sense)
? The O-notation is used for talking about
the complexity classes of algorithms.

6 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Order of Complexity, O- notation
? For the quadratic function
f(n) = an2 + bn + c
we will say that f(n) is O(n2).
? We focus on the dominant term, and ignore the lesser
terms; then throw away the coefficient. [Asymptotic
Analysis]
? Since constants are finally not important we may
assume that each machine instruction takes one unit of
time when we analyze the complexity of an algorithm.
? We may sometimes abstract this to unit time operators
like add, sub, multiply, etc and do an operator count.
? We can perform worst case or average case analysis.

7 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Centigrade to Fahrenheit
#include <stdio.h> T(n) = constant steps
main() = O(1)
{ (independent of input size)
float cent, fahr;
scanf(“%f”,&cent);
fahr = cent*(9.0/5.0) + 32;
printf( “%f C equals %f F\n”, cent, fahr);}

8 08-04-03 P.P.Chakrabarti, IIT Kharagpur


Max of n numbers …
#include <stdio.h>
main() Input size n consists of m+1 integers
{
int x, total, max; T(n) = k*(n) where k is some constant
scanf(“%d”,&total);
= O(n) /* Order n */
max=0;
while (total > 0)
{ scanf(“%d”,&x);
if (max < x) max = x;
total=total-1;
}
printf(“Largest is %d\n”,max);
}

9 08-04-03 P.P.Chakrabarti, IIT Kharagpur


How execution time is affected by various
complexity measures:
Assume speed S is 107 instructions per second.
size 10 20 30 50 100 1000 10000
C
O n .001 .002 .003 .005 .01 .1 ms 1 ms
ms ms ms ms ms
M
nlogn .003 .008 .015 .03 .07 1 ms 13 ms
P ms ms ms ms ms
L 2 .01 .04 .09 .25 1 ms 100 10 s
n
E ms ms ms ms ms
3 .1 .8 2.7 12.5 100 100 s 28 h
X n
ms ms ms ms ms
I
n .1 .1 s 100 s 3y 3x inf inf
T 2 ms 13
10 c
Y

10 08-04-03 P.P.Chakrabarti, IIT Kharagpur

You might also like