chapter 2 algorithm analysis. 2.1. mathematical background figure 2.1 typical growth rates
DESCRIPTION
2.3 What to Analyze Figure 2.2 Running times of several algorithms for maximum subsequence sum (in seconds)TRANSCRIPT
![Page 1: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/1.jpg)
Chapter 2Algorithm Analysis
![Page 2: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/2.jpg)
2.1. Mathematical Background
Figure 2.1 Typical growth rates
![Page 3: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/3.jpg)
2.3 What to Analyze
Figure 2.2 Running times of several algorithms for maximum subsequence sum (in seconds)
![Page 4: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/4.jpg)
2.3 What to Analyze
Figure 2.3 Plot (N vs. milliseconds) of various maximum subsequence sum algorithms
![Page 5: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/5.jpg)
2.3 What to Analyze
Figure 2.4 Plot (N vs. seconds) of various maximum subsequence sum algorithms
![Page 6: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/6.jpg)
2.4. Running Time Calculations
• 2.4.1 A Simple ExampleIntSum( int N ){
int i, PartialSum;
/* 1*/ PartialSum = 0;/* 2*/ for( i = 1; i <= N; i ++ )/* 3*/ PartialSum += i * i * i /* 4*/ return PartialSum;
}
![Page 7: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/7.jpg)
2.4.2 General Rules• RULE 1-FOR LOOPS• RULE 2-NESTED FOR LOOPS
for( i = 0; i < N; i++ )for( j = 0; j < N; j++ )k++;
• RULE 3-CONSECUTIVE STATEMENTSfor( i = 0; i < N; i++ )A[ i ] = 0;for( i = 0; i < N; i++ )for( j = 0; j < N; j++ )A[ i ] += A[ j ] + i + j;
• RULE 4-IF/ELSEif( Condition )S1elseS2
![Page 8: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/8.jpg)
2.4.3. Algorithm 1intMaxSubsequenceSum( const int A[ ], int N ){
int ThisSum, MaxSum, i, j, k;
/* 1*/ MaxSum = 0;/* 2*/ for( i = 0; i < N; i++ )/* 3*/ for( j = i; j < N; j++ )
{/* 4*/ ThisSum = 0;/* 5*/ for( k = i; k <= j; k++ )/* 6*/ ThisSum += A[ k ];/* 7*/ if( ThisSum > MaxSum )/* 8*/ MaxSum = ThisSum;
}/* 9*/ return MaxSum;
}
![Page 9: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/9.jpg)
2.4.3. Algorithm 2intMaxSubsequenceSum( const int A[ ], int N ){
int ThisSum, MaxSum, i, j;/* 1*/ MaxSum = 0;/* 2*/ for( i = 0; i < N; i++ )
{/* 3*/ ThisSum = 0;/* 4*/ for( j = i; j < N; j++ )
{/* 5*/ ThisSum += A[ j ];/* 6*/ if( ThisSum > MaxSum )/* 7*/ MaxSum = ThisSum;
}}
/* 8*/ return MaxSum;}
![Page 10: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/10.jpg)
2.4.3. Algorithm 3static intMaxSubSum( const int A[ ], int Left, int Right ){
int MaxLeftSum, MaxRightSum; int MaxLeftBorderSum, MaxRightBorderSum;int LeftBorderSum, RightBorderSum;int Center, i;
/* 1*/ if( Left == Right ) /* Base case *//* 2*/ if( A[ Left ] > 0 )/* 3*/ return A[ Left ];
else/* 4*/ return 0;
/* 5*/ Center = ( Left + Right ) / 2;/* 6*/ MaxLeftSum = MaxSubSum( A, Left, Center );/* 7*/ MaxRightSum = MaxSubSum( A, Center + 1, Right );
![Page 11: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/11.jpg)
2.4.3. Algorithm 3 cont./* 8*/ MaxLeftBorderSum = 0; LeftBorderSum = 0;/* 9*/ for( i = Center; i >= Left; i-- )
{/*10*/ LeftBorderSum += A[ i ];/*11*/ if( LeftBorderSum > MaxLeftBorderSum )/*12*/ MaxLeftBorderSum = LeftBorderSum;
}/*13*/ MaxRightBorderSum = 0; RightBorderSum = 0;/*14*/ for( i = Center + 1; i <= Right; i++ )
{/*15*/ RightBorderSum += A[ i ];/*16*/ if( RightBorderSum > MaxRightBorderSum )/*17*/ MaxRightBorderSum = RightBorderSum;
}/*18*/ return Max3( MaxLeftSum, MaxRightSum,/*19*/ MaxLeftBorderSum + MaxRightBorderSum );
} /*MaxSubSum*/
![Page 12: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/12.jpg)
2.4.3. Algorithm 3 cont.intMaxSubsequenceSum( const int A[ ], int N ){
return MaxSubSum( A, 0, N - 1 );}
![Page 13: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/13.jpg)
2.4.4. Algorithm 4intMaxSubsequenceSum( const int A[ ], int N){
int ThisSum, MaxSum, j;
/* 1*/ ThisSum = MaxSum = 0;/* 2*/ for( j = 0; j < N; j++ )
{/* 3*/ ThisSum += A[ j ];
/* 4*/ if( ThisSum > MaxSum )/* 5*/ MaxSum = ThisSum;/* 6*/ else if( ThisSum < 0 )/* 7*/ ThisSum = 0;
}/* 8*/ return MaxSum;
}
![Page 14: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/14.jpg)
2.4.4. Binary searchintBinarySearch( const ElementType A[ ], ElementType X, int N ){
int Low, Mid, High;
/* 1*/ Low = 0; High = N - 1;/* 2*/ while( Low <= High )
{/* 3*/ Mid = ( Low + High ) / 2;/* 4*/ if( A[ Mid ] < X )/* 5*/ Low = Mid + 1;
else/* 6*/ if( A[ Mid ] > X )/* 7*/ High = Mid - 1;
else/* 8*/ return Mid; /* Found */
}/* 9*/ return NotFound; /* NotFound is defined as -1 */
}
![Page 15: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/15.jpg)
2.4.4. Euclid’s algorithmunsigned intGcd( unsigned int M, unsigned int N ){
unsigned int Rem;
/* 1*/ while( N > 0 ){
/* 2*/ Rem = M % N;/* 3*/ M = N;/* 4*/ N = Rem;
}/* 5*/ return M;
}
![Page 16: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/16.jpg)
2.4.4. Efficient exponentiationlong intPow( long int X, unsigned int N ){
/* 1*/ if( N == 0 )/* 2*/ return 1;/* 3*/ if( N == 1 )/* 4*/ return X;/* 5*/ if( IsEven( N ) )/* 6*/ return Pow( X * X, N / 2 );
else/* 7*/ return Pow( X * X, N / 2 ) * X;
}
![Page 17: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/17.jpg)
2.4.6. Estimate the probability that two random numbers are relatively prime
Rel = 0; Tot = 0;for( i = 1; i <= N; i++ )
for( j = i + 1; j <= N; j++){
Tot++;if( Gcd( i, j) == 1 )
Rel++;}
printf( “Percentage of relatively prime pairs is %f\n”, ( double ) Rel / Tot );
![Page 18: Chapter 2 Algorithm Analysis. 2.1. Mathematical Background Figure 2.1 Typical growth rates](https://reader036.vdocuments.site/reader036/viewer/2022062302/5a4d1b877f8b9ab0599bd22b/html5/thumbnails/18.jpg)
2.4.6 Empirical running times for the previous routine