Lecture 3
Lecture 3
Analysis and
Asymptotic Bounds
Algorithm Analysis
Useful for:
Comparing algorithms
Evaluating the variations of execution time with
regard to the input data
Some complexity
analysis techniques
1. Operation counts
2. Step counts
3. Counting cache misses
4. Asymptotic complexity
5. Amortized complexity
6. Practical complexity
Best-case / fastest
Symbol Ω. Example; Ω(n2)
Average-case
Symbol θ. Example; θ(logn)
Worst-case/ slowest
Symbol O. Example; O(n)
Typical growth rates
Function Name
C constant
logN logarithmic
log2N Log-squared
N Linear
N logN
N2 Quadratic
N3 Cubic
2N Exponential
Normally these formulas
are very handy
Continue….
Continue…
Examples step counts
1. sum = 0;
2. for( i = 0; i < n; ++i )
3. for( j = 0; j < n * n; ++j ){
4. ++sum;
5. x=+1}
1. sum = 0;
2. for( i = 0; i < n; ++i )
3. for( j = 0; j < i; ++j )
4. ++sum;
1. sum = 0;
2. for( i = 0; i < n; ++i )
3. for( j = 0; j < i * i; ++j )
4. for( k = 0; k < j; ++k )
5. ++sum;
Example 6
1. Int sum=0;
2. for (int j = 1; j < n; j = j * 2)
3. ++sum;
4. Int sum=0;
5. for (int j = 1; j < n; j = j * k)
6. ++sum;
Example 7
1. Int sum=0;
2. for (int j = 1; j < n; j = j + 2)
3. ++sum;
4. Int sum=0;
5. for (int j = n; j < 0; j = j - 2)
6. ++sum;
Example 8
1. sum = 0;
2. for( i = 0; i < n; ++i ){
3. for( j = 0; j < i * 2; ++j ){
4. k=j
5. while (k>=1)
6. k=k-1;
7. }}