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 ? Memory space ? time complexity space complexity ? Observation : ? The larger the input data the more the resource requirement: Complexities are functions of the amount of input data (input size).Chakrabarti.P. 2 08-04-03 P. IIT Kharagpur .

4 72.3 3114.9 690.8 250 500 1000 2000 3 08-04-03 49.5 P.8 780.Selection Sort on two computers Array Size Computer 1 Computer 2 n ms ms 125 12.3 195.9 11.5 2. IIT Kharagpur .P.Chakrabarti.0 43.

(Also we are interested at asymptotic behaviour. ?Algorithms usually consume resources in some fashion that depends on the size of the problem they solve.Chakrabarti.P. n. ?Need a machine independent complexity measure that is a function of input size. when n becomes large. 4 08-04-03 P. that is.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. IIT Kharagpur . different programming languages. different compilers.

00305 n + 0.Analyzing SelSort int max_loc(int x[]. k<size-1. int k. temp. k++) { m = max_loc(x.00040 n + 0. k. IIT Kharagpur 5 . temp = x[k]. pos. j++) if (x[j] > x[pos]) pos = j. for (j=k+1. } int selsort (int x[].001 ? Computer 2: f2(n) = 0. for (k=0. x[m] = temp.Chakrabarti. } } ? Computer 1: f1(n) = 0. int size) { int j.100 Note: Both are quadratic functions of n ? The shape of the curve that expresses the running time as a function of the problem size stays the same. We say that Selection Sort is of complexity Order n 2 or O(n2) 08-04-03 P. size).0001724 n2 + 0. int size) { int k. x[k] = x[m]. pos = k. m. return pos.0007772 n2 + 0.P. j<size.

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

Chakrabarti.P. etc and do an operator count. sub. O. We can perform worst case or average case analysis.notation ? For the quadratic function f(n) = an2 + bn + c we will say that f(n) is O(n2). IIT Kharagpur . then throw away the coefficient. multiply. We may sometimes abstract this to unit time operators like add. and ignore the lesser terms. ? ? ? ? We focus on the dominant term. 7 08-04-03 P.Order of Complexity. [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.

0) + 32. fahr = cent*(9. cent.} 8 08-04-03 P. fahr). printf( “%f C equals %f F\n”.&cent).0/5.Chakrabarti. scanf(“%f”.Centigrade to Fahrenheit #include <stdio. IIT Kharagpur . fahr.h> T(n) = constant steps main() = O(1) (independent of input size) { float cent.P.

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

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