4

I want to solve the "Towers of Hanoi" problem by using a good "state space". Using an appropriate state space is a way that is suggested by some AI techniques. Having a good state space, I would then like to be able to build a search tree and then use some strategy like "DFS" (depth-first-search) to find a solution.

Edit: My problem is, I just don't know how to develop a good state space and then use it to build a search tree. Can anyone describe how to create a state space for the Tower of Hanoi problem? Then tell me how to build a search tree for that.

Darren
  • 138
  • 1
  • 8
Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80

4 Answers4

7

I suggest the following state space:

Assuming you have n bricks and 3 towers denoted by 0,1,2. Denote the current state by a n trinary numbers, for example (in the case n=9):

987654321
001102020 (current state)

meaning that brick 9,8,5,3 and 1 are in the 0:th tower. Brick 7 and 6 in the 1:th tower and brick 4 and 2 in the 2:nd tower.

This would give you a state space of size 3^n, which is not too large.

(This is only a partial answer. But every state-string will correspond to a legal state. That is to say,

  1. in each tower the size of the bricks will decrase from bottom to top,

  2. no brick will appear in two different towers.

I therefore think that the suggested state space is minimal. )

ragnarius
  • 5,642
  • 10
  • 47
  • 68
1

I think You can easily solve this problem using Divide and Conquer approach: Assume that U have to solve the problem of moving n discs from src to dest using some auxiliary peg. U can recursively define a function:

towers(n,src,dest,peg)
{
    if(n==1) //BASE CASE
        move a disc from src to dest. 

   else  //INDUCTIVE CASE
   {
    towers(n-1,src,aux,dest);
    towers(1,src,dest,aux);
    towers(n-1,aux,dest,src)
   }
}

Complexity Analysis: T(n)=2T(n-1)+1

This results in the solution T(n)=O(2^n) [exponential order].

May be u can also employee some kind of memoization to store the solution of already solved subproblems to further improve the time complexity, but this is a trade off for increased usage of space complexity.

1

I think optimal solution (2^n - 1)

state space is given by 3^n

Here is another link with tree diagram that you can use to count the state (I think this relates to you question about state space)

Community
  • 1
  • 1
surajz
  • 3,471
  • 3
  • 32
  • 38
1

2^(n+1)-1 is not correct for the towers of hanoi problem. If you look at figure 2 here, then when applying n=3 to 2^(n+1)-1 gives 2^4 - 1 or 15 states. But figure 2 shows 27 states.

muki61
  • 41
  • 2