Fundamentals of Algorithm
Efficiency Analysis
Chapter 2
Some properties of asymptotic order of growth
f(n) O(f(n))
f(n) O(g(n)) iff g(n) (f(n))
If f (n) O(g (n)) and g(n) O(h(n)) , then f(n) O(h(n))
If f1(n) O(g1(n)) and f2(n) O(g2(n)) , then
f1(n) + f2(n) O(max{g1(n), g2(n)})
2
If f1(n) O(g1(n)) and f2(n) O(g2(n)) , then
f1(n) + f2(n) O(max{g1(n), g2(n)})
3
Establishing order of growth using limits
0 order of growth of T(n) < order of growth of g(n)
lim T(n)/g(n) = c > 0 order of growth of T(n) = order of growth of g(n)
n→∞
∞ order of growth of T(n) > order of growth of g(n)
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
4
L’Hôpital’s rule and Stirling’s formula
L’Hôpital’s rule: If limn f(n) = limn g(n) = and
the derivatives f´, g´ exist, then
lim f(n) lim f ´(n)
=
n g(n) n g ´(n)
Example: log n vs. √n
Stirling’s formula: n! (2n)1/2 (n/e)n
Example: 2n vs. n!
5
Orders of growth of some important functions
All logarithmic functions loga n belong to the same class
(log n) no matter what the logarithm’s base a > 1 is
All polynomials of the same degree k belong to the same class:
aknk + ak-1nk-1 + … + a0 (nk)
Exponential functions an have different orders of growth for
different a’s
order log n < order n (>0) < order an < order n! < order nn
6
Basic asymptotic efficiency classes
1 constant
log n logarithmic
n linear
n log n n-log-n or linearithmic
n2 quadratic
n3 cubic
2n exponential
n! factorial
7
Time efficiency of nonrecursive algorithms
General Plan for Analysis
Decide on parameter n indicating input size
Identify algorithm’s basic operation
Determine worst, average, and best cases for input of size n
Set up a sum for the number of times the basic operation is
executed
Simplify the sum using standard formulas and rules (see
Appendix A)
8
Useful summation formulas and rules
liu1 = 1+1+ ⋯ +1 = u - l + 1
In particular, liu1 = n - 1 + 1 = n (n)
1in i = 1+2+ ⋯ +n = n(n+1)/2 n2/2 (n2)
1in i2 = 12+22+ ⋯ +n2 = n(n+1)(2n+1)/6 n3/3 (n3)
0in ai = 1 + a + ⋯ + an = (an+1 - 1)/(a - 1) for any a 1
In particular, 0in 2i = 20 + 21 + ⋯ + 2n = 2n+1 - 1 (2n )
(ai ± bi ) = ai ± bi cai = cai liuai = limai + m+1iuai
9
Example 1: Maximum element
10
Example 2: Element uniqueness problem
11
Example 3: Matrix multiplication
Given two n × n matrices A and B, find the time efficiency
of the definition-based algorithm for computing their
product C = AB. By definition, C is an n × n matrix whose
elements are computed as the scalar products of the rows of
matrix A and the columns of matrix B:
where C[i, j] = A[i, 0] B[0, j]+ . . . + A[i, k] B[k, j]+ . . . + A[i,
n − 1] B[n − 1, j] for every pair of indices 0 ≤ i, j ≤ n−1
12
Example 3: Matrix multiplication
13
Example 4: Counting binary digits
It cannot be investigated the way the previous examples are.
14
Plan for Analysis of Recursive Algorithms
Decide on a parameter indicating an input’s size.
Identify the algorithm’s basic operation.
Check whether the number of times the basic op. is executed
may vary on different inputs of the same size. (If it may, the
worst, average, and best cases must be investigated
separately.)
Set up a recurrence relation with an appropriate initial
condition expressing the number of times the basic op. is
executed.
Solve the recurrence (or, at the very least, establish its
solution’s order of growth) by backward substitutions or
another method.
15
Example 1: Recursive evaluation of n!
Definition: n ! = 1 2 … (n-1) n for n ≥ 1 and 0! = 1
Recursive definition of n!: F(n) = F(n-1) n for n ≥ 1 and
F(0) = 1
16
Solving the recurrence for M(n)
M(n) = M(n-1) + 1, M(0) = 0
M(n) = M(n − i) + i
We can use the initial condition. Since it is specified for
n = 0, we have to substitute i = n in the above
equation to get the ultimate result of our backward
substitutions:
M(n) = M(n − n) + n = n.
M(n) = Ɵ(n) 17
Example 2: The Tower of Hanoi Puzzle
In this puzzle, we have n disks of different sizes that can slide
onto any of three pegs. Initially, all the disks are on the first
peg in order of size. The goal is to move all the disks to the
third peg, using the second one as an auxiliary, if necessary.
We can move only one disk at a time, and it is forbidden to
place a larger disk on top of a smaller one.
18
Example 2: The Tower of Hanoi Puzzle
1 3
Recurrence for number of moves:
19
Pseudocode for Tower of Hanoi
20
Solving recurrence for number of moves
Let us apply the general plan outlined before:
1. The number of disks n is the obvious choice for the input’s
size indicator
2. Moving one disk is the algorithm’s basic operation.
3. If we strictly follow the technique of moving disks as
discussed in the earlier slides worst, best, average cases are
all similar.
4. Clearly, the number of moves M(n) depends on n only, and
we get the following recurrence equation for it:
M(n) = M(n−1) + 1 + M(n−1) for n > 1
With initial condition M(1) = 1.
21
Solving recurrence for number of moves
5. We solve this recurrence by the same method of
backward substitutions
22
Tree of calls for the Tower of Hanoi Puzzle
n
n-1 n-1
n-2 n-2 n-2 n-2
... ... ...
2 2 2 2
1 1 1 1 1 1 1 1
23
Example 3: Counting #bits
24