I was recently solving Towers of Hanoi problem. I used a “Divide and Conquer” Strategy to solve this problem. I divided the main problem into three smaller sub problems and thus following recurrence was generated.
T(n)=2T(n-1)+1
Solving this leads to
O(2^n) [exponential time]
Then i tried to use memoization technique to solve it, but here too the space complexity was exponential and heap space exhausted very soon and problem was still unsolvable for larger n.
Is there a way to solve the problem in less than exponential time? What is the best time in which the problem can be solved?
It depends what you mean by “solved”. The Tower of Hanoi problem with 3 pegs and
ndisks takes2**n - 1moves to solve, so if you want to enumerate the moves, you obviously can’t do better thanO(2**n)since enumeratingkthings isO(k).On the other hand, if you just want to know the number of moves required (without enumerating them), calculating
2**n - 1is a much faster operation.Also worth noting, the enumeration of the moves can be done iteratively with
O(n)space complexity as follows (disk1is the smallest disk):