Question #224904

Solve the recurrence by substitution method.
T(n) = 2T (n/2)+n-1 and T (1) =1.

Expert's answer

Let's make a few substitutions to understand the properties of the desired sequence.



T(n)=2T(n/2)+nβˆ’1⟢{nβ†’n/2:T(n/2)=2T(n/4)+n/2βˆ’1nβ†’n/4:T(n/4)=2T(n/8)+n/4βˆ’1nβ†’n/8:T(n/8)=2T(n/16)+n/8βˆ’1nβ†’n/16:T(n/16)=2T(n/32)+n/16βˆ’1T(n)=2T\left(n/2\right)+n-1\longrightarrow\\[0.3cm] \left\{\begin{array}{l} n\to n/2 :\quad T\left(n/2\right)=2T\left(n/4\right)+n/2-1\\[0.3cm] n\to n/4 :\quad T\left(n/4\right)=2T\left(n/8\right)+n/4-1\\[0.3cm] n\to n/8 :\quad T\left(n/8\right)=2T\left(n/16\right)+n/8-1\\[0.3cm] n\to n/16 :\quad T\left(n/16\right)=2T\left(n/32\right)+n/16-1 \end{array}\right.

To move forward, we need to understand how T(n)T(n) is expressed through T(n/2),   T(n/4),…T(n/2),\,\,\,T(n/4),\ldots Therefore,



T(n)=21β‹…T(n21)+1β‹…nβˆ’1⏟=20⟢T(n)=2β‹…(2β‹…T(n4)+n/2βˆ’1)+1β‹…nβˆ’1==4β‹…T(n4)+1β‹…nβˆ’2+1β‹…nβˆ’1⟢T(n)=22β‹…T(n22)+2β‹…nβˆ’(20+21)T(n)=4β‹…(2β‹…T(n8)+n/4βˆ’1)+2β‹…nβˆ’(1+2)==8β‹…T(n8)+nβˆ’4+2β‹…nβˆ’(1+2)⟢T(n)=23β‹…T(n23)+3β‹…nβˆ’(20+21+22)T(n)=8β‹…(2β‹…T(n16)+n/8βˆ’1)+3β‹…nβˆ’(20+21+22)==16β‹…T(n16)+nβˆ’8+3β‹…nβˆ’(20+21+22)⟢T(n)=24β‹…T(n24)+4nβˆ’(20+21+22+23)T(n)=16β‹…(2β‹…T(n32)+n/16βˆ’1)+4β‹…nβˆ’(20+21+22+23)==32β‹…T(n32)+nβˆ’16+4β‹…nβˆ’(20+21+22+23)⟢T(n)=25β‹…T(n25)+5nβˆ’(20+21+22+23+24)\boxed{T(n)=2^1\cdot T\left(\frac{n}{2^1}\right)+1\cdot n-\underbrace{1}_{=2^0}}\longrightarrow\\[0.3cm] T(n)=2\cdot\left(2\cdot T\left(\frac{n}{4}\right)+n/2-1\right)+1\cdot n-1=\\[0.3cm] =4\cdot T\left(\frac{n}{4}\right)+1\cdot n-2+1\cdot n-1\longrightarrow\\[0.3cm] \boxed{T(n)=2^2\cdot T\left(\frac{n}{2^2}\right)+2\cdot n-\left(2^0+2^1\right)}\\[0.3cm] T(n)=4\cdot\left(2\cdot T\left(\frac{n}{8}\right)+n/4-1\right)+2\cdot n-\left(1+2\right)=\\[0.3cm] =8\cdot T\left(\frac{n}{8}\right)+n-4+2\cdot n-\left(1+2\right)\longrightarrow\\[0.3cm] \boxed{T(n)=2^3\cdot T\left(\frac{n}{2^3}\right)+3\cdot n-\left(2^0+2^1+2^2\right)}\\[0.3cm] T(n)=8\cdot\left(2\cdot T\left(\frac{n}{16}\right)+n/8-1\right)+3\cdot n-\left(2^0+2^1+2^2\right)=\\[0.3cm] =16\cdot T\left(\frac{n}{16}\right)+n-8+3\cdot n-\left(2^0+2^1+2^2\right)\longrightarrow\\[0.3cm] \boxed{T(n)=2^4\cdot T\left(\frac{n}{2^4}\right)+4n-\left(2^0+2^1+2^2+2^3\right)}\\[0.3cm] T(n)=16\cdot\left(2\cdot T\left(\frac{n}{32}\right)+n/16-1\right)+4\cdot n-\left(2^0+2^1+2^2+2^3\right)=\\[0.3cm] =32\cdot T\left(\frac{n}{32}\right)+n-16+4\cdot n-\left(2^0+2^1+2^2+2^3\right)\longrightarrow\\[0.3cm] \boxed{T(n)=2^5\cdot T\left(\frac{n}{2^5}\right)+5n-\left(2^0+2^1+2^2+2^3+2^4\right)}\\[0.3cm]

Making a proposal for a general formula :



T(n)=2pβ‹…T(n2p)+npβˆ’(20+21+22+…+2pβˆ’1)\boxed{T(n)=2^p\cdot T\left(\frac{n}{2^p}\right)+np-\left(2^0+2^1+2^2+\ldots+2^{p-1}\right)}


It remains to calculate the sum in brackets, which is a geometric sum with the following parameters: a1=1a_1=1 , r=2r=2 , the number of items in the sum 0…(pβˆ’1)β†’Number=p0\ldots(p-1)\to \text{Number}=p , then



20+21+22+…+2pβˆ’1=a1β‹…(rpβˆ’1)rβˆ’1=2pβˆ’12βˆ’1⟢20+21+22+…+2pβˆ’1=2pβˆ’12^0+2^1+2^2+\ldots+2^{p-1}=\frac{a_1\cdot\left(r^p-1\right)}{r-1}=\frac{2^p-1}{2-1}\longrightarrow\\[0.3cm] \boxed{2^0+2^1+2^2+\ldots+2^{p-1}=2^p-1}

Conclusion,



T(n)=2pβ‹…T(n2p)+npβˆ’(2pβˆ’1)T(n)=2^p\cdot T\left(\frac{n}{2^p}\right)+np-\left(2^p-1\right)

It remains to use the initial condition :



n2p=1β†’n=2pβ†’p=log⁑2n\frac{n}{2^p}=1\to n=2^p\to\boxed{ p=\log_2n}

Then,



T(n)=2log⁑2n⏟=nβ‹…T(1)+nβ‹…log⁑2nβˆ’(2log⁑2n⏟=nβˆ’1)⟢T(n)=nβ‹…1+nβ‹…log⁑2nβˆ’(nβˆ’1)⟢T(n)=nβ‹…log⁑2n+1+O(nβ‹…log⁑2n)T(n)=\underbrace{2^{\log_2n}}_{=n}\cdot T(1)+n\cdot\log_2n-\left(\underbrace{2^{\log_2n}}_{=n}-1\right)\longrightarrow\\[0.3cm] T(n)=n\cdot1+n\cdot\log_2n-(n-1)\longrightarrow\\[0.3cm] \boxed{T(n)=n\cdot\log_2n+1+O\left(n\cdot\log_2n\right)}

ANSWER



T(n)=2T(n/2)+nβˆ’1⟢T(n)=nβ‹…log⁑2n+1+O(nβ‹…log⁑2n)T(n)=2T\left(n/2\right)+n-1\longrightarrow\\[0.3cm] T(n)=n\cdot\log_2n+1+O\left(n\cdot\log_2n\right)


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS