I’m working on a programming assignment (in java) to solve a fifteen-puzzle sort of thing. The first part is to use depth first search to find a solution. I want it to be able to solve an arbitrarily large puzzle, so the whole state space graph may not fit in memory. Instead, the algorithm will keep a list of explored states to check future states against. The algorithm goes something like this:
If we’re in the goal state, then we’re done.
If not, then for each neighbor that is not in the explored list, add it to the explored list and recursively depth first search it.
However, it’s not working properly. I did some tests and I believe the problem is that when I insert a state into the explored list, it actually inserts the same object, so that when I change the state later the explored list changes as well. Because of this, it never appears to the program that a state has not been explored.
I tried to write a method to copy states, and pass a copy to the explored list; but the same problem persists. However, when I tested the copy method independently, it appears to successfully copy states. I’m totally baffled as to what the problem is.
There’s a lot of code involved, and I’m not sure where the problem is, but here’s the code for the depth first search:
this class we're in is the puzzle class, and this is the depth first search
'state' keeps track of the current state we're in
'explored' is a linked list of explored states
public String depthFirst(){
state.clearPath(); // state keeps track of the path taken
explored = new LLNode(state); // create explored list with starting state
this.dfs(); // depth first search
return state.path();
}
private boolean dfs(){
if(state.equals(goal)) // if we're in the goal state, then done
return true;
char[] directions = {'n', 'e', 's', 'w'};
for(char dir : directions){ // otherwise, for each direction
if(state.move(dir)){ // if we can move in that direction, then do
if(!explored.contains(state)){ // if the new state is unexplored
explored = explored.insert(state.copy());
if(this.dfs()) // add it to explored and recurse
return true;
}
switch(dir){ // if search failed, move back and
// search the next direction
case 'n': state.move('s');
case 'e': state.move('w');
case 's': state.move('n');
case 'w': state.move('e');
}
}
}
return false; // indicates failure
}
Here are the insert and contains methods for explored:
public LLNode insert(Node gameState){
if(state == null){ // if this list node doesn't have a state
state = gameState; // then give it this one
return this;
} // otherwise tack it on the front of the list
return new LLNode(gameState, this); // and return the new head
}
public boolean contains(Node gameState){
// I had a test here that printed a message if (state == gameState)
// which was triggered when I ran the program, suggesting the problem
// I explained above
if(state.equals(gameState))
return true;
else if(next == null)
return false;
else
return next.contains(gameState);
}
Here is the copy function for the Node class
public Node copy(){
int[][] newState = new int[state.length][state[0].length];
for(int i = 0; i < state.length; ++i)
for(int j = 0; j < state[0].length; ++j)
newState[i][j] = state[i][j];
return new Node(newState);
}
Thanks for any ideas!
I’d check that
state.move()doesn’t referenceexploredat all – looking at the supplied code, that’s the only place I can imagine your variables could be referencing the same arrays.Was it a requirement of the assignment to roll your own data structures (i.e. Linked list)? If not then I’d go with a standard Java
HashSet(or whatever best suits your requirements), as I cannot see the need to remember the ordering of the search, only the fact that you’ve already explored a particular state.Also, is it necessary to store the whole array for every explored state? It might be better (consume less memory and be generally more efficient) to just remember a hash for each explored state (which you could store in a
HashSet).Note: Your
copy()method assumes an NxN array (which is fine for your example, but wouldn’t work if the puzzle was rectangular).