6

I have as a school excersice to implement breadth first search in java. I have implemented almost everything but the problem is that my search is not working and I cant find the problem :( So Im asking you to advice me and give me some guidlines on where the eventual problem could be.

public ArrayList<SearchNode> search(Problem p) {
        // The frontier is a queue of expanded SearchNodes not processed yet
        frontier = new NodeQueue();
        /// The explored set is a set of nodes that have been processed 
        explored = new HashSet<SearchNode>();
        // The start state is given
        GridPos startState = (GridPos) p.getInitialState();
        // Initialize the frontier with the start state  
        frontier.addNodeToFront(new SearchNode(startState));

        // Path will be empty until we find the goal.
        path = new ArrayList<SearchNode>();

        // The start NODE

        SearchNode node = new SearchNode(startState); 

        // Check if startState = GoalState??
        if(p.isGoalState(startState)){
           path.add(new SearchNode(startState)); 
           return path; 
        }


        do {  

          node = frontier.removeFirst();
          explored.add(node); 

          ArrayList reachable = new ArrayList<GridPos>();
          reachable = p.getReachableStatesFrom(node.getState()); 

          SearchNode child; 

          for(int i = 0; i< reachable.size(); i++){

              child = new SearchNode((GridPos)reachable.get(i)); 

              if(!(explored.contains(child) || frontier.contains(child))){
                  if(p.isGoalState(child.getState())){
                      path = child.getPathFromRoot() ;  
                      return path; 
                  }
                  frontier.addNodeToFront(child); 
              }

          }
        }while(!frontier.isEmpty());


        return path;
    }

Thank you

7
  • 1
    How is it not working? Be precise. Commented Sep 23, 2012 at 19:26
  • It's seems that it's exploring the "wrong" nodes and path. Commented Sep 23, 2012 at 19:28
  • You have a lot of methods that you aren't showing us. You seem to be extracting nodes from two different arrays / lists and inserting the reachable nodes to just one. Your condition is also weird, you should only check the explored list, in a classic implementation. The basic idea is: extract the first node from the beginning of a list, add all of its neighbors to the end of the same list. Stop when the list is empty or when you have added the destination node to that list. Commented Sep 23, 2012 at 19:34
  • It's just an assumption but how does your hashCode and equals Methods for SearchNode look like? explored.contains(child) can use both to find the SearchNode and may fail. Commented Sep 23, 2012 at 19:35
  • 1
    I reverted the addNodeToBack back to addNodeToFront - since it is an important issue in this answer, as mentioned by Alex Lynch in his answer. Please do not change it, since it might help future readers who encounter similar problem and won't understand what's wrong after reading the editted question. Commented Sep 23, 2012 at 22:53

2 Answers 2

8
frontier.addNodeToFront(child);

assuming the rest of your code (getReachableStatesFrom(), etc) is correct, adding elements to the front of your queue will cause your code to execute as a depth first search.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you are right. Stupid misstake by me. After changing it to add the nodes to the back, it's seems that it "almost work" :D
@user1285737 if you can identify another place where your code may have an issue, feel free to open another question :) if you believe i've answered this question appropriately, accepting my answer is the preferred way of giving thanks. good luck!
0

For implementing the breadth first search, you should use a queue. This process may become very complicated. So, it is better to keep it simple. You should push the children of a node to the queue (left then right) and then visit the node (print data). Then, yo should remove the node from the queue. You should continue this process till the queue becomes empty. You can see my implementation of the BFS here: https://github.com/m-vahidalizadeh/foundations/blob/master/src/algorithms/TreeTraverse.java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.