P. P. Chakrabarti
10-04-03
Analysis of Algorithms
? ?
Observation :
?
The larger the input data the more the resource requirement: Complexities are functions of the amount of input data (input size).
Refer to first few chapters of the book Introduction To Algorithms by Cormen, Leiserson, Rivest and Stein
10-04-03
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.
3
10-04-03 P.P.Chakrabarti, IIT Kharagpur
Analyzing SelSort
int max_loc(int x[], int k, int size) { int j, pos; pos = k; for (j=k+1; j<size; j++) if (x[j] > x[pos]) pos = j; return pos; } int selsort (int x[], int size) { int k, m, temp; for (k=0; k<size-1; k++) { m = max_loc(x, k, size); temp = x[k]; x[k] = x[m]; x[m] = temp; } }
?
Computer 1: f1(n) = 0.0007772 n2 + 0.00305 n + 0.001 ? Computer 2: f2(n) = 0.0001724 n2 + 0.00040 n + 0.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.
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.
10-04-03 P.P.Chakrabarti, IIT Kharagpur
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.
10-04-03
C O M P L E X I T Y
10-04-03
speed
S
N1 = 3.6x10 N2 =
10 9
100 S 1000 S
100 N1 85 N2 10 N3 N4+7 1000 N1 750 N2 30 N3 N4+10
P.P.Chakrabarti, IIT Kharagpur
n n log n n 2
8
2
1.2x10 N3 = 5 2x10 N4 = 35
10-04-03
Formal Definition
?
T(N) = O(f(N)) if there are positive constants c and n 0 such that T(N) ? c f(N) when N ? n0. Meaning : As N increases, T(N) grows no faster than f(N). The function T is eventually bounded by some multiple of f(N). f(N) gives an upper bound in the behavior of T(N).
? ?
Linear Search in an array: ? Binary Search: ? Selection Sort: ? Mergesort: ? Quicksort: ? Stack ADT: Push and Pop are ? Queue using a singly linked list: ?? (What about a doubly linked list??) Multiplying 2 square matrices:? The primes that we wrote in class:?
10-04-03 P.P.Chakrabarti, IIT Kharagpur
10
T(n) = 2T(n/2) + cn ? Solves to O(n log n) ? T(n) = 2(2T(n/4) + cn/2) + cn = 4T(n/4) + 2cn = 2kT(n/2k) + kcn [Note that T(2) = 1] = O(nk) = O(n log n)
?
11
10-04-03
12
10-04-03
Primes:
?
? ?
For an integer of value k we loop at most sqrt(k) times. So is the complexity O(sqrt(n))? Remember that the input for value n is in log2 k bits. So the input size is n = log2 n The complexity in terms of input size for this algorithm is therefore O(2n) The latest proof of Dr Manindra Agarwal et al proves that prime detection can be done in O(nk) time for some constant k<5
10-04-03 P.P.Chakrabarti, IIT Kharagpur
13
More definitions
?
T(N) = ? (g(N)) if there are positive constants c and n0 such that T(N) ? c f(N) when N ? n0. Meaning : As N increases, T(N) grows no slower than g(N) ; T(N) grows at least as fast as g(N). T(N) = ?(h(N)) if and only if T(N) = O (h(N)) and T(N) = ? (h(N)) Meaning : As N increases, T(N) grows as fast as h(N). T(N) = o(p(N)) if T(N) = O(p(N)) and T(N) ? ?(p(N)) Meaning : As N increases, T(N) grows slower than p(N). lim n? ? T(N)/p(N) = 0.
Space Complexity
?
Measures the work space or space required in addition to input storage ? Selection Sort: O(1) ? Binary Search: ? Recursive algorithm : O(log n) {Recursion Stack} ? Iterative algorithm: O(1) ? Mergesort: O(n) {Why: Additional array. Here recursion stack is of depth log n} ? Quicksort: O(n) {Why: recursion stack in the worst case}
15
10-04-03