1 / 18

Sum of a Linear Array

Sum of a Linear Array. Given an array a = ( a i : i = 0, 1, …, n – 1 ), compute the sum of its elements: s = a 0 + a 1 +…+ a n – 1 Elementary operations: fetching a i from memory: n operations per array addition: n – 1 operations per array.

xaria
Download Presentation

Sum of a Linear Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sum of a Linear Array • Given an array a = ( ai: i = 0, 1, …, n – 1 ), compute the sum of its elements: s = a0 + a1+…+ an – 1 • Elementary operations: • fetching ai from memory: n operations per array • addition: n– 1 operations per array COMPSCI.220.S1.T - 2004

  2. Sum of a Linear Array • Linear computing time: for n>>1 T(n) = c  n • Factor c depends on the computer, operating system, programming language, compiler, etc • Linear relative changes: T(a n) = a T(n) COMPSCI.220.S1.T - 2004

  3. Sums of Contiguous Subsequences • Given an array (ai: i = 0,1, …, n – 1) of size n =2m, compute m + 1 sums: sk = ak + ak+1 + …+ a k+m–1; k = 0, …, m of all contiguous subsequences of size m • Brute force computation: c m operations per subsequence; in total: c m (m + 1) operations COMPSCI.220.S1.T - 2004

  4. Sums of Contiguous Subsequences • Brute-force program (two nested loops):...int[ ] s = new int[ m + 1 ];for( int k = 0; k <= m; k++ ) { // m+ 1 sums s[ k ] = 0; for( int i = 0; i < m; i++ ) // each sum: c  m s[ k ] += a[ k + i ]; // operations } COMPSCI.220.S1.T - 2004

  5. Brute-force Quadratic Approach • Quadratic time (two nested loops): • Quadratic relative changes: COMPSCI.220.S1.T - 2004

  6. Brute-force Quadratic Approach • Quadratic time complexity is due to reiterated computations in the innermost loop COMPSCI.220.S1.T - 2004

  7. Getting Linear Computing Time • Exclude reiterated computations of the “brute-force” algorithm: • As a result, the linear computing time: COMPSCI.220.S1.T - 2004

  8. Getting Linear Computing Time • Better program (two successive loops): ...int[ ] s = new int[ m + 1 ];s[ 0 ] = 0;for( int i = 0; i <m; i++ ) // first sum: moperations s[ 0 ] += a[ i ];for( int k = 1; k <= m; k++ ) // 2 operations per sum s[ k ] = s[ k – 1 ] + a[ k + m – 1] - a[ k – 1 ]; … COMPSCI.220.S1.T - 2004

  9. Computing Time for T(1)=1ms COMPSCI.220.S1.T - 2004

  10. Practical Usage: Data Averaging Data smoothing in a moving 1D window: COMPSCI.220.S1.T - 2004

  11. Data Averaging Window size: m { 2, 3, …, n/2 } Partial sums: Sk = a0 + ... + ak Brute-force approach: T(n) c · m · nC ·n2 if m is a fraction of n Brute-force computing time might be even cubic: T(n) C·n3 when the averaged data should be compared for many windows 2  mn/2 COMPSCI.220.S1.T - 2004

  12. Faster Data Averaging • Sequential modification of the current sum: T(n)  m + 2·(n - m) per window (m n/2) • An extra memory for storing n partial sums Sk = a0+a1+…+ak; k = 0, 1,…, n-1 allows to find the sum of each subsequence with only 1 subtraction per window: sk+1 = Sk+m - Sk COMPSCI.220.S1.T - 2004

  13. 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Tbody of a loop where k is number of times the loop is executed COMPSCI.220.S1.T - 2004

  14. Estimating Running Time • Conditional / switch statements are more complicated (because it is necessary toaccount for frequencies of branches) • Function calls: Tfunction= STstatements in function • Function composition: T(f(g(n))) = T(g(n))+T(f(n)) COMPSCI.220.S1.T - 2004

  15. Estimating Running Time • Function calls: T = S Tstatement i ... x.myMethod( y, ... ); public void myMethod( int a, ... ){ statements 1, 2, … , N } • Function composition T(f(g(n))) Computation of x = g(n)  T(g(n)) Computation of y = f(x)  T(f(n)) COMPSCI.220.S1.T - 2004

  16. Time to Sum Contiguous Sequences • Ignore data initialisation code • “Brute-force” nested loops: T(n) = m · (m +1) = n/2· ( n/2 + 1 ) = 0.25 n2 + 0.5 n • For a large n, T(n)  0.25 n2 • e.g., if n > 500, 0.5 n < 0.4% of T(n) COMPSCI.220.S1.T - 2004

  17. Time to Sum Contiguous Sequences • Factor c = 0.25 is referred to as a “constant of proportionality” • This factor does not effect the behaviour of the algorithm for a large n: • 10% increase in n leads to a 20% increase in T(n) COMPSCI.220.S1.T - 2004

  18. Quadratic vs linear time COMPSCI.220.S1.T - 2004

More Related