L-3recurrence Relation (Autosaved)
L-3recurrence Relation (Autosaved)
Recurrence Relation
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
The running time of a recursive function is denoted by T(n) where n is the size of the input.
In recurrence relation, the running time of a recursive function of input size n is expressed in terms of
the running time of the lower value of n. For example
T(n)=T(n−1)+O(1)
• Here, the running time for size n is equal to the running time for size n−1 plus a constant time.
Usually, the running time T(n) is expressed as a conditional statement having the following two
conditions:
1. Base Case: The base case is the running time of an algorithm when the value of n is small such that
the problem can be solved trivially.
2. Recursive case: The recursive running time is given by a recurrence relation for a large value of n.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
how to write recurrence relation looking at the code?
• The first thing to look in the code is the base condition and note down the running time of the base
condition. Remember: every recursive function must have a base condition.
• For each recursive call, notice the size of the input passed as a parameter.
• Calculate the running time of operations that are done after the recursion calls.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 1
The base condition happens when the value of n is 0. We can find the value of 0! in constant time O(1)
trivially i.e. for base condition
T(n) = O(1)
Next, we look for the recursive call. There is only one recursive call with input size n−1 and after the
recursive function returns we do a simple multiplication that takes O(1) time i.e. the total running time is
T(n)=T(n−1)+O(1)
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 2
The base condition happens when the value of n is 0. We can find the
value of 0! in constant time O(1) trivially i.e. for base condition
1 T(n) = O(1)
n+1
T(n-1)
Total = T(n-1)+n+n+1+1
= T(n-1)+2n+2
= T(n-1)+n
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 3
The base condition happens when the value of n is 0. We can find the
value of 0! in constant time O(1) trivially i.e. for base condition
1
T(n) = O(1)
log n +1
log n
T(n-1)
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
T(n) = 2 T(n-1)+ 1 T(n) = T(n/2)+ 1
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Methods for solving Recurrence
• Substitution Method
We make a guess for the solution and then we use mathematical induction to prove the guess is correct or
incorrect.
• Master Method
Master Method is a direct way to get the solution. The master method works only for the following type of
recurrences or for recurrences that can be transformed into the following type.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Substitution Method
• The Substitution Method Consists of two main steps:
Use the mathematical induction to find the boundary condition and shows that the guess is correct.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 1
T(n) = 2T(n/2) + 1
T(n) = 2T(n/2) + 1 = 2[2T(n/4) + 1] + 1 = 4T(n/4) + 2 + 1
T(n) = 4T(n/4) + 3
T(n) = 4T(n/4) + 3 = 4[2T(n/8) + 1] + 3 = 8T(n/8) + 4 + 3
T(n) = 8T(n/8) + 7
T(n) = 8T(n/8) + 7 = 8[2T(n/16) + 1] + 7 = 16T(n/16) + 8 + 7
T(n) = 16T(n/16) + 15
T(n) = 16T(n/16) + 15 = 16[2T(n/32) + 1] + 15 = 32T(n/32) + 16 + 15
T(n) = 32T(n/32) + 31
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
In general, after k iterations, we have:
𝑇(𝑛) = 2𝑘 𝑇 (𝑛/ 2𝑘 )+ 2𝑘 − 1
We’re not done since we still have T(…)’s on the right side of the equation. We need to get down to T(1). How?
We have T(n/ 2𝑘 ), and we want T(1). So let n = 2𝑘 We will then have T(2𝑘 / 2𝑘 ), which equals T(1).
So use that substitution (n = 2𝑘 ) throughout the entire generalized, kth recurrence relation.
𝑇(𝑛) = 2𝑘 𝑇 (𝑛/ 2𝑘 ) + 2𝑘 − 1
= 𝑛 ∗ 𝑇 (2𝑘 / 2𝑘 )+ 𝑛 − 1 = 𝑛 ∗ 𝑇(1) + 𝑛 − 1
𝑇(𝑛) = 𝑛 ∗ 1 + 𝑛 − 1 = 2𝑛 − 1
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 2
Solution:
T(n) = 2T(n/2) + n = 2[2T(n/4) + n/2] + n = 4T(n/4) + n + n
T(n) = 4T(n/4) + 2n
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
So now rewrite these five equations and look for a pattern:
T(n) = 2T(n/2) + n = 21 T(n/ 21) + 1n 1st step of recursion
T(n) = 4T(n/4) + 2n = 22 T(n/ 22 ) + 2n 2nd step of recursion
T(n) = 8T(n/8) + 3n = 23 T(n/ 23) + 3n 3rd step of recursion
T(n) = 16T(n/16) + 4n = 24 T(n/ 24 ) + 4n 4th step of recursion
T(n) = 32T(n/32) + 5n = 25 T(n/ 25 ) + 5n 5th step of recursion
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
So now rewrite these five equations and look for a pattern:
Right side does not have any T(…)’s. This recurrence relation is now solved
it runs in O(n) time
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Master Theorem
The Master Theorem provides a systematic way of solving recurrence relations of the form:
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Continue……..
This theorem is an advance version of master theorem that can be used to determine running time of divide
and conquer algorithms if the recurrence is of the following form :-
Where,
n = size of the problem
a = number of subproblems in the recursion and a >= 1
n/b = size of each subproblem
b > 1, k >= 0 and p is a real number.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Case 1: if a > bk, then T(n) = θ(nlogba)
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 1
Here a = 1, b = 2, k = 0 and p = 0
bk = 1. So, a = bk and p > -1 [Case 2.(a)]
T(n) = θ(logn)
Example 2
a = 3, b = 2, k = 2, p = 0
bk = 4. So, a < bk and p = 0 [Case 3.(a)]
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Practice questions on Master Theorem
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Recurrence Tree Method
• In this method a recurrence relation is converted into recursive tree.
• To find the total cost , cost of all levels are summed up.
Steps to solve recurrence relation
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Example 1
Step 1: Draw the recursion tree
When n= n/2
T(n) = 2T(n/4)+n/2
T(n/20) n Level 0
T(n/21) Level 1
n/2 n/2
T(n/2k) Level k
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
2. Calculate the cost at each level
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Calculate the Height of the Tree
Since we know that when we continuously divide a number by 2, there comes a time when this number is
reduced to 1. Same as with the problem size N, suppose after K divisions by 2, N becomes equal to 1,
which implies, (n / 2k) = 1
Here n / 2^k is the problem size at the last level and it is always equal to 1.
Now we can easily calculate the value of k from the above expression by taking log() to both sides. Below is
a more clear derivation,
n = 2k
•log(n) = log2 (2k)
•log(n) = k * log2 (2)
•k = log2(n) / log2 (2)
•k = log2 (n) ………………..(2)
So the height of the tree is log (n)
base 2.
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Calculate the number of nodes at each level
Let's first determine the number of nodes in the last level. From the recursion tree, we can deduce
this
Level-0 have 1 (2^0) node
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Sum up the cost of all the levels
Total Cost = Cost of all levels except last level + Cost of last level
Total Cost = Cost for level-0 + Cost for level-1 + Cost for level-2 +.... + Cost for level-log(n) + Cost for last
level
= n log2n +O(n)
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II
Thank You
Subject Coordinator: Dr. Sonal Chandel Subject Name & Code: DSA (CS-102) Semester: II