Mathematical Analysis of Recursive and NonRecursive Techniques
Mathematical Analysis of Recursive and NonRecursive Techniques
⚫ The portion of the definition that does not contain T is called the
base case of the recurrence relation; the portion that contains T is
called the recurrent or recursive case.
3
Forming Recurrence Relation
⚫ Example 1: Write the recurrence relation for the following method.
void f(int n) {
if (n > 0)
{
cout<<n;
f(n-1);
} }
5
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
⚫ Inevaluating the summation one or more of
the following summation formulae may be used:
⚫ Arithmetic series:
•Special Cases of Geometric Series:
⚫ Geometric Series:
7
Iteration method
Harmonic Series:
Others:
8
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)
10
= Θ(lgn)
Iteration Method – Example
Solve using iteration method T(n) = n + 2T(n/2)
= in + 2iT(1)
= nlgn + nT(1) = Θ(nlgn) 11
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
icn 2
3 cn 2 i
2 cn 2
= 16
log 3 log 3 log 4 3
i0 16 n 4 4
1 n O(n )
i0
1 16
n 3 11
2
Example 3 (simpler proof)
W(n) = W(n/3) + W(2n/3) + n
L2.29
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
T(n)
L2.30
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
T(n/4) T(n/2)
L2.31
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
L2.32
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2
Q(1)
L2.33
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 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)
MATHEMATICAL ANALYSIS OF NON-RECURSIVE
ALGORITHMS
C(n) є Θ(n)
Example 2: Element uniqueness problem
C(n) є Θ(n2)
Example 3: Matrix multiplication
C(n) є Θ(n3)