Download presentation
Presentation is loading. Please wait.
2
Chapter 2: Algorithm Analysis What is an algorithm? What to analyse? What we want to know? How accurate is required?
3
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
4
2.1:Example- MSSP Example: input -2, 11, -4, 13, -5, -2 the answer is 20 (A 2 through A 4 ) 4 possible solutions
5
2.1:MSSP: Solution1 Exhaustively tries all possibilities Running Time: O(N 3 ) Running Time: O(N 3 ) 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; }
6
2.1:MSSP: Solution 2 Eliminate the last for loop in solution 1 Running Time: O(N 2 ) Running Time: O(N 2 ) 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; }
7
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]; elsereturn (0); Center = (Left + right)/2; MaxLeftSum = MaxSubSum(A, Left, Center); MaxRightSum=MaxSubSum(A, Center +1, Right); MaxLeftBoardSum = 0; LeftBoardSum = 0;
8
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) Running Time: O(NlogN)
9
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) Running Time: O(N)
10
9 2.1:MSSP: Running Times (in second)
11
10 2.1:MSSP: Running Times (in second)
12
11 2.1:MSSP: Running Times (in second)
13
12 2.2: Mathematical Background 4 Definitions:
14
13 2.2: Mathematical Background Physical meaning of the definitions and objectives. Example:
15
14 2.2: Mathematical Background Typical Growth Rates
16
15 2.2: Mathematical Background Rule 1 Examples: if T 1 (N) = O(N 2 ) and T 2 (N)= O(N) then (a) T 1 (N) + T 2 (N) = O(N 2 ) (b) T 1 (N)*T 2 (N) = O(N 3 )
17
16 2.2: Mathematical Background Rule 2 Rule 3
18
17 2.2: Mathematical Background Using L’Hospital rule to evaluate:
19
18 2.2: Mathematical Background Example:
20
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++
21
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(N 2 )
22
21 2.3:Running Time Calculations Rule 4: Condition Statement if (condition) S1 else S2
23
22 2.3.A:Examples Sum=0 for (j=0;j<N;j++) Sum++; O(N)
24
23 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<N;k++) Sum++; O(N 2 )
25
24 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<N*N;k++) Sum++; O(N 3 )
26
25 2.3.A:Examples Sum=0 for (j=0;j<N;j++) for (k=0;k<j;k++) Sum++; O(N 2 )
27
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(N 5 )
28
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(N 4 )
29
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)
30
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 )
31
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)
32
31 2.4 Check Your Analysis Check with actual running time Compute T(N)/f(N) for a range of N
33
32 2.4 Check Your Analysis
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.