UNIT-I SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
7. MATHEMATICAL ANALYSIS FOR RECURSIVE
ALGORITHMS:
General Plan for Analyzing the Time Efficiency of
Recursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation.
3. Check whether the number of times the basic operation is executed can vary
on different inputs of the same size; if it can, the worst-case, average-case,
and best-case efficiencies must be investigated separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the
number of times the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its solution.
EXAMPLE 1: Compute the factorial function F(n) = n! for an arbitrary non
negative integer n. Since n!= 1•. ..... • (n − 1) • n = (n − 1)! • n, for n ≥ 1 and
0!= 1 by definition, we can compute F(n) = F(n − 1) • n with the following
recursive algorithm.(ND 2015) ALGORITHMF(n)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1
else return F(n − 1) * n
Algorithm analysis
For simplicity, we consider n itself as an indicator of this algorithm’s input
size. i.e.1.
The basic operation of the algorithm is multiplication; whose number of
executions we denote M(n). Since the function F(n) is computed according
to the formula F(n) = F(n −1)•n for n >0.
The number of multiplications M(n) needed to compute it must satisfy the
equality
M (n − 1) multiplications are spent to compute F(n − 1), and one more
multiplication is needed to multiply the result by n
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
UNIT-I SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
Recurrence relations
The last equation defines the sequence M(n) that we need to find. This
equation defines M(n) not explicitly, i.e., as a function of n, but implicitly as a
function of its value at another point, namely n − 1. Such equations are called
recurrence relations or recurrences.
Solve the recurrence relation (n) = (n − 1) + 1, i.e., to find an explicit formula
forM(n) in terms of n only.
To determine a solution uniquely, we need an initial condition that tells us
the value with which the sequence starts. We can obtain this value by inspecting
the condition that makes the algorithm stop its recursive calls:
if n = 0 return 1.
This tells us two things. First, since the calls stop when n = 0, the smallest
value of n for which this algorithm is executed and hence M(n) defined is 0.
Second, by inspecting the pseudocode’s exiting line, we can see that when n = 0,
the algorithm performs no multiplications.
Thus, the recurrence relation and initial condition for the algorithm’s number of
multiplications
M(n):
M(n) = M(n − 1) + 1
for n >0, M(0)=0 for
n =0.
Method of backward substitutions
M(n) = M(n − 1)+1 substitute M(n − 1) = M(n − 2) +1
= [M(n − 2) + 1]+ 1
= M(n − 2)+2 substitute M(n − 2) = M(n − 3) +1
= [M(n − 3) + 1]+ 2
= M(n − 3) + 3
…
= M(n − i) + i
…
= M(n − n) + n
= n.
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
UNIT-I SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
ThereforeM(n)=n
EXAMPLE 2: consider educational workhorse of recursive algorithms: theTower
of Hanoi puzzle. We have n disks of different sizes that can slide onto any of three
pegs. Consider A (source), B (auxiliary), and C (Destination). Initially, all the
disks are on the first peg in order of size, the largest on the bottom and the smallest
FIGURE 1.7 Recursive solution to the Tower of Hanoi puzzle.
on top. The goal is to move all the disks to the third peg, using the second one as
an auxiliary.
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
UNIT-I SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
Algorithm analysis
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 the obvious initial condition M(1) = 1, we have the following recurrence
relation for the number of moves M(n):
M(n) = 2M (n − 1) + 1
for n >1, M (1) = 1.
We solve this recurrence by the same method of backward substitutions:
M(n) = 2M(n − 1)+1 sub. M(n − 1) = 2M(n − 2) +1
= 2[2M (n − 2) + 1] + 1
= 22M (n − 2) + 2+1 sub. M (n − 2) = 2M (n − 3) +1
= 2 [2M (n − 3) + 1]+ 2 + 1
2
= 23M (n − 3) + 22 + 2+1 sub. M (n − 3) = 2M (n − 4) +1
= 2 M (n − 4) + 2 + 2 + 2 + 1
4 3 2
…
= 2iM (n − i) + 2i−1 + 2i−2 + . . . + 2 + 1= 2iM (n − i) + 2i− 1.
…
Since the initial condition is specified for n = 1, which is achieved
for i = n − 1, M(n) = 2n−1M (n − (n − 1)) + 2n−1 – 1 = 2n−1M (1) + 2n−1
− 1= 2n−1 + 2n−1 − 1= 2n− 1.
Thus, we have an exponential time algorithm
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
UNIT-I SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
EXAMPLE 3: An investigation of a recursive version of the algorithm which
finds the number of binary digits in the binary representation of a positive
decimal integer.
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec(𝗁n/2])+1
Algorithm analysis
The number of additions made in computing BinRec(𝗁n/2]) is A(𝗁n/2]), plus one
more addition is made by the algorithm to increase the returned value by 1. This
leads to the recurrence A(n)=A(𝗁n/2])+1forn >1
Then, the initial condition is A(1) =0.
The standard approach to solving such a recurrence is to solve it
only for n = 2kA(2k) = A(2k−1) + 1 for k >0,
A(20) = 0.
backward substitutions
A(2k) = A(2k−1)+1 substitute A(2k−1) = A(2k−2) +1
= [A(2k−2) + 1]+ 1= A(2k−2)+2 substitute A(2k−2) = A(2k−3) +1
= [A(2k−3) + 1]+ 2 = A(2k−3)+3 . . .
...
= A(2k−i) + i
...
= A(2k−k) + k.
Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original
variable n = 2k and hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
UNIT-1 SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
8. MATHEMATICAL ANALYSIS FOR NON-RECURSIVE ALGORITHMS
1.1 General Plan for Analyzing the Time Efficiency of Non
recursive Algorithms:
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation (in the inner most oop).
3. Check whether the number of times the basic operation is executed
depends only on the size of an input. If it also depends on some additional
property, the worst-case, average-case, and, if necessary, best-case
efficiencies have to be investigated separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation
is executed.
5. Using standard formulas and rules of sum manipulation either find a closed
form formula for the count or at the least, establish its order of growth.
EXAMPLE 1: Consider the problem of finding the value of the largest element
in a list of n numbers. Assume that the list is implemented as an array for
simplicity.
ALGORITHM Max Element(A[0..n − 1])
//Determines the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers
//Output: The value of the largest element in A
Max val ←A[0]
for i ←1 to n − 1 do
if A[i]>maxval
maxval←A[i]
return maxval
Algorithm analysis
The measure of an input’s size here is the number of elements in the array,
i.e., n.
There are two operations in the for loop’s body:
o The comparison A[i]> maxval and
o The assignment max val←A[i].
The comparison operation is considered as the algorithm’s basic operation,
because the comparison is executed on each repetition of the loop and not
the assignment.
The number of comparisons will be the same for all arrays of size n;
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
II/III
UNIT-1 SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
therefore, there is no need to distinguish among the worst, average, and best
cases here.
Let C(n) denotes the number of times this comparison is executed. The
algorithm makes one comparison on each execution of the loop, which is
repeated for each value of the loop’s variable i within the bounds 1 and n −
1, inclusive. Therefore, the sum for C(n) is calculated as follows:
−
() = ∑
=
i.e., Sum up 1 in repeated n-1 times
−
() = ∑ = − ∈ ()
=
EXAMPLE 2: Consider the element uniqueness problem: check whether all the
Elements in a given array of n elements are distinct.
ALGORITHM Unique Elements (A[0..n − 1])
//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct and “false”
otherwise
for i ←0 to n − 2 do
for j ←i + 1 to n − 1 do
if A[i]= A [j ] return false
return true
Algorithm
Analysis
The natural measure of the input’s size here is again n (the number of
elements in the array).
Sincetheinnermostloopcontainsasingleoperation(thecomparisonoftwoeleme
nts), we should consider it as the algorithm’s basic operation.
The number of element comparisons depends not only on n but also on
whether there are equal elements in the array and, if there are, which array
positions they occupy. We will limit our investigation to the worst case only.
One comparison is made for each repetition of the innermost loop, i.e., for
each value of the loop variable j between its limits i + 1 and n − 1; this is
repeated for each value of the outer loop, i.e., for each value of the loop
variable i between its limits 0 and n −2.
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
II/III
UNIT-1 SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
EXAMPLE 3: Consider 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
an n × n matrix whose elements are computed as the scalar (dot) 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.
ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1], B[0..n − 1, 0..n − 1])
//Multiplies two square matrices of order n by the definition-based
algorithm
//Input: Two n × n matrices A and B
//Output: Matrix C = AB
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]
return C
Algorithm analysis
An input’s size is matrix order n.
There are two arithmetical operations (multiplication and addition) in the
innermost loop. But we consider multiplication as the basic operation.
Let us set up a sum for the total number of multiplications M(n) executed
by the algorithm. Since this count depends only on the size of the input
matrices, we do not have to investigate the worst-case, average-case, and
best-case efficiencies separately.
There is just one multiplication executed on each repetition of the
algorithm’s innermost loop, which is governed by the variable k ranging
from the lower bound 0 to the upper bound n −1.
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
II/III
UNIT-1 SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
Therefore, the number of multiplications made for every pair of specific
values of variables i and j is
The total number of multiplications M(n) is expressed by the following triple
sum:
Now, we can compute this sum by using formula (S1) and rule (R1)
.
The running time of the algorithm on a particular machine m, we can do
it by the product If we consider, time spent on the additions too, then
the total time on the machine is
Example: 4
The following algorithm finds the number of binary digits in the binary
representation of a positive decimal integer.
ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary
representation count ←1
while n > 1 do
count
←count +
1 n←𝗁n/2]
return count
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
II/III
UNIT-1 SRI MUTHUKUMARAN INSTITUTE OF TECHNOLOGY
Algorithm Analysis:
An input’s size is n.
The loop variable takes on only a few values between its lower and upper
limits.
Since the value of n is about halved on each repetition of the loop, the
answer should be about log2 n.
The exact formula for the number of times.
The comparison n > 1 will be executed is actually 𝗁log2 n] +1.
AI&DS DESIGN AND ANALYSIS OF ALGORITHM
II/III