Hello all -
I am writing a Depth First Search program in java for the eight puzzle problem. I have completed the coding but for some reason I am not able to print the exact depth at which nodes are being searched. Here is two of the methods that does majority of the processing in my code:
This code is the main function which has a hash table (closedList) that holds all the nodes that have been visited. The openList Stack class object holds all the nodes that currently need to be expanded.
The given criteria is that the traversals have to be done strictly in the UP, LEFT, RIGHT and DOWN directions. In DFS, calling the functions up(), left(), right() and down() in reverse directions will help me in getting the desired output.
The functions down(), right(), left() and up(), call the addNewNode() function to add the newly created nodes into the openList stack so that the nodes can be traversed by popping out elements.
I am pointing to the following line:
System.out.print(", depth: " + storeDepth);
Can someone please help me out?
poppedNode = state;
addNewNode(state, 0);
closedList.put(poppedNode, 0);
while (!openList.isEmpty())
{
poppedNode = openList.pop();
linearList.add(poppedNode);
if(!closedList.containsKey(poppedNode))
closedList.put(poppedNode, storeDepth);
int nodeSearched = closedList.size()-1;
if ((verbosity == 0) || (nodeSearched%verbosity == 0))
{
System.out.print("{time: " +time);
System.out.print(", depth: " + storeDepth);
System.out.print(", nodes searched: " + nodeSearched);
System.out.print(", current: " + "\"");
for (int i=0; i<poppedNode.length(); i++)
System.out.print(poppedNode.substring(i,i+1) + ",");
System.out.println("\"}");
}
if(poppedNode.equals(goalNode))
goal(linearList);
down(poppedNode);
right(poppedNode);
left(poppedNode);
up(poppedNode);
}
void addNewNode(String newStr, int depth)
{
// If duplicate node, then return without adding
if(closedList.containsKey(newStr))
return;
openList.add(newStr);
storeDepth = depth;
}