Substitution Method - Example Problems

 

Problem-1

Use the substitution method to show that

T(n)=T(n−1)+nT(n)=T(n-1)+n

has the asymptotic upper bound

T(n)=O(n2)\boxed{T(n)=O(n^2)}

Step 1: Guess the Solution

Looking at the recurrence,

T(n)=T(n−1)+n,T(n)=T(n-1)+n,

the additional work performed at each recursive call is nn.

Since

1+2+3+⋯+n=n(n+1)2,1+2+3+\cdots+n=\frac{n(n+1)}{2},

it is reasonable to guess that

T(n)=O(n2)\boxed{T(n)=O(n^2)}

Step 2: State the Induction Hypothesis

Assume that for all values smaller than nn,

T(k)≤ck2\boxed{T(k)\le ck^2}

for some constant

c>0.c>0.

In particular,

T(n−1)≤c(n−1)2.T(n-1)\le c(n-1)^2.

Step 3: Substitute into the Recurrence

Using the recurrence,

T(n)=T(n−1)+nT(n)=T(n-1)+n

Substitute the induction hypothesis:

T(n)≤c(n−1)2+n.T(n) \le c(n-1)^2+n.

Step 4: Expand the Square

Recall that

(n−1)2=n2−2n+1.(n-1)^2=n^2-2n+1.

Therefore,

T(n)≤c(n2−2n+1)+n.T(n) \le c(n^2-2n+1)+n.

Expanding,

T(n)≤cn2−2cn+c+n.T(n) \le cn^2-2cn+c+n.

Step 5: Rearrange

T(n)≤cn2−(2c−1)n+c.T(n) \le cn^2-(2c-1)n+c.

To prove

T(n)≤cn2,T(n)\le cn^2,

we require

−(2c−1)n+c≤0.-(2c-1)n+c\le0.

Step 6: Choose Suitable Constants

If

c≥1,c\ge1,

then

2c−1≥1.2c-1\ge1.

Hence

−(2c−1)n+c-(2c-1)n+c

is negative for sufficiently large values of nn.

For example, choosing

c=1,\boxed{c=1},

gives

−n+1≤0-n+1\le0

for all

n≥1.n\ge1.

Thus,

T(n)≤cn2.T(n)\le cn^2.

Step 7: Verify the Base Case

Assume

T(1)=1.T(1)=1.

Then

T(1)=1≤c(1)2.T(1)=1\le c(1)^2.

Choosing

c≥1c\ge1

satisfies the base case.


Conclusion

Since both the induction step and the base case hold,

T(n)≤cn2\boxed{T(n)\le cn^2}

for some constant cc.

Therefore,

T(n)=O(n2).\boxed{T(n)=O(n^2).}

Complete Induction Proof (Exam Style)

Given:

T(n)=T(n−1)+nT(n)=T(n-1)+n

Guess:

T(n)=O(n2)T(n)=O(n^2)

Assume that

T(n−1)≤c(n−1)2T(n-1)\le c(n-1)^2

for some constant c>0c>0.

Then,

T(n)=T(n−1)+n≤c(n−1)2+n=c(n2−2n+1)+n=cn2−2cn+c+n=cn2−(2c−1)n+c≤cn2\begin{aligned} T(n) &=T(n-1)+n\\ &\le c(n-1)^2+n\\ &=c(n^2-2n+1)+n\\ &=cn^2-2cn+c+n\\ &=cn^2-(2c-1)n+c\\ &\le cn^2 \end{aligned}

provided c≥1c\ge1 and nn is sufficiently large.

For the base case,

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

which is true for c≥1c\ge1.

Hence,

T(n)=O(n2).\boxed{T(n)=O(n^2).}

Note for Students

The bound O(n2)O(n^2)is correct but not tight. If we expand the recurrence,

T(n)=T(1)+2+3+⋯+n=Θ(n2),T(n)=T(1)+2+3+\cdots+n =\Theta(n^2),

so the tight asymptotic bound is

T(n)=Θ(n2).\boxed{T(n)=\Theta(n^2).}

Problem-3

Use the substitution method to show that

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

has the asymptotic upper bound

T(n)=O(nlog⁡n)\boxed{T(n)=O(n\log n)}

Assume

T(1)=Θ(1).T(1)=\Theta(1).

Step 1: Guess the Solution

Since this is the recurrence for Merge Sort, we guess

T(n)=O(nlog⁡n)\boxed{T(n)=O(n\log n)}

Step 2: State the Induction Hypothesis

Assume that for all smaller values,

T(k)≤cklog⁡k\boxed{T(k)\le ck\log k}

for some constant

c>0.c>0.

In particular,

T(n/2)≤cn2log⁡(n2).T(n/2)\le c\frac n2\log\left(\frac n2\right).

