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

Recurrence Relation for Dividing Function
Figure: Recurrence Relation for Dividing Function

From the image, we can say

T(n)=T(n2)+1+1
=T(n2)+2

In place of constant 2, we can write 1 or simply represent it with c.

T(n)=T(n2)+1

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:

T(n)=ab+f(n)
where,
f(n)=Ө(nk(logn)p),
a1,b>1,
k1,
p=real number

Now, let's observe the equation for three different cases:

  • Case 1: logba>k
  • Case 2: logba=k
  • Case 3: logba<k

If logba>k, then T(n)=Ө(nlogba)

If logba=k, then we should check values of p for three cases:

  1. if p >-1, then T(n)=Ө(nklogp+1n)
  2. if p = -1, then T(n)=Ө(nkloglogn))
  3. if p < -1, then T(n)=Ө(nk)

If logba<k, then we should check values of p for two cases:

  1. if p0, then T(n)=Ө(nklogpn))
  2. if p < 0, then T(n)=Ө(nk)

Example 1

The recurrence relation of the dividing function we previously wrote is:

T(n)=T(n2)+1

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:

T(n)=aT(nb)+Ө(nk(logn)p))

Step 2: Modifying recurrence relation to match the general form.

T(n)=1T(n2)+Ө(n0(logn)0)

Step 3: Finding abk and p and checking if the master theorem is possible.

a = 1
b = 2
k = 0
p = 0

Since a1b > 1k0, and p is a real number, the master theorem is possible.

Step 4: Comparing logba and k and applying the matching rule.

Here,

logba=log21=0
k=0

Since logba=k and p > -1, let's apply the formula to find the time complexity for this scenario.

T(n)=Ө(nklogp+1n)
=Ө(n0log0+1n)
=Ө(1log1n)
=Ө(logn)


Example 2

Suppose we have the following recurrence relation:

T(n)=4T(n2)+n

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:

T(n)=aT(nb)+Ө(nk(logn)p)

Step 2: Modifying recurrence relation to match the general form.

T(n)=4T(n2)+Ө(n1(logn)0)

Step 3: Finding abk and p and checking if the master theorem is possible

a = 4
b = 2
k = 1
p = 0

Since a1b > 1k0, and p is a real number, the master theorem is possible.

Step 4: Comparing logba and k and applying the matching rule.

Here,

logba=log24=2
k=1

Since logba>k, let's apply the formula to find the time complexity for this scenario.

T(n)=Ө(nlogba)
=Ө(n2)


Example 3

Suppose we have the following recurrence relation:

T(n)=2T(n2)+n2

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:

T(n)=aT(nb)+Ө(nk(logn)p)

Step 2: Modifying recurrence relation to match the general form.

T(n)=2T(n2)+Ө(n2(logn)0)

Step 3: Finding abk, and p and checking if the master theorem is possible.

a = 2
b = 2
k = 2
p = 0

Since a1b > 1k0, and p is a real number, the master theorem is possible.

Step 4: Comparing logba and k and applying the matching rule.

Here,

logba=log22=1
k=2

Since logba and p0, let's apply the formula to find the time complexity for this scenario.

T(n)=Ө(nk(logn)p)
=Ө(n2(logn)0)
=Ө(n21)
=Ө(n2)


Example 4

Suppose we have the following recurrence relation:

T(n)=2T(n2)+Ө(n2log2n)

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:

T(n)=aT(nb)+Ө(nk(logn)p)

Step 2: Modifying recurrence relation to match the general form.

T(n)=2T(n2)+Ө(n2(logn)2)

Step 3: Finding abk, and p and checking if the master theorem is possible.

a = 2
b = 2
k = 2
p = 2

Since a1b > 1k0, and p is a real number, the master theorem is possible.

Step 4: Comparing logba and k and applying the matching rule.

Here,

logba=log22=1
k=2

Since logba<k and p0, let's apply the formula to find the time complexity for this scenario.

T(n)=Ө(nk(logn)p)
=Ө(n2(logn)2)
=Ө(n2log2n)

Comments