Master Theorem for Dividing Function
Recurrence Relation for Dividing Function
Mathematically speaking, the master theorem provides a solution to the recurrence relation. Let's learn how we can find the recurrence relation for a dividing function.
Suppose we have a program like this.
def test(n):
if n > 0:
print(n)
test(n // 2)If the test() function takes T(n) time to execute, then

From the image, we can say
In place of constant 2, we can write 1 or simply represent it with c.
Solving Recurrence Relation with Master Theorem
To solve an equation using recurrence relation for a dividing function, the function must be in the following format:
Now, let's observe the equation for three different cases:
- Case 1:
- Case 2:
- Case 3:
If , then
If , then we should check values of p for three cases:
- if
p >-1, then - if
p = -1, then - if
p < -1, then
If , then we should check values of p for two cases:
- if , then
- if
p < 0, then
Example 1
The recurrence relation of the dividing function we previously wrote is:
Now let's calculate its time complexity using the master theorem.
Step 1: Comparing the recurrence relation with the general form.
The general form of T(n) is:
Step 2: Modifying recurrence relation to match the general form.
Step 3: Finding a, b, k and p and checking if the master theorem is possible.
a = 1
b = 2
k = 0
p = 0Since , b > 1, , and p is a real number, the master theorem is possible.
Step 4: Comparing and k and applying the matching rule.
Here,
Since and p > -1, let's apply the formula to find the time complexity for this scenario.
Example 2
Suppose we have the following recurrence relation:
Now let's calculate its time complexity using the master theorem.
Step 1: Comparing the recurrence relation with the general form.
The general form of T(n) is:
Step 2: Modifying recurrence relation to match the general form.
Step 3: Finding a, b, k and p and checking if the master theorem is possible
a = 4
b = 2
k = 1
p = 0Since , b > 1, , and p is a real number, the master theorem is possible.
Step 4: Comparing and k and applying the matching rule.
Here,
Since , let's apply the formula to find the time complexity for this scenario.
Example 3
Suppose we have the following recurrence relation:
Now let's calculate its time complexity using the master theorem.
Step 1: Comparing the recurrence relation with the general form.
The general form of T(n) is:
Step 2: Modifying recurrence relation to match the general form.
Step 3: Finding a, b, k, and p and checking if the master theorem is possible.
a = 2
b = 2
k = 2
p = 0Since , b > 1, , and p is a real number, the master theorem is possible.
Step 4: Comparing and k and applying the matching rule.
Here,
Since and , let's apply the formula to find the time complexity for this scenario.
Example 4
Suppose we have the following recurrence relation:
Now let's calculate its time complexity using the master theorem.
Step 1: Comparing the recurrence relation with the general form.
The general form of T(n) is:
Step 2: Modifying recurrence relation to match the general form.
Step 3: Finding a, b, k, and p and checking if the master theorem is possible.
a = 2
b = 2
k = 2
p = 2Since , b > 1, , and p is a real number, the master theorem is possible.
Step 4: Comparing and k and applying the matching rule.
Here,
Since and , let's apply the formula to find the time complexity for this scenario.
Comments
Post a Comment