Step 3: Substitute into the Recurrence

The recurrence is

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

Substitute the induction hypothesis.

T(n)≤2(cn2log⁡n2)+n.\begin{aligned} T(n) &\le 2\left(c\frac n2\log\frac n2\right)+n. \end{aligned}

Step 4: Simplify

Since

2×n2=n,2\times\frac n2=n,

we get

T(n)≤cnlog⁡n2+n.T(n) \le cn\log\frac n2+n.

Using the logarithm identity

log⁡n2=log⁡n−log⁡2,\log\frac n2=\log n-\log2,

and

log⁡2=1,\log2=1,

we obtain

T(n)≤cn(log⁡n−1)+n=cnlog⁡n−cn+n=cnlog⁡n−(c−1)n.\begin{aligned} T(n) &\le cn(\log n-1)+n\\ &=cn\log n-cn+n\\ &=cn\log n-(c-1)n. \end{aligned}

Step 5: Choose Suitable Constants

To prove

T(n)≤cnlog⁡n,T(n)\le cn\log n,

we require

−(c−1)n≤0.-(c-1)n\le0.

This holds whenever

c≥1.\boxed{c\ge1.}

Hence,

T(n)≤cnlog⁡n.T(n)\le cn\log n.

Thus, the induction step is complete.


Step 6: Verify the Base Case

Choose

n0=2.n_0=2.

For

n=2,n=2,
T(2)≤2clog⁡2=2c.T(2)\le2c\log2=2c.

Choose

cc

large enough so that

T(2)≤2c.T(2)\le2c.

Hence, the base case is satisfied.


Conclusion

Since both the induction step and the base case hold,

T(n)≤cnlog⁡n\boxed{T(n)\le cn\log n}

for sufficiently large nn.

Therefore,

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

Complete Exam-Style Proof

Given

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

Guess

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

Assume

T(n/2)≤cn2log⁡n2.T(n/2)\le c\frac n2\log\frac n2.

Then

T(n)=2T(n/2)+n≤2(cn2log⁡n2)+n=cnlog⁡n2+n=cn(log⁡n−1)+n=cnlog⁡n−cn+n=cnlog⁡n−(c−1)n≤cnlog⁡n,\begin{aligned} T(n) &=2T(n/2)+n\\ &\le2\left(c\frac n2\log\frac n2\right)+n\\ &=cn\log\frac n2+n\\ &=cn(\log n-1)+n\\ &=cn\log n-cn+n\\ &=cn\log n-(c-1)n\\ &\le cn\log n, \end{aligned}

provided

c≥1.c\ge1.

Choose cc large enough to satisfy the base case.

Hence,

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

Proving the Tight Bound Θ(nlog⁡n)

To show the solution is tight, we also prove the lower bound.

Assume

T(n)≥dnlog⁡nT(n)\ge dn\log n

for some constant d>0d>0

Using the recurrence,

T(n)≥2(dn2log⁡n2)+n=dn(log⁡n−1)+n=dnlog⁡n−dn+n=dnlog⁡n+(1−d)n.\begin{aligned} T(n) &\ge2\left(d\frac n2\log\frac n2\right)+n\\ &=dn(\log n-1)+n\\ &=dn\log n-dn+n\\ &=dn\log n+(1-d)n. \end{aligned}

If

d≤1,d\le1,

then

(1−d)n≥0,(1-d)n\ge0,

so

T(n)≥dnlog⁡n.T(n)\ge dn\log n.

Thus,

T(n)=Ω(nlog⁡n).\boxed{T(n)=\Omega(n\log n).}

Combining the upper and lower bounds,

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

Problem-4

Use the substitution method to show that

T(n)=2T(n2+17)+nT(n)=2T\left(\frac n2+17\right)+n

has the asymptotic upper bound

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

Step 1: Guess the Solution

Since the recurrence is similar to the Merge Sort recurrence,

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

we guess

T(n)≤cnlog⁡n\boxed{T(n)\le cn\log n}

for some constant c>0c>0

To avoid changing constants during the proof, strengthen the induction hypothesis.

Assume

T(n)≤cnlog⁡n−dn\boxed{ T(n)\le cn\log n-dn }

where d>0d>0.

Then

T(n)≤2[c(n2+17)log⁡(n2+17)−d(n2+17)]+n.\begin{aligned} T(n) &\le 2\left[ c\left(\frac n2+17\right) \log\left(\frac n2+17\right) - d\left(\frac n2+17\right) \right] +n. \end{aligned}

Using

log⁡(n2+17)≤log⁡n,\log\left(\frac n2+17\right)\le\log n,

we obtain

T(n)≤cnlog⁡n+34clog⁡n−dn−34d+n.T(n) \le cn\log n + 34c\log n - dn - 34d + n.

