1 / 18

Chapter 2: Algorithm Analysis

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. Chapter 2: Algorithm Analysis. Application of Big-Oh to program analysis Running Time Calculations. Lydia Sinapova, Simpson College. Background. The work done by an algorithm, i.e. its complexity , is determined by the

tessa
Download Presentation

Chapter 2: Algorithm Analysis

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. Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Running Time Calculations Lydia Sinapova, Simpson College

  2. Background The work done by an algorithm, i.e. its complexity, is determined by the number of the basic operations necessary to solve the problem.

  3. The Task Determine how the number of operations depend on the size of input : N - size of input F(N) - number of operations

  4. Basic operations in an algorithm Problem:Find x in an array Operation:Comparison of x with an entry in the array Size of input:The number of the elements in the array

  5. Basic operations …. Problem:Multiplying two matrices with real entries Operation: Multiplication of two real numbers Size of input: The dimensions of the matrices

  6. Basic operations …. Problem:Sort an array of numbers Operation:Comparison of two array entries plus moving elements in the array Size of input:The number of elements in the array

  7. Counting the number of operations A.for loops O(n) The running time of a forloop is at most the running time of the statements inside the looptimesthe number of iterations.

  8. for loops • sum = 0;for( i = 0; i < n; i++ ) • sum = sum + i; • The running time is O(n)

  9. Counting the number of operations B. Nested loops The total running time is the running time of the inside statementstimes the product of the sizes of all the loops

  10. Nested loops • sum = 0;for( i = 0; i < n; i++) • for( j = 0; j < n; j++) • sum++; • The running time is O(n2)

  11. Counting the number of operations C. Consecutive program fragments Total running time : the maximum of the running time of the individual fragments

  12. Consecutive program fragments • sum = 0; O(n) • for( i = 0; i < n; i++) • sum = sum + i; • sum = 0; O(n2)for( i = 0; i < n; i++) • for( j = 0; j < 2n; j++) sum++; • The maximum is O(n2)

  13. Counting the number of operations D: If statement if C S1; else S2; The running time is the maximum of the running times of S1 and S2.

  14. EXAMPLES • O(n3): • sum = 0;for( i = 0 ; i < n; i++) • for( j = 0 ; j < n*n ; j++ ) • sum++;

  15. EXAMPLES • O(n2): • sum = 0;for( i = 0; i < n ; i++) • for( j = 0; j < i ; j++) • sum++;

  16. EXAMPLES • O(n3logn) • for(j = 0; j < n*n; j++) • compute_val(j); • The complexity of compute_val(x)is given to be O(n*logn)

  17. Search in an unordered array of elements for (i = 0; i < n; i++) if (a[ i ] == x) return 1; return -1; O(n)

  18. Search in a table n x m for (i = 0; i < n; i++) for (j = 0; j < m; j++)   if (a[ i ][ j ] == x) return 1 ; return -1; O(n*m)

More Related