|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcore.Search<T>
core.DirectedSearch<T>
basic.BreadthFirstSearch<T>
T
- the specific type of all elements in the search domain.public class BreadthFirstSearch<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);
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 |
---|
public BreadthFirstSearch(Problem<T> problem)
problem
- to be solvedpublic BreadthFirstSearch(Problem<T> problem, boolean noHash)
problem
- to be solvednoHash
- flag indicating that duplicates should not be handledMethod Detail |
---|
protected boolean add(T state)
DirectedSearch
add
in class DirectedSearch<T>
state
- to be added to the queue
DirectedSearch.add(java.lang.Object)
protected void clear()
DirectedSearch
clear
in class DirectedSearch<T>
DirectedSearch.clear()
protected boolean empty()
DirectedSearch
empty
in class DirectedSearch<T>
DirectedSearch.empty()
protected T pop()
DirectedSearch
pop
in class DirectedSearch<T>
DirectedSearch.pop()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |