Example Problems - Recursion Tree Method

 

Problem-1

Irregular Example Using the Recursion Tree Method

Solve the recurrence

T(n)=T(n/3)+T(2n/3)+Θ(n)\boxed{T(n)=T(n/3)+T(2n/3)+\Theta(n)}

using the Recursion Tree Method.


Step 1: Understand the Recurrence

The recurrence consists of

  • One subproblem of size n3\frac n3
  • One subproblem of size 2n3\frac{2n}{3}
  • Linear work outside recursion

Thus,

a=2a=2

but unlike Merge Sort,

the two recursive calls are not equal.


Why is this recurrence irregular?

Compare it with Merge Sort.

Merge Sort

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

Every recursive call produces

           n

        /      \

      n/2      n/2

Both branches have the same height.


Present Recurrence

T(n)=T(n/3)+T(2n/3)+nT(n)=T(n/3)+T(2n/3)+n
                n

          /             \

       n/3            2n/3

The left branch shrinks much faster than the right branch.

Hence,

the recursion tree is unbalanced.

This is exactly  an irregular example.


Step 2: Draw the First Few Levels

                     n
                  Cost = cn

               /            \

            n/3            2n/3
          Cost            Cost

        /     \          /      \

     n/9   2n/9      2n/9     4n/9

Notice

Left child:

13\frac13

Right child:

23\frac23

Each level continues similarly.





Step 3: Cost of Each Level

The root performs

cncn

units of work.


Level 1

Nodes

n3,2n3\frac n3,\qquad\frac{2n}3

Cost

cn3+c2n3=cnc\frac n3 + c\frac{2n}3 = cn

Level 2

Nodes

n9,2n9,2n9,4n9\frac n9,\quad \frac{2n}9,\quad \frac{2n}9,\quad \frac{4n}9

Total cost

c(19+29+29+49)n=cnc \left( \frac19+\frac29+\frac29+\frac49 \right)n = cn

Again,

the level cost is

cn.cn.

General Observation

At every level,

the subproblem sizes always add up to

n.n.

Therefore,

every level contributes

cn\boxed{cn}



Step 4: Height of the Tree

Unlike Merge Sort,

there is no single height.

Different branches stop at different times.


Leftmost Branch

Each level divides by

3.3.

Problem size after

ii

levels

n3i\frac n{3^i}

Stop when

n3i=1\frac n{3^i}=1

Thus,

i=log⁡3n.i=\log_3 n.

Rightmost Branch

Each level divides only by

32\frac32

because

n→23n.n \rightarrow \frac23n.

After

ii

levels,

problem size

(23)in.\left(\frac23\right)^i n.

Stop when

(23)in=1.\left(\frac23\right)^in=1.

Taking logarithms,

i=log⁡3/2n\boxed{ i=\log_{3/2}n }

 The rightmost branch determines the maximum height of the recursion tree.


Step 5: Total Cost of Internal Nodes

We found

Every level costs

cn.cn.

Height

Θ(log⁡n).\Theta(\log n).

Therefore,

Total Internal Cost=cn×Θ(log⁡n)=O(nlog⁡n)\boxed{ \text{Total Internal Cost} = cn\times\Theta(\log n) = O(n\log n) }



Step 6: What About the Leaves?

At first glance,

one might think

"The tree is binary. Therefore, the number of leaves must be about 2h2^h"

 this reasoning is incorrect.


Incorrect Analysis

Height

log⁡3/2n\log_{3/2}n

Hence,

complete binary tree

contains

2log⁡3/2n=nlog⁡3/222^{\log_{3/2}n} = n^{\log_{3/2}2}

Since

log⁡3/22≈1.71,\log_{3/2}2 \approx1.71,

this suggests

O(n1.71)O(n^{1.71})

leaves.

This would imply

O(n1.71)O(n^{1.71})

leaf cost,

which is larger than

O(nlog⁡n).O(n\log n).

But this conclusion is not correct because the recursion tree is not a complete binary tree. Most branches terminate earlier, especially those repeatedly taking the n/3 branch.


Step 7: Counting Leaves Properly

Instead of guessing,

 a new recurrence is introduced

Let

L(n)L(n)

be the number of leaves.

Then

L(n)=L(n/3)+L(2n/3)\boxed{ L(n) = L(n/3) + L(2n/3) }

with

L(n)=1L(n)=1

for small nn.

Notice that this recurrence is the same as the original one but without the +n+n term.


Step 8: Solve the Leaf Recurrence

Assume

L(n)≤dn.L(n)\le dn.

Substitute

L(n)=L(n/3)+L(2n/3)≤dn3+d2n3=dn.\begin{aligned} L(n) &= L(n/3)+L(2n/3)\\ &\le d\frac n3 + d\frac{2n}3\\ &= dn. \end{aligned}

Thus,

L(n)=O(n)\boxed{ L(n)=O(n) }

We can  prove the matching lower bound as an exercise, yielding

L(n)=Θ(n).\boxed{ L(n)=\Theta(n). }


Step 9: Cost of Leaves

Each leaf contributes

Θ(1).\Theta(1).

Number of leaves

Θ(n).\Theta(n).

Therefore,

Leaf Cost=Θ(n).\boxed{ \text{Leaf Cost} = \Theta(n). }

Step 10: Final Cost

Internal nodes

O(nlog⁡n)O(n\log n)

Leaves

Θ(n)\Theta(n)

Therefore,

T(n)=O(nlog⁡n)+Θ(n)=O(nlog⁡n).T(n) = O(n\log n) + \Theta(n) = O(n\log n).

We can  prove the matching lower bound by substitution, giving the tight result

T(n)=Θ(nlog⁡n).\boxed{ T(n)=\Theta(n\log n). }


