VIEWS: 0 PAGES: 17 POSTED ON: 12/12/2011 Public Domain
Evaluating Running Time of Algorithm DS / Running-Time 1 Principles of Running Time Analysis • Measure time as a function of the input size • Evaluate the worst-case performance for all inputs up to a given size • Ignore constant factor • Compare the functions that measure the time complexity of algorithms by their growth rate: – big-O for estimated upper bounds – big- W for estimated lower bounds DS / Running-Time 2 Running Time of Simple Statements • Simple Statement T(n) = O(1) – arithmetic (+, *,…) operations – logic operations (&&, ….) – comparisons (<=, …) – assignment (a := b) with no function call – read statement – break, continue, return (simple control) DS / Running-Time 3 Running Time of Loops for-loop initialize O(1) test O(1) g(n) times body O(f(n)) establishing g(n) is usually simple. O (g (n) * f (n)) reinitialize O(1) DS / Running-Time 4 While Loops while test O(1) g(n) times Need to establish a bound on the number of iterations of the loop g(n). body O(f(n)) Might need an inductive proof for g(n). O(g(n) * f(n)) DS / Running-Time 5 While Loop - example Searching an element with given value within an array of size n: i = 0; (1) while ( x != a[i]) (2) i++; (3) (1) O(1) test in (2) O(1) (3) O(1) iterations maximum n O(while-loop) = O(1) + n * O(1) = O(n) DS / Running-Time 6 Nested Loops initialize O (1) test O (1) O (g (n) * f (n)) g(n) times inner-loop O (f (n)) reinitialize O (1) DS / Running-Time 7 Nested Loop - example Compute inside out for i := 1 to N for j := 1 to N T(n) = O (n2) i=i+j DS / Running-Time 8 Nested Loop - example selectionSortI(int a[], int n) for (j = 2, n, j++) key = A[j] k := j-1 while (k > 0 and A[k]> key A[k+1]:= A[k] k := k-1 A[k+1] := key T(n) = ? DS / Running-Time 9 Running Time of if-statement test O (max (f (n), g (n))) if-part else-part O(f(n)) O(g(n)) DS / Running-Time 10 If Statement - example if ( a[1][i] == 0) for (i = 0, i < n, i++) for (j = 0, j < n, j++) a[i][j] = 0; else for ( = 0, i<n, i++) a[i][i] := 1; if: T(n) = O(n2) else : T(n) = O(n) T(n) = max (O(n2), O(n)) = O (n2) DS / Running-Time 11 Running Time of Consecutive Statements O(f1(n)) O (f1 (n) + f2 (n) + … + f4 (n)) O(f2(n)) O (max (fi (n)), i = 1, …., 4 O(f3(n)) O(f4(n)) DS / Running-Time 12 Consecutive Statements - example for i := 1 to n a[1]: = 0 for i := 1 to N for j := 1 to N a[i] := a[i] + a[j] + i + j T(n) = O ( max (f (first-loop), f (second-loops) = O ( max (f (n), f (n2)) = O (f (n2)) DS / Running-Time 13 Recursive Rule for Bounding Running Time • Recursive definition of a statement • a simple statement (is a statement) • Let S, S1 and S2 be statements, then the following are statements: – While (Cond) S – for( E1, E2, E3) S – If (Cond) S1 else S2 – If (Cond) S – S1; S2; ... DS / Running-Time 14 Example: Insertion Sort InsertionSortI(int a[], int n) for (j = 2, n, j++) 1 key = A[j] 2 k := j-1 3 while (k > 0 and A[k]> key A[k+1]:= A[k] 4 k := k-1 5 A[k+1] := key 6 DS / Running-Time 15 Insertion sort - tree structure Evaluate running time bottom-up for 2 3 while 6 4 leaves = simple statements internal nodes = compound statements 5 DS / Running-Time 16 Programs with Function Call • Non Recursive function calls – Evaluated like compound statements. Bottom up. • Recursive function calls – Running time represented by a recursive function – Requires techniques for solving recursive functions. DS / Running-Time 17