简体   繁体   中英

Unrolling Method For: T(n) = 1 when n = 0 and 2T(n-1) + 1

For

  • T(n) = 1 when n = 0
  • T(n) = 2T(n-1) + 1 otherwise

I know we're supposed to look for patterns and understand the problem down till we start transforming the equations with different variables. However, once I get there, I don't understand how it is done and why certain things are done.

My issue is specifically where we replace i with n at 2 i ·T(n-1). However, a full explanation would be useful as well!

展开方法的图片

It the same way that 99999 10 is 100000 10 - 1, 11111 2 is 100000 2 - 1.

There are two ways to look at this problem. One is from some arbitrary n working backwards, which your description already covers. Another way that I've found helpful is starting from zero and working up to n :

  • T(0) = 1
  • T(1) = 1 << 1 + 1 = 3
  • T(2) = 3 << 1 + 1 = 7
  • ...

Here << is the "left shift" operator on binary numbers, which is equivalent to multiplication by 2. This operator is fairly common in many programming languages. It helps show that you are just adding a 1 bit at every step of the way. Going back to the first assertion, n consecutive one bits are equivalent to 2 n + 1 - 1.

The explanation in your question just uses i as a counter that runs from 1 to n . It's not part of the equation except as the step counter. In the last step, i = n , hence the conversion you may be confused about.

When first seeing the formulas I had the same problem as you. But let's start from the beginning.
At first you try to approach the final depth n step by step through i . So for each number i you resolve the Formula i times (in the picture for i=1 , i=2 , i=3 , i=4 ), but beware you can't resolve T(ni) yet. But you can derive the pattern for i=n . This line actually starts with the error: the picture says i=ni but it should be actually i=n since you reached the bottom.
And the rest is pretty straight forward: you write down the pattern you see and put it in a Sum Formula ( Sum_{j=0}^{i-1}2^j ). You substitute i with n since this is the premise of the line. You substitute T(nn) with T(0)=1 . Next you apply the Finite Geometric Series and therefore get rid of the sum symbol. The rest should be clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM