Mathematical Analysis of Recursive Techniques
Mathematical Analysis of Recursive Techniques
2
Forming Recurrence Relation
⚫ For a given recursive method, the base case and the recursive
case of its recurrence relation correspond directly to the base
case and the recursive case of the method.
⚫ Example 1: Write the recurrence relation for the following
method.
void f(int n) {
if (n > 0)
{
cout<<n;
f(n-1);
} }
⚫ The base case is reached when n == 0. The method performs
one comparison. Thus, the number of operations when n == 0,
T(0), is some constant a.
⚫ When n > 0, the method performs two basic operations and then
calls itself,
using ONE recursive call, with a parameter n – 1.
⚫ Therefore the recurrence relation
T(0)is:
=a where a is constant
T(n) = b + where b is constant, 3
T(n-1) n>0
Forming Recurrence Relation
⚫ Example 2: Write the recurrence relation for the
following method.
int g(int n) {
if (n == 1)
return 2;
else
return g(n /
2) + g( n
/ 2) + 5;
⚫ The }base case is reached when n == 1. The
method performs one comparison and
one return statement. Therefore, T(1), is constant c.
⚫ When n>1, the method performs TWO recursive calls,
each with the parameter n/2, and some constant # of
basic operations.
⚫ Hence, the recurrence relation is:
4
Solving Recurrences
• Iteration Method
• Recursion Tree Method
• Master Method
• Change of Variable Method
Iteration method
⚫ In this method the recurrence is expanded as
summation of terms and finally the summation
provides the solution
⚫ In evaluating the summation one or more of
the following summation formulae may be used:
⚫ Arithmetic series:
⚫ Geometric Series:
6
Iteration method
Harmonic
Series:
Other
s:
7
The Method of Iteration
• Let {ai} be the sequence defined by:
ak = ak−1 + 2 with a0 = 1.
• Plugging values of k into the relation, we get:
a1 = a0 + 2 = 1 + 2
a2 = a1 + 2 = 1 + 2 + 2 = 1 + 2(2)
a3 = a2 + 2 = 1 + 2 + 2 + 2 = 1 + 3(2)
a4 = a3 + 2 = 1 + 2 + 2 + 2 + 2 = 1 + 4(2)
• Continuing in this fashion reinforces the apparent pattern that
an = 1 + n(2) = 1 + 2n.
• This brute force technique is the Method of Iteration.
The Iteration Method
T(n) = c + T(n/2)
T(n) = c + c + … + c + T(1)
i times
= c*i + T(1)
= clgn + T(1)
9
= Θ(lgn)
Iteration Method – Example
Solve using iteration method T(n) = n + 2T(n/2)
= in + 2iT(1)
= nlgn + nT(1) = Θ(nlgn) 10
Iteration Method
Iteration Method cont……
Recursion-tree method
• A recursion tree models the costs (time) of a
recursive execution of an algorithm.
• The recursion tree method is good for
generating guesses for the substitution
method.
• Convert the recurrence into a tree:
• – Each node represents the cost incurred at
various levels of recursion
• – Sum up the costs of all levels
Solving Recurrences using Recursion Tree Method
• For e.g., T(n) = a T(n/b) + f(n) where a > 1 ,b > 1 and f(n) is a given
function .
T n/b T n/b
Example 1
W(n) = 2W(n/2) + n2
• Total cost:
T (n)
log 4 n1
3
i cn
2
3 cn 2 i
2 cn 2
16
log 3 log 3 log 4 3
i 16 n 4 4
1 n O(n )
0
i0
1 16
n 3 11
Example 3 (simpler proof)
W(n) = W(n/3) + W(2n/3)
+n
• The longest path from the root to a
leaf is: n
(2/3)n (2/3)2 n … 1
• Subproblem size hits 1 when 1
=
(2/3)in i=log3/2n
•
Cost of the problem at level i = n
•
Total cost:
lg n
W (n) n n ... n(log
3/ n) n
2
O(n lg
3
n) lg 2
17
W(n) =
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
L2.18
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
T(n)
L2.19
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
T(n/4) T(n/2)
L2.20
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
L2.21
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
Q(1)
L2.22
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
)i + 2log2n
)i + nlog22
So T(n) = n2 (1 / (1-5/16)) + n
T(n) = n2 (11/16) + n
So T(n) = Q(n2)
Appendix: geometric series
n 1
1 x
1 x x2 xn for x ¹ 1
1 x
2 1
1 x x for |x| < 1
1 x
1) T(n) = 2T(n/2) + n
n
n
T n/2 T n/2
n/2 n/2 n
:
:
:
1 1 1 1 1 1 1
When we add the values across the levels of the recursion tree, we get a
value of n for every level.
T(n) = Ɵ (n log n)
II.
Given : T(n) = 2T(n/2) + 1
Solution : The recursion tree for the above recurrence is
T(n/2) T(n/2)
II.
Given : T(n) = 2T(n/2) + 1
Solution : The recursion tree for the above recurrence is
1 1
1 1
1 1 2
log n 1 1 1 1 4
:
:
:
Now we add up the costs over all levels of the recursion tree, to determine
the cost for the entire tree :
Consider the following recurrence and find out the upper bound
T(n) = 2∗T(√n) + logn
Sol: So we are given T(n) = 2∗T(√n) + logn
To simplify this let n = 2m (or m = log n)
⇒ T(2m) = 2∗T(√ 2m) + log(2m)
⇒ T(2m) = 2∗T(2m/2) + m
now to simplify more let S(m) = T(2m)
⇒ S(m) = 2∗S(m/2) + m
now we can guess the solution very easily so let's assume S(m) = O( m log m)
now changing back from S(m) to T(n) we will obtain
T(n) = T(2m) = S(m) = O (m log m)
so T(n) = O(log n log log n)
Example (using change the variable method) cont………
So we have
T(n) = 2∗T(√n) + log n
So we need to prove that T(n) < = c(logn log log n)
So by induction lets assume that our bound holds for k = n 1/2
So T(n) <= 2 (c. log n1/2 log log n1/2) + log n
<= 2 (c (logn)/2 * log((logn)/2) + log n
= c logn (log log n – log 2) + log n
= c log n loglog n – c logn log2 + log n
= c log n loglog n – c log n +log n
= c log n loglog n – log n(c-1)
<= c log n loglog n
So our guessed solution works for c>1 and n>=2