Title: Evaluating Running Time of Algorithm
1Evaluating Running Time of Algorithm
2Principles 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 ofalgorithms by their growth rate - big-O for estimated upper bounds
- big- W for estimated lower bounds
3Running Time of Simple Statements
- Simple Statement
- arithmetic (, ,) operations
- logic operations (, .)
- comparisons (lt, )
- assignment (a b) with no function call
- read statement
- break, continue, return (simple control)
T(n) O(1)
4Running Time of Loops
for-loop
initialize
O(1)
test
O(1)
g(n) times
O(f(n))
body
establishing g(n) is usually simple.
O (g (n) f (n))
reinitialize
O(1)
5While Loops
while
O(1)
test
Need to establish a bound on the number of
iterations of the loop g(n). Might need an
inductive proof for g(n).
g(n) times
body
O(f(n))
O(g(n) f(n))
6While Loop - example
Searching an element with given value within an
array of size n i 0 (1) while ( x !
ai) (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)
7Nested Loops
O (1)
initialize
test
O (1)
O (g (n) f (n))
g(n) times
inner-loop
O (f (n))
O (1)
reinitialize
8Nested Loop - example
Compute inside out
for i 1 to N for j 1 to N i i j
T(n) O (n2)
9Nested Loop - example
selectionSortI(int a, int n)
for (j 2, n, j) key Aj
k j-1 while (k gt 0 and Akgt key
Ak1 Ak k k-1
Ak1 key
T(n) ?
10Running Time of if-statement
test
O (max (f (n), g (n)))
if-part
else-part
O(f(n))
O(g(n))
11If Statement - example
if ( a1i 0) for (i 0, i lt n,
i) for (j 0, j lt n, j)
aij 0 else for ( 0, iltn,
i) aii 1
if T(n) O(n2) else T(n)
O(n) T(n) max (O(n2), O(n)) O (n2)
12Running 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))
13Consecutive Statements - example
for i 1 to n a1 0 for i 1 to N for
j 1 to N ai ai aj i j
T(n) O ( max (f (first-loop), f
(second-loops) O ( max (f (n), f
(n2)) O (f (n2))
14Recursive 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 ...
15Example Insertion Sort
InsertionSortI(int a, int n)
for (j 2, n, j) 1 key Aj
2 k j-1 3
while (k gt 0 and Akgt key
Ak1 Ak 4 k k-1
5 Ak1 key 6
16Insertion sort - tree structure
Evaluate running time bottom-up
for
while
3
6
2
4
leaves simple statements internal nodes
compound statements
5
17Programs 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.