Evaluating Running Time of Algorithm - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Evaluating Running Time of Algorithm

Description:

Measure time as a function of the input size ... big-O for estimated upper bounds. big- W for estimated lower bounds. DS / Running-Time ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 18
Provided by: yon5
Category:

less

Transcript and Presenter's Notes

Title: Evaluating Running Time of Algorithm


1
Evaluating Running Time of Algorithm
2
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 ofalgorithms by their growth rate
  • big-O for estimated upper bounds
  • big- W for estimated lower bounds

3
Running 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)
4
Running 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)
5
While 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))
6
While 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)
7
Nested Loops
O (1)
initialize
test
O (1)
O (g (n) f (n))
g(n) times
inner-loop
O (f (n))
O (1)
reinitialize
8
Nested Loop - example
Compute inside out
for i 1 to N for j 1 to N i i j
T(n) O (n2)
9
Nested 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) ?
10
Running Time of if-statement
test
O (max (f (n), g (n)))
if-part
else-part
O(f(n))
O(g(n))
11
If 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)
12
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))
13
Consecutive 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))
14
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 ...

15
Example 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
16
Insertion sort - tree structure
Evaluate running time bottom-up
for
while
3
6
2
4
leaves simple statements internal nodes
compound statements
5
17
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.
Write a Comment
User Comments (0)
About PowerShow.com