简体   繁体   中英

Best Case time complexity of Tower of Hanoi algorithm

I want to know the best case complexity of Tower of Hanoi algorithm. The algorithm that I used is Algorithm

I have calculated the time complexity and it is T(2^n -1) and Big O is O(n). But what is the best case complexity and how to calculate it?

You have incorrectly calculated the time complexity.

The correct recurrence can be represented by:

T(n) = 2*T(n-1) + 1.... eq(1)

T(n) = (2^2)*T(n-2) + 1 + 2.... eq(2)

T(n) = (2^3)*T(n-3) + 1 + 2 + 2^2....eq(3)

Therefore,

T(n) = (2^k)*T(nk) + (2^0) + (2^1) + (2^2) +.... + (2^(k-1)).... eq(4)

Now, substituting, nk = 1 in eq(4), we get,

T(k+1) = (2^k)*T(1) + ((2^k) + 1)

Substituting T(1) = 1 , we get,

T(k+1) = 2^k + 2^k + 1 = 2^(k+1) + 1... eq(5)

Finally, substituting k+1 = n in eq(5) to get the Closed Form:

T(n) = 2^n + 1

Now, the answer to your question:

The algorithm takes O(2^n) just to print out the steps, therefore the best case time complexity also remains exponential, ie, O(2^n).

Therefore, you cannot find any better algorithm.

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