1 / 32

Chapter 2: Algorithm Analysis

Chapter 2: Algorithm Analysis. What is an algorithm? What to analyse? What we want to know? How accurate is required?. 2.1: Example: Max Subsequence Sum Problem(MSSP). Given N (possibly negative) integers A 1 , A 2 , …, A N , find the maximum value of. Assumed that. 2.1:Example- MSSP.

adina
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. Chapter 2: Algorithm Analysis • What is an algorithm? • What to analyse? • What we want to know? • How accurate is required?

  2. 2.1: Example: Max Subsequence Sum Problem(MSSP) • Given N (possibly negative) integers A1, A2, …, AN, find the maximum value of • Assumed that

  3. 2.1:Example- MSSP • Example: input -2, 11, -4, 13, -5, -2 • the answer is 20 (A2 through A4) • 4 possible solutions

  4. 2.1:MSSP: Solution1 • Exhaustively tries all possibilities • int MaxSubSumSol1(const int A[], int N) { int ThisSum, MaxSum, i, j, k; MaxSum = 0; for (i=0; i<N; i++) for ( j=i; j<N; j++) { ThisSum = 0; for (k=i; k<j; k++) ThisSum += A[k]; if (ThisSum > MaxSum) MaxSum = ThisSum; } return MaxSum; } Running Time: O(N3)

  5. 2.1:MSSP: Solution 2 • Eliminate the last for loop in solution 1 int MaxSubSumSol2(const int A[], int N) { int ThisSum, MaxSum, i, j, k; MaxSum = 0; for (i=0; i<N; i++) { ThisSum = 0; for ( j=i; j<N; j++) { ThisSum += A[j]; if (ThisSum > MaxSum) MaxSum = ThisSum; } return MaxSum; } Running Time: O(N2)

  6. 2.1:MSSP: Solution 3 • “divide-and-conquer” strategy static int MaxSubSum(const int A[], int Left, int Right) { int MaxLeftSum, MaxRightSum; int MaxLeftBoarderSum, MaxRightBoardSum; int LeftBoarderSum, RightBoardSum; int center, i; if ( Left == Right) /*Base Case*/ if (A[Left] > 0) return A[Left]; else return (0); Center = (Left + right)/2; MaxLeftSum = MaxSubSum(A, Left, Center); MaxRightSum=MaxSubSum(A, Center +1, Right); MaxLeftBoardSum = 0; LeftBoardSum = 0;

  7. 2.1:MSSP: Solution 3 for (i= Center; i>= Left; i--) { LeftBoarderSum += A[i]; if (LeftBoarderSum > MaxLeftBoarderSum) MaxLeftBoarderSum = LeftBoarderSum; } for (i= Center+1; i>= Right; i++) { RightBoarderSum += A[i]; if (RightBoarderSum>MaxRightBoarderSum) MaxRightBoarderSum = RightBoarderSum; } return Max3(MaxLeftSum, MaxRightSum, MaxLeftBoarderSum+MaxLeftBoarderSum) } int MaxSubSumSol3(const int A[], int N) { return (MaxSubSum(A, 0, N-1); } Running Time: O(NlogN)

  8. 2.1:MSSP: Solution 4 • int MaxSubSumSol4(const int A[], int N) { int ThisSum, MaxSum, j; ThisSum = MaxSum = 0; for ( j=0; j<N; j++) { ThisSum += A[j]; if (ThisSum > MaxSum) MaxSum = ThisSum; else if (ThisSum < 0) ThisSum = 0; } return MaxSum; } Running Time: O(N)

  9. 2.1:MSSP: Running Times (in second)

  10. 2.1:MSSP: Running Times (in second)

  11. 2.1:MSSP: Running Times (in second)

  12. 2.2: Mathematical Background • 4 Definitions:

  13. 2.2: Mathematical Background • Physical meaning of the definitions and objectives. • Example:

  14. 2.2: Mathematical Background Typical Growth Rates

  15. 2.2: Mathematical Background • Rule 1 • Examples: if T1(N) = O(N2) and T2(N)= O(N) then (a) T1(N) + T2(N) = O(N2) (b) T1(N)*T2(N) = O(N3)

  16. 2.2: Mathematical Background • Rule 2 • Rule 3

  17. 2.2: Mathematical Background • Using L’Hospital rule to evaluate:

  18. 2.2: Mathematical Background • Example:

  19. 2.3:Running Time Calculations • Ways to estimate the running time • Rule 1: for loops for (i=0;i<N;i++) k++ • Rule 2: Nested for loops for (i=0; i<N; i++) for (j=0; j<N; j++) k++

  20. 2.3:Running Time Calculations • Rule 3: Consecutive Statements for (i=0;i<N;i++) k++ for (i=0; i<N; i++) for (j=0; j<N; j++) k++ O(N) O(N2)

  21. 2.3:Running Time Calculations • Rule 4: Condition Statement if (condition) S1 else S2

  22. 2.3.A:Examples Sum=0 for (j=0;j<N;j++) Sum++; O(N)

  23. 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<N;k++) Sum++; O(N2)

  24. 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<N*N;k++) Sum++; O(N3)

  25. 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<j;k++) Sum++; O(N2)

  26. 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<j*j;k++) for (m=0; m<k; m++) Sum++; O(N5)

  27. 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<j*j;k++) if (k%j == 0) for (m=0; m<k; m++) Sum++; O(N4)

  28. 2.3:Running Time Calculations • Analysis of factorial int fact(int n) { if (n<=1) return 1; else return (n*fact(n-1)); } O(N)

  29. 2.3:Running Time Calculations • Analysis of Fibonacci number int Fib (int N) { if (N<=1) return 1; else return ( Fib(N-1) + Fib(N-2) ); } O((3/2)N)

  30. 2.3:Running Time Calculations • Analysis of Binary Search while (low <= high) { mid = (low + high) / 2; if (x == a [mid]) return (mid); else if (x < a [mid]) high = mid - 1; else low = mid + 1; } O(logN)

  31. 2.4 Check Your Analysis • Check with actual running time • Compute T(N)/f(N) for a range of N

  32. 2.4 Check Your Analysis

More Related