0% found this document useful (0 votes)
32 views52 pages

4-5. Mathematical Analysis of Recursive and NonRecursive Techniques

Uploaded by

ritikjain4560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views52 pages

4-5. Mathematical Analysis of Recursive and NonRecursive Techniques

Uploaded by

ritikjain4560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Definition

⚫ A recurrence relation, is a recursive function


T(n), variable n. ofinteger
⚫ Like all recursive functions, it has both recursive case
and base case.
⚫ Example:

⚫ 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.
⚫ Recurrence relations are useful for expressing the
1
running times (i.e., the number of basic operations
Example: Fibonacci Sequence
⚫The Fibonacci is defined by
sequence recurrence the
relation
⚫ fn = fn-1 + fn-2,
n>=3 and initial
conditions
⚫fFibonacci
1 = 1, fSequence:
2 = 1. 1, 1, 2, 3, 5, 8,
13, 21,…….

2
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);
} }

⚫ 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:

•Special Cases of Geometric


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 + T(n/2) T(n/2) = c + T(n/4)


= c + c + T(n/4)
T(n/4) = c + T(n/8)
= c + c + c + T(n/8) +---+ T(n/2i)

In ith step n/2i = 1


so n = 2i
by the property of log i = log2n

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)

T(n/2) = n/2 + 2T(n/4)


T(n) = n + 2T(n/2)
= n + 2(n/2 + 2T(n/4))
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8) +---+ 2iT(n/2i)
= i*n + 2iT(n/2i)
In ith step n/2i = 1
so n = 2i
by the property of i = log2n

= 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

• Here while solving recurrences, we divide the problem into


subproblems of equal size.

• For e.g., T(n) = a T(n/b) + f(n) where a > 1 ,b > 1 and f(n) is a given
function .

F(n) is the cost of splitting or combining the sub problems.

T n/b T n/b
Example 1
W(n) = 2W(n/2) + n2

• Subproblem size at level i is: n/2i


• Subproblem size hits 1 when 1 = n/2i  i = lgn
• Cost of the problem at level i = (n/2i)2 No. of nodes at level i =
• 2i
Total cost: i

1
lg n1
2 lg n1 i
n 1 1
W (n)    2 W (1)  n     n  n  
lg n 2 2
  O(n) n
2
i
i 0 2 i 0  2  i 0  2  1 1 2
 O(n)  2n 2
 W(n) = O(n2) 10
Example 2
T(n) = 3T(n/4) + cn2

• Subproblem size at level i is: n/4i


• Subproblem size hits 1 when 1 = n/4i  i = log4n
• Cost of a node at level i = c(n/4i)2
• Number of nodes at level i = 3i  last level has 3log n = nlog 3 nodes
4 4

• Total cost:
T (n) 
log 4 n1
 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 
i0
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
27
 W(n) =
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:

L2.28
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
T(n)

L2.29
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
T(n/4) T(n/2)

L2.30
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2

T(n/16) T(n/8) T(n/8) T(n/4)

L2.31
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2

(n/16)2 (n/8)2 (n/8)2 (n/4)2


Q(1)

L2.32
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2
(n/4)2 (n/2)2

(n/16)2 (n/8)2 (n/8)2 (n/4)2




Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1)


Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
(n/4)2 (n/2)2

(n/16)2 (n/8)2 (n/8)2 (n/4)2




Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1)


Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
(n/16)2 (n/8)2 (n/8)2 (n/4)2



Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1)


Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256




Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1)


Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256




Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) )


Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2:
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256




Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) )


Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:


n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256




Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) )


n/2i = 1 => n = 2i => i = log2n
Example of recursion tree
+ 2i *T(1)

)i + 2log2n

For simplicity assume it as an infinity series

)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

The recursion tree for this recurrence is :

n
n
T n/2 T n/2
n/2 n/2 n

log2 n n/22 n/22 n/22 n/22

:
:
:

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.

We have n + n + n + …… log n times


= n (1 + 1 + 1 + …… log n times)
= n (log2 n)
= Ɵ (n log n)

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

T(n/4) T(n/4) T(n/4) T(n/4)


II.
Given : T(n) = 2T(n/2) + 1
Solution : The recursion tree for the above recurrence is

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 :

We get series like


1 + 2 + 22 + 23 + …… log n times which is a G.P.

[ So, using the formula for sum of terms in a G.P. :


a + ar + ar2 + ar3 + …… + ar n – 1 = a( r n – 1 )
r – 1 ]
= 1 (2log n – 1)
2–1
= n–1
= Ɵ (n – 1) (neglecting the lower order terms)
= Ɵ (n)
Example (using change the variable method)
Domain transformations can sometimes be used to substitute a function for the
argument of the relation and make it easier to solve.

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

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
Example 1: Maximum element

C(n) є Θ(n)
Example 2: Element uniqueness problem

C(n) є Θ(n2)
Example 3: Matrix multiplication

C(n) є Θ(n3)

You might also like