320 likes | 463 Views
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.
E N D
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 A1, A2, …, AN, find the maximum value of • Assumed that
2.1:Example- MSSP • Example: input -2, 11, -4, 13, -5, -2 • the answer is 20 (A2 through A4) • 4 possible solutions
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)
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)
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;
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)
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)
2.2: Mathematical Background • 4 Definitions:
2.2: Mathematical Background • Physical meaning of the definitions and objectives. • Example:
2.2: Mathematical Background Typical Growth Rates
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)
2.2: Mathematical Background • Rule 2 • Rule 3
2.2: Mathematical Background • Using L’Hospital rule to evaluate:
2.2: Mathematical Background • Example:
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++
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)
2.3:Running Time Calculations • Rule 4: Condition Statement if (condition) S1 else S2
2.3.A:Examples Sum=0 for (j=0;j<N;j++) Sum++; O(N)
2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<N;k++) Sum++; O(N2)
2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<N*N;k++) Sum++; O(N3)
2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<j;k++) Sum++; O(N2)
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)
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)
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)
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)
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)
2.4 Check Your Analysis • Check with actual running time • Compute T(N)/f(N) for a range of N