Mathematical Analysis of Nonrecursive and Recursive Algorithms
Levitin presents a standardized, step-by-step mathematical methodology for determining the exact operation counts of algorithms.
1. Analysis of Nonrecursive Algorithms
General Plan for Nonrecursive Algorithms
- Decide on parameter n indicating input size.
- Identify the algorithm’s basic operation (usually located in the innermost loop).
- Check whether operation count depends on input properties beyond n. If it does, analyze best-case, worst-case, and average-case separately.
- Set up a summation expressing the number of times the basic operation is executed.
- Simplify the sum using standard summation formulas and rules.
i=l∑u1=u−l+1
i=1∑ni=1+2+⋯+n=2n(n+1)≈21n2∈Θ(n2)
i=1∑ni2=6n(n+1)(2n+1)≈31n3∈Θ(n3)
i=0∑nai=a−1an+1−1(a=1)
Nonrecursive Example: Maximum Element
MaxElement(A[0..n-1]):
maxval = A[0]
for i = 1 to n - 1 do:
if A[i] > maxval then:
maxval = A[i]
return maxval
- Basic operation: Comparison
A[i] > maxval.
- Summation:
C(n)=i=1∑n−11=(n−1)−1+1=n−1=Θ(n)
Nonrecursive Example: Matrix Multiplication
Multiplying two n×n matrices C=A×B:
MatrixMultiplication(A, B):
for i = 0 to n - 1 do:
for j = 0 to n - 1 do:
C[i, j] = 0.0
for k = 0 to n - 1 do:
C[i, j] = C[i, j] + A[i, k] * B[k, j]
- Basic operation: Multiplication
A[i, k] * B[k, j].
- Summation:
M(n)=i=0∑n−1j=0∑n−1k=0∑n−11=i=0∑n−1j=0∑n−1n=i=0∑n−1n2=n3=Θ(n3)
2. Analysis of Recursive Algorithms
General Plan for Recursive Algorithms
- Decide on parameter n indicating input size.
- Identify basic operation.
- Check whether operation count depends on input properties beyond n.
- Set up a recurrence relation with an appropriate base condition (initial condition) expressing the count of basic operations.
- Solve the recurrence using backward substitution or known formulas (such as the Master Theorem).
Method of Backward Substitution
Backward substitution works by repeatedly expanding the recurrence using its own definition until a pattern emerges, expressing T(n) in terms of the initial condition.
Example: Recursive Factorial
Factorial(n):
if n == 0 return 1
else return Factorial(n - 1) * n
Recurrence:
M(n)=M(n−1)+1for n>0,M(0)=0
Applying backward substitution:
M(n)=M(n−1)+1=[M(n−2)+1]+1=M(n−2)+2=[M(n−3)+1]+2=M(n−3)+3 ⋮=M(n−k)+k
The base case is reached when n−k=0⟹k=n:
M(n)=M(0)+n=0+n=n=Θ(n)
Exercises
Exercise 1
Analyze the number of multiplications made by the recursive algorithm that computes 2n by doubling:
P(n)={12⋅P(n−1)if n=0if n>0Set up the recurrence and solve it by backward substitution.
Show solution ↓Hide solution ↑
Solution
Let M(n) be the number of multiplications:
- Base case: M(0)=0.
- Recurrence: M(n)=M(n−1)+1 for n>0.
Backward substitution:
M(n)=M(n−1)+1=M(n−2)+2=⋯=M(n−i)+iAt i=n: M(n)=M(0)+n=n.
Therefore, the algorithm performs exactly n multiplications, which is Θ(n).
Exercise 2
Evaluate the following nested summation:
S(n)=i=1∑nj=1∑i(i+j)Show solution ↓Hide solution ↑
Solution
Split the inner summation:
j=1∑i(i+j)=j=1∑ii+j=1∑ij=i⋅i+2i(i+1)=i2+2i2+i=23i2+iNow compute the outer summation:
S(n)=i=1∑n23i2+i=23i=1∑ni2+21i=1∑niSubstitute standard formulas:
S(n)=23[6n(n+1)(2n+1)]+21[2n(n+1)]=4n(n+1)(2n+1)+4n(n+1)=4n(n+1)(2n+2)=2n(n+1)2=Θ(n3)