Summary

StepResult
Recurrence  T(n)=T(n/3)+T(2n/3)+nT(n)=T(n/3)+T(2n/3)+n
Tree type  Unbalanced (irregular)
Cost per level  cncn
Maximum height  Θ(log⁡n)\Theta(\log n) (specifically, log⁡3/2n\log_{3/2}n)
Total internal-node cost  O(nlog⁡n)O(n\log n)
Number of leaves  Θ(n)\Theta(n)
Total leaf cost  Θ(n)\Theta(n)
Overall running time  Θ(nlog⁡n)\boxed{\Theta(n\log n)}


Problem-2

Solve

T(n)=T(n/2)+n3\boxed{T(n)=T(n/2)+n^3}

using

  1. Recursion Tree Method
  2. Substitution Method ( verify )

Part I: Recursion Tree Method

Step 1: Draw the Recursion Tree

The recurrence is

T(n)=T(n/2)+n3T(n)=T(n/2)+n^3

Each recursive call produces one subproblem of size n/2n/2.

                    n
                 Cost = cn³
                     |
                     |
                  n/2
            Cost = c(n/2)³
                     |
                     |
                  n/4
            Cost = c(n/4)³
                     |
                     |
                  n/8
            Cost = c(n/8)³
                     |
                    ...

Unlike Merge Sort, this tree has only one node at each level.


Step 2: Cost at Each Level

Level 0

cn3cn^3

Level 1

c(n2)3=18cn3c\left(\frac n2\right)^3 = \frac18cn^3

Level 2

c(n4)3=164cn3c\left(\frac n4\right)^3 = \frac1{64}cn^3

Level 3

c(n8)3=1512cn3c\left(\frac n8\right)^3 = \frac1{512}cn^3

Pattern

At level ii,

Problem size

n2i\frac n{2^i}

Cost

c(n2i)3=cn38i\boxed{ c\left(\frac n{2^i}\right)^3 = \frac{cn^3}{8^i} }

Step 3: Height of the Tree

Recursion stops when

n2h=1\frac n{2^h}=1

Therefore,

2h=n2^h=n

Hence,

h=log⁡2n\boxed{ h=\log_2n }

Step 4: Total Cost

Add all levels.

T(n)=cn3+18cn3+164cn3+⋯T(n) = cn^3 +\frac18cn^3 +\frac1{64}cn^3 +\cdots

This is a geometric series with ratio

18.\frac18.

Therefore,

T(n)=cn3(1+18+164+⋯ ).T(n) = cn^3 \left( 1+\frac18+\frac1{64}+\cdots \right).

Using

∑i=0∞ri=11−r,\sum_{i=0}^{\infty}r^i = \frac1{1-r},

where

r=18,r=\frac18,
=cn3(11−18)=cn3(87).= cn^3 \left( \frac1{1-\frac18} \right) = cn^3 \left( \frac87 \right).

Hence,

T(n)=O(n3)\boxed{ T(n)=O(n^3) }

Step 5: Guess

From the recursion tree,

T(n)=O(n3)\boxed{ T(n)=O(n^3) }

Part II: Verify Using the Substitution Method

Step 1: Guess

Assume

T(n)≤cn3\boxed{ T(n)\le cn^3 }

for some constant c>0


Step 2: Induction Hypothesis

Assume

T(n/2)≤c(n2)3.T(n/2)\le c\left(\frac n2\right)^3.

Step 3: Substitute

The recurrence is

T(n)=T(n/2)+n3.T(n)=T(n/2)+n^3.

Substitute the induction hypothesis.

T(n)≤c(n2)3+n3.\begin{aligned} T(n) &\le c\left(\frac n2\right)^3+n^3. \end{aligned}

Step 4: Simplify

(n2)3=n38.\left(\frac n2\right)^3 = \frac{n^3}{8}.

Hence,

T(n)≤c8n3+n3=(c8+1)n3.\begin{aligned} T(n) &\le \frac c8n^3+n^3\\ &= \left(\frac c8+1\right)n^3. \end{aligned}

Step 5: Choose the Constant

We want

T(n)≤cn3.T(n)\le cn^3.

Therefore,

c8+1≤c.\frac c8+1 \le c.

Solve.

1≤c−c8=78c.\begin{aligned} 1 &\le c-\frac c8\\ &= \frac78c. \end{aligned}

Hence,

c≥87.\boxed{ c\ge\frac87. }

Choose

c=2.\boxed{ c=2. }

Then

T(n)≤2n3.T(n)\le2n^3.

Thus, the induction step is proved.


Step 6: Base Case

Choose cc large enough so that

T(1)≤c.T(1)\le c.

Hence,

the base case holds.


Conclusion

Therefore,

T(n)=O(n3).\boxed{ T(n)=O(n^3). }

Showing the Tight Bound

To prove

Θ(n3),\Theta(n^3),

prove the lower bound.

Assume

T(n)≥dn3.T(n)\ge dn^3.

Then

T(n)≥d(n2)3+n3=d8n3+n3=(d8+1)n3.\begin{aligned} T(n) &\ge d\left(\frac n2\right)^3+n^3\\ &= \frac d8n^3+n^3\\ &= \left(\frac d8+1\right)n^3. \end{aligned}

We require

d8+1≥d.\frac d8+1 \ge d.

This gives

d≤87.d\le\frac87.

Choose

d=1.d=1.

Hence,

T(n)=Ω(n3).T(n)=\Omega(n^3).

Therefore,

T(n)=Θ(n3).\boxed{ T(n)=\Theta(n^3). }

Comments

Popular posts from this blog

Design and Analysis of Algorithms PCCST502 Semester 5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Algorithms

Criteria for Analyzing Algorithms- Time and Space Complexity