basic
Class BreadthFirstSearch<T>

java.lang.Object
  extended by core.Search<T>
      extended by core.DirectedSearch<T>
          extended by basic.BreadthFirstSearch<T>
Type Parameters:
T - the specific type of all elements in the search domain.
All Implemented Interfaces:
java.lang.Runnable

public class BreadthFirstSearch<T>
extends DirectedSearch<T>

The breadth first search algorithm is a blind search able to solve any problem implementing the core.Problem interface.

The breadth first search starts at depth zero and expands all states with the same depth in the search tree before it does the same in the next depth.
This strategy grants completeness and a time and space complexity of O(b^n) (where b is the branching factor and n the depth of the solution).
An optimal solution can be found only if the path costs between two states are constant or increase by a function of the depth.

In general this algorithm is only applicable for small problem domains with a small branching factor, because of the large amount of space needed during execution.
Note: You should prefer basic.IterativeDeepeningSearch whenever the BreadthFirstSearch exceeds the available amount of space.

In some cases it is necessary to turn of the implicit duplicate handling mechanism. For example there are no duplicate states in the search domain, or special knowledge about the search domain makes customized duplicate handling more feasible.
The following example shows how to implement a duplicate handling mechanism and how to disable the implicit duplicate handling mechanism:

 
 class MyProblem<T> extends Problem<T>{
        private Set<Integer> visited=new HashSet<Integer>();
     
        public List expand(T state){
                List result = new LinkedList();
                for (T newState : super.expand(state) ){
                        if ( visited.add( newState.hashCode() ) ){
                                result.add(newState);
                        }
                }
                return result;
        }
     
 } 
 
 // ...
 
 Problem problem = new MyProblem();
 //Turn of the implicit duplicate handling mechanism
 Search search = new BreadthFirstSearch(problem, false);
 

Author:
eden06

Field Summary
 
Fields inherited from class core.DirectedSearch
hash, problem
 
Fields inherited from class core.Search
neededSteps, result
 
Constructor Summary
BreadthFirstSearch(Problem<T> problem)
          This method creates a new BreadthFirstSearch where implicit duplicate handling is enabled.
BreadthFirstSearch(Problem<T> problem, boolean noHash)
          This method creates a new BreadthFirstSearch.
 
Method Summary
protected  boolean add(T state)
          This method is the hook for adding new states to the queue during the search.
protected  void clear()
          This method is the hook for clearing the queue on preparation of a new search.
protected  boolean empty()
          This method is the hook for the check if the queue is empty during the search.
protected  T pop()
          This method is the hook for extracting the first element of the queue during the search.
 
Methods inherited from class core.DirectedSearch
branchedNodes, canPrepare, finalize, getProblem, prepare, search
 
Methods inherited from class core.Search
getResult, initialize, initialized, neededSteps, run, running, stop
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BreadthFirstSearch

public BreadthFirstSearch(Problem<T> problem)
This method creates a new BreadthFirstSearch where implicit duplicate handling is enabled.

Parameters:
problem - to be solved

BreadthFirstSearch

public BreadthFirstSearch(Problem<T> problem,
                          boolean noHash)
This method creates a new BreadthFirstSearch. According to the given flag the search will turn off the internal duplicate handling mechanism or not.

Parameters:
problem - to be solved
noHash - flag indicating that duplicates should not be handled
Method Detail

add

protected boolean add(T state)
Description copied from class: DirectedSearch
This method is the hook for adding new states to the queue during the search.
After calling this method the state should be added to the queue.
Note: Most implementations do not check whether a state has been successfully added or not.

Specified by:
add in class DirectedSearch<T>
Parameters:
state - to be added to the queue
Returns:
true only if this state has been added.
See Also:
DirectedSearch.add(java.lang.Object)

clear

protected void clear()
Description copied from class: DirectedSearch
This method is the hook for clearing the queue on preparation of a new search.
It should remove all elements of the queue.

Specified by:
clear in class DirectedSearch<T>
See Also:
DirectedSearch.clear()

empty

protected boolean empty()
Description copied from class: DirectedSearch
This method is the hook for the check if the queue is empty during the search.
It should return true whenever the queue is empty.

Specified by:
empty in class DirectedSearch<T>
Returns:
true only if the queue is empty.
See Also:
DirectedSearch.empty()

pop

protected T pop()
Description copied from class: DirectedSearch
This method is the hook for extracting the first element of the queue during the search.
It should return and remove the first element of the queue.
Note: The implementation assures that this method will only be called if empty() returned false.

Specified by:
pop in class DirectedSearch<T>
Returns:
the first element of the queue
See Also:
DirectedSearch.pop()