Rearranging,

T(n)≤cnlog⁡n−(d−1)n+34clog⁡n−34d.T(n) \le cn\log n - (d-1)n + 34c\log n - 34d.

Choose d>1d>1. Since nn grows faster than log⁡n\log n,

(d−1)n>34clog⁡n(d-1)n > 34c\log n

for sufficiently large nn.

Hence,

T(n)≤cnlog⁡n−dn.T(n) \le cn\log n-dn.

Therefore, the induction hypothesis is preserved.


Conclusion

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

Problem-5

Use the substitution method to show that

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

has the asymptotic upper bound

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

Step 1: Guess the Solution

Observe that

  • Each recursive call divides the problem by 3.
  • There are 2 recursive calls.
  • The work outside recursion is nn.

Since

nlog⁡32=n0.63,n^{\log_3 2}=n^{0.63},

the linear term dominates. Hence, we guess

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

Step 2: State the Induction Hypothesis

Assume that for all smaller values,

T(k)≤ck\boxed{T(k)\le ck}

for some constant

c>0.c>0.

In particular,

T(n3)≤cn3.T\left(\frac n3\right)\le c\frac n3.

Step 3: Substitute into the Recurrence

The given recurrence is

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

Using the induction hypothesis,

T(n)≤2(cn3)+n=2c3n+n.\begin{aligned} T(n) &\le 2\left(c\frac n3\right)+n\\[2mm] &= \frac{2c}{3}n+n. \end{aligned}

Step 4: Simplify

Factor out nn:

T(n)≤(2c3+1)n.T(n) \le \left(\frac{2c}{3}+1\right)n.

To prove

T(n)≤cn,T(n)\le cn,

we need

2c3+1≤c.\frac{2c}{3}+1\le c.

Step 5: Find the Constant

Solve the inequality:

2c3+1≤c1≤c−2c31≤c3c≥3.\begin{aligned} \frac{2c}{3}+1 &\le c\\ 1 &\le c-\frac{2c}{3}\\ 1 &\le \frac{c}{3}\\ c &\ge 3. \end{aligned}

Choose

c=3.\boxed{c=3.}

Then

T(n)≤3n.T(n)\le3n.

Thus, the induction step is proved.


Step 6: Verify the Base Case

Assume

T(1)=k,T(1)=k,

where kk is a constant.

Choose

c≥max⁡{3,k}.c\ge\max\{3,k\}.

Then

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

so the base case is satisfied.


Conclusion

Since both the induction step and the base case hold,

T(n)≤cn\boxed{T(n)\le cn}

for some constant cc.

Therefore,

T(n)=O(n).\boxed{T(n)=O(n).}

Complete Exam-Style Proof

Given

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

Guess

T(n)=O(n).T(n)=O(n).

Assume

T(n/3)≤cn3T(n/3)\le c\frac n3

for some constant c>0c>0.

Then,

T(n)=2T(n/3)+n≤2(cn3)+n=2c3n+n=(2c3+1)n.\begin{aligned} T(n) &=2T(n/3)+n\\ &\le2\left(c\frac n3\right)+n\\ &=\frac{2c}{3}n+n\\ &=\left(\frac{2c}{3}+1\right)n. \end{aligned}

To satisfy

T(n)≤cn,T(n)\le cn,

we require

2c3+1≤c,\frac{2c}{3}+1\le c,

which gives

c≥3.c\ge3.

Choose c=3c=3. The base case also holds for a sufficiently large cc.

Hence,

T(n)=O(n).\boxed{T(n)=O(n).}

Tight Bound

To prove the lower bound, assume

T(n)≥dnT(n)\ge dn

for some constant d>0d>0

Then,

T(n)≥2(dn3)+n=(2d3+1)n.\begin{aligned} T(n) &\ge2\left(d\frac n3\right)+n\\ &=\left(\frac{2d}{3}+1\right)n. \end{aligned}

To show

T(n)≥dn,T(n)\ge dn,

we need

2d3+1≥d.\frac{2d}{3}+1\ge d.

This simplifies to

d≤3.d\le3.

Choose

d=3.d=3.

Hence,

T(n)=Ω(n).T(n)=\Omega(n).

Combining the upper and lower bounds,

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

Problem-6

Use the substitution method to show that

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

has the asymptotic upper bound

T(n)=O(n2)\boxed{T(n)=O(n^2)}

Assume

T(1)=Θ(1).T(1)=\Theta(1).

Step 1: Guess the Solution

Observe that

  • Number of recursive calls = 4
  • Each subproblem size = n/2n/2
  • Work outside recursion = nn

Since the recurrence is similar to one solved using the Master Theorem,

nlog⁡24=n2,n^{\log_24}=n^2,

we guess

T(n)=O(n2).\boxed{T(n)=O(n^2)}.

