Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2COMPSCI 220 - AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n) 

Similar presentations


Presentation on theme: "Lecture 2COMPSCI 220 - AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n) "— Presentation transcript:

1 Lecture 2COMPSCI 220 - AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n)  m(m +1)  n / 2 ( n / 2 + 1 )  0.25n 2 + 0.5n For a large n, T(n)  0.25n 2 –e.g., if n ≥10, the linear term 0.5n ≤ 16.7% of T(n) –if n ≥ 500, the linear term 0.5n ≤ 0.4% of T(n)

2 Lecture 2COMPSCI 220 - AP G Gimel'farb2 Quadratic vs linear term T(n) = 0.25n 2 + 0.5n nT(n)T(n)0.25n 2 0.5n 103025516.7% 50650625253.8% 10025502500502.0% 50062750625002500.4% 10002505002500005000.2%

3 Lecture 2COMPSCI 220 - AP G Gimel'farb3 Quadratic Time to Sum Subarrays: T(n)=0.25n 2 + 0.5n Factor c = 0.25 is referred to as a “ constant of proportionality ” An actual value of the factor does not affect the behaviour of the algorithm for a large n: –10% increase in n  20% increase in T(n) –Double value of n  4-fold increase in T(n): T(2n) = 4 T(n)

4 Lecture 2COMPSCI 220 - AP G Gimel'farb4 Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size becomes large, the most significant term is that which has the largest power of n This term increases faster than other terms which reduce in significance

5 Lecture 2COMPSCI 220 - AP G Gimel'farb5 Running Time: Estimation Rules Constants of proportionality depend on the compiler, language, computer, etc. –It is useful to ignore the constants when analysing algorithms. Constants of proportionality are reduced by using faster hardware or minimising time spent on the “inner loop” – But this would not affect behaviour of an algorithm for a large problem!

6 Lecture 2COMPSCI 220 - AP G Gimel'farb6 Elementary Operations Basic arithmetic operations (  ; – ;  ;  ; % ) Basic relational operators ( ==, !=, >, =, <= ) Basic Boolean operations (AND,OR,NOT) Branch operations, return, … Input for problem domains (meaning of n ): Sorting: n items Graph / path: n vertices / edges Image processing: n pixels Text processing: string length

7 Lecture 2COMPSCI 220 - AP G Gimel'farb7 Estimating Running Time Simplifying assumptions: all elementary statements / expressions take the same amount of time to execute e.g., simple arithmetic assignments return Loops increase in time linearly as k  T body of a loop where k is number of times the loop is executed

8 Lecture 2COMPSCI 220 - AP G Gimel'farb8 Estimating Running Time Conditional / switch statements like if { condition } then { const time T 1 } else { const time T 2 } are more complicated ( one has to account for branching frequencies: T  f true T 1  (1  f true )T 2 ≤ max{ T 1, T 2 } Function calls : T function =  T statements in function Function composition : T(f(g(n))) = T(g(n))  T(f(n))

9 Lecture 2COMPSCI 220 - AP G Gimel'farb9 Estimating Running Time Function calls in more detail: T   T statemen t i... x.myMethod( 5,... ); public void myMethod( int a,... ){ statements 1, 2, …, N } Function composition in more detail: T(f(g(n))) Computation of x  g(n)  T(g(n)) Computation of y  f(x)  T(f(n))

10 Lecture 2COMPSCI 220 - AP G Gimel'farb10 Example 1.5: Textbook, p.11 Logarithmic time due to an exponential change i  k, k 2, k 3, …, k m of the loop control in the range 1 ≤ i ≤ n : for i = k step i  ik until n do … {const # of elementary operations} end for m iterations such that k m  1 < n ≤ k m  T(n)  c  log k n 

11 Lecture 2COMPSCI 220 - AP G Gimel'farb11 Example 1.6: Textbook, p.11 n log n running time of the conditional nested loops: m  2 ; for j  1 to n do if ( j  m ) then m  2m for i  1 to n do …{const # of operations} end for end if end for The inner loop is executed k times for j  2, 4, …, 2 k ; k < log 2 n ≤ k  1 ; in total: T(n)  kn  n  log 2 n 

12 Lecture 2COMPSCI 220 - AP G Gimel'farb12 Exercise 1.2.1: Textbook, p.12 Conditional nested loops: linear or quadratic running time? m  1 ; for j  1 to n do if ( j  m ) then m  m (n  1) for i  1 to n do …{const # of operations} end for end if end for The inner loop is executed only twice, for j  1 and j  n  1 ; in total: T(n)  n  linear running time


Download ppt "Lecture 2COMPSCI 220 - AP G Gimel'farb1 Estimated Time to Sum Subarrays Ignore data initialisation “Brute-force” summing with two nested loops: T(n) "

Similar presentations


Ads by Google