I have been trying to implement an Iterative Deepening Search in Java. However, for some reason, not all of the children, for each node are being visited, resulting in incorrect results. Here is my code so far:
public int IDS(Node start, Node goal){
int depth = 0; //set starting depth to 0
Node current=start; //current node is equal to start
int goalNode=0; //goalNode is originally set to 0
//List<Node> tempList=new ArrayList<Node>();
while(goalNode==0){ //while goalNode is equal to 0
List<Node> visited=new ArrayList<Node>(); //create an array list of nodes
goalNode=DLS(current, goal, depth, visited);
depth++; //increment the depth
}
System.out.println("RESULT");
return goalNode;
}
public int DLS(Node current, Node goal, int depth, List<Node> visited){
if(depth>=0){
if ( current == goal ){ //stop program
System.out.println("REACHED GOAL");
return current.value;
}else{
visited.add(current); //add the current node to visited list (in the beginning =start)
List<Node> temp = Adjacency_List.get(current.value); //get children of current node
for(Node node: temp){ //for each child
System.out.println("Current Node: "+current.value);
System.out.println(current.value + " - " + node.value);
if(node != null && !visited.contains(node)){
//tempList.add(node);
return DLS(node, goal, depth-1, visited);
}
}
}
}else{
return 0;
}
return 0;
}