Step 2: State the Induction Hypothesis

Assume that for all smaller values,

T(k)≤ck2\boxed{T(k)\le ck^2}

for some constant

c>0.c>0.

In particular,

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

Step 3: Substitute into the Recurrence

Given

T(n)=4T(n/2)+n,T(n)=4T(n/2)+n,

substitute the induction hypothesis:

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

Step 4: Simplify

Since

(n2)2=n24,\left(\frac n2\right)^2=\frac{n^2}{4},

we obtain

T(n)≤4(cn24)+n=cn2+n.\begin{aligned} T(n) &\le 4\left(c\frac{n^2}{4}\right)+n\\ &= cn^2+n. \end{aligned}

Step 5: Problem Encountered

We want to prove

T(n)≤cn2,T(n)\le cn^2,

but we obtained

T(n)≤cn2+n.T(n)\le cn^2+n.

The extra +n+n term prevents us from completing the proof.

This is exactly the type of situation discussed  under "A trick of the trade: subtracting a low-order term."

The induction hypothesis is not strong enough.


Step 6: Strengthen the Induction Hypothesis

Instead of assuming

T(n)≤cn2,T(n)\le cn^2,

assume

T(n)≤cn2−dn\boxed{T(n)\le cn^2-dn}

where

d>0.d>0.

Step 7: Substitute Again

Using the stronger hypothesis,

T(n)≤4(c(n2)2−dn2)+n.\begin{aligned} T(n) &\le 4\left(c\left(\frac n2\right)^2-d\frac n2\right)+n. \end{aligned}

Step 8: Simplify

Expanding,

T(n)=4(cn24−dn2)+n=cn2−2dn+n=cn2−(2d−1)n.\begin{aligned} T(n) &= 4\left(c\frac{n^2}{4}-d\frac n2\right)+n\\ &= cn^2-2dn+n\\ &= cn^2-(2d-1)n. \end{aligned}

Step 9: Choose Suitable Constant

We want

T(n)≤cn2−dn.T(n)\le cn^2-dn.

Therefore,

cn2−(2d−1)n≤cn2−dn.cn^2-(2d-1)n \le cn^2-dn.

Subtract cn2cn^2 from both sides:

−(2d−1)n≤−dn.-(2d-1)n\le-dn.

Multiplying by −1-1 (which reverses the inequality),

2d−1≥d.2d-1\ge d.

Hence,

d≥1.d\ge1.

Choose

d=1.\boxed{d=1.}

Thus,

T(n)≤cn2−n,T(n)\le cn^2-n,

which is exactly the strengthened induction hypothesis.


Step 10: Base Case

Suppose

T(1)=k.T(1)=k.

Choose

cc

large enough so that

T(1)≤c−d.T(1)\le c-d.

Hence, the base case is satisfied.


Conclusion

Since the induction step and base case hold,

T(n)≤cn2−dn\boxed{T(n)\le cn^2-dn}

for suitable constants cc and dd.

Therefore,

T(n)=O(n2).\boxed{T(n)=O(n^2).}

Complete Exam-Style Proof

Given

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

Guess

T(n)=O(n2).T(n)=O(n^2).

The initial hypothesis

T(n)≤cn2T(n)\le cn^2

fails because

T(n)≤cn2+n.T(n)\le cn^2+n.

Hence, strengthen the induction hypothesis:

T(n)≤cn2−dn.T(n)\le cn^2-dn.

Assume

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

Then,

T(n)=4T(n/2)+n≤4(cn24−dn2)+n=cn2−2dn+n=cn2−(2d−1)n.\begin{aligned} T(n) &=4T(n/2)+n\\ &\le4\left(c\frac{n^2}{4}-d\frac n2\right)+n\\ &=cn^2-2dn+n\\ &=cn^2-(2d-1)n. \end{aligned}

To satisfy

T(n)≤cn2−dn,T(n)\le cn^2-dn,

we require

2d−1≥d,2d-1\ge d,

which gives

d≥1.d\ge1.

Choose d=1d=1. The base case holds for a sufficiently large cc.

Hence,

T(n)=O(n2).\boxed{T(n)=O(n^2).}

Proving the Tight Bound

To show that the solution is tight, we prove a matching lower bound.

Assume

T(n)≥en2T(n)\ge en^2

for some constant e>0

Then,

T(n)≥4e(n2)2+n=en2+n≥en2.\begin{aligned} T(n) &\ge4e\left(\frac n2\right)^2+n\\ &=en^2+n\\ &\ge en^2. \end{aligned}

Thus,

T(n)=Ω(n2).\boxed{T(n)=\Omega(n^2).}

Combining the upper and lower bounds,

T(n)=Θ(n2).\boxed{T(n)=\Theta(n^2).}

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