0% found this document useful (0 votes)
122 views20 pages

Design and Analysis of Algorithms: Recursion

1) Recursion is a process of defining objects or calculations in terms of simpler instances of the same object or calculation. 2) A recurrence relation expresses a problem in terms of smaller instances of the same problem plus additional work. It is used to analyze recursive problems. 3) The master theorem provides a way to solve recurrence relations of the form T(n) = aT(n/b) + f(n) where a >= 1, b > 1, and determining the runtime of T(n).

Uploaded by

Talha Khalid
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)
122 views20 pages

Design and Analysis of Algorithms: Recursion

1) Recursion is a process of defining objects or calculations in terms of simpler instances of the same object or calculation. 2) A recurrence relation expresses a problem in terms of smaller instances of the same problem plus additional work. It is used to analyze recursive problems. 3) The master theorem provides a way to solve recurrence relations of the form T(n) = aT(n/b) + f(n) where a >= 1, b > 1, and determining the runtime of T(n).

Uploaded by

Talha Khalid
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/ 20

DESIGN AND ANALYSIS OF

ALGORITHMS

Lecture 7

Recursion
Recursive Definition
 To define objects/processes in terms of simpler
objects/processes
In simple words converting a problem into similar

problem ,but with reduced size.

2
Recurrence Relation
 To analyse a recursive problem we need to write
recurrence relation.
 A formula which represent a bigger problem into
smaller problem.

But..How to write a recurrence relation?


Analogy!
 Problem: To eat an apple.
 Problem size: n grams
 P(n) could be expressed as:
 P(n)=P(n/2)+P(n/2)
n
 Stopping/terminating condition: remaining
size1gram

Note: Without terminating condition a problem


n/2 n/2
remain unsolved.
Problem 1:
How many binary sequences possible of size n?

 Goal: Use recursive relation to express the problem.

1 2 3 …… n
 Total work= Fill all places with general sequence.
 How to fill first position? Either: 0|1

0
n-1
1 2 3 …… n

1
n-1
1 2 3 …… n
Problem 1:
How many binary sequences possible of size n?

 T(n)=Binary sequence of length n.


 T(2)= Binary sequence of length 2.

 T(3)= Binary sequence of length 3.

 T(4)= Binary sequence of length 4.

 T(k)= Binary sequence of length k.

So far,
 T(n)=T(n-1)+T(n-1)

 T(n)=2T(n-1)

What about the terminating condition?


Problem 1:
How many binary sequences possible of size n?

 Base Case/Terminating condition:


 T(1)=2 ,we could fill either 0 or 1
 This factor should be considered in the solution:

T(n)=2T(n-1) , n>1
T(1)=2 recurrence relation

Provided the base condition ,we could now solve the


rest of the sub problems till size n.
Example – Factorial program
Using Iteration Using Recursion
int Fact(int n)
int Fact(int n)
{
int i=1; int fac=1; {
for(i=1;i=n;i++) if(n==1 || n==0)
{ return 1;
fac=fac*i;
} Else
return fac; return n*fact(n-1);
} }
More Examples of Recursion
Solving Recurrence Relations

 Back Substitution Method


 The Recursion Tree

 The Master Theorem


Recurrence (Back Substitution Method)

A(){ T (n)  1  T (n  1) 1
if(n>1)
return(A(n-1)) T (n  1)  1  T (n  2) 2
} T (n  2)  1  T (n  3) 3

Substitute eq(2) in eq(1) for kth value


 k  T (n  k )
T (n)  1  1  T (n  2) Stopping condition
 2  T (n  2) T (n)  1  T (n  1); n  1
So k=n-1, substitute the value of k
now substitute eq(3)
 (n  1)  T (n  (n  1))
 2  1  T (n  3)  (n  1)  T (1)  n  1  1
 3  T (n  3) n O(n)
Recurrence (Back Substitution Method)
Let’s consider a recursive equation
T (n)  n  T (n  1) 1
T (n)  n  T (n  1); n  1 T (n  1)  (n  1)  T (n  2) 2
T (1)  1 ;n 1 T (n  2)  (n  2)  T (n  3) 3

Substitute eq(2) in eq(1) Stopping condition


T (n)  n  (n  1)  T (n  2) n  (k  1)  1
now substitute eq(3) in above equation n  k 1  1
T (n)  n  (n  1)  (n  2)  T (n  3) k  n2
for kth value
T (n)  n  (n  1)  (n  2)  T (n  3) .. .. ... T (n  k )  T (n  (k  1))
Recurrence (cont)
T (n)  n  (n  1)  (n  2)  T (n  3) .. .. ... T (n  k )  T (n  (k  1))
So k=n-2, substitute the value of k in above equation

T (n)  n  (n  1)  (n  2)  T (n  3) .. .. ... T (n  (n  2))  T (n  (n  2  1))


T (n)  n  (n  1)  (n  2)  T (n  3) .. .. ... 2  1
n(n  1)
T (n) 
2

O(n2)
Recurrence (Master Theorem)
Recurrence (Master Theorem)
T (n)  aT ( )   (n log n)
n k p
b
where a  1, b  1, k  0 and p is real number
1) if a  b , then T (n)   (n
k logba
)

2) if a  b
k

a) if p  1, then T (n)   (n log p 1 n)


logba

b) if p  1, then T (n)   (n logba


log log n)
c) if p  1, then T (n)   (n logba
)
3) if a  bk
a) if p  0, then T (n)   (n log n)
k p

b) if p  0, then T (n)   (nk )


Recurrence (Master Theorem)
Example 1

T (n)  3T ( n )  n2
2
a  3, b  2, k  2 and p  0
Compare a with bk
a bk
3 22
So
3 4
a < bk
Because a<bk , condition 3 of Master theorem is true

As we have p=0 Therefore it falls in the condition 3(a)


So, T (n)   (nk log p n)   (n 2 )
Recurrence (Master Theorem)
Example 2

T (n)  4T ( n )  n2
2
a  4, b  2, k  2 and p  0
Compare a with bk
a bk
4 22
So
4 4
a = bk
Because a=bk , condition 2 of Master theorem is true

As we have p=0 Therefore it falls in the condition 2(a)


So,T (n)   (nlog2 log01 n)   (n log n)
4
2
Recurrence (Master Theorem)
Example 3

T ( n)  T ( n )  n 2
2
a  1, b  2, k  2 and p  0
Compare a with bk
a bk
1 22
So
1 4
a < bk
Because a<bk , condition 3 of Master theorem is true

As we have p=0 Therefore it falls in the condition 3(a)


So, T (n)   (n2 log 0 n)   (n )2
Recurrence (Master Theorem)
Example 4

T (n)  2T ( n )  n log n
2
a  2, b  2, k  1 and p  1
Compare a with bk
a bk
2 21
So
2 2
a = bk
Because a=bk , condition 2 of Master theorem is true

As we have p=1 Therefore it falls in the condition 2(a)


So, T (n)   (n log 2 n)   (n log n) 2
Recurrence (Master Theorem)
Example 5
T (n)  2T ( n )  n
2 log n
 2T ( n )  n log 1 n
2
a  2, b  2, k  1 and p  1
Compare a with bk
2 2
So
a = bk
Because a=bk , condition 2 of Master theorem is true

As we have p=-1 Therefore it falls in the condition 2(b)


So, T (n)   (n log log n)   (n log log n)

You might also like