|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcore.Search<T>
basic.DepthFirstSearch<T>
T
- the specific type of all elements in the search domain.public class DepthFirstSearch<T>
The depth first search algorithm is a blind search able
to solve any problem implementing the core.Problem interface.
This class is a subclass of core.Search because it uses
recursion rather then the general search algorithm employed in core.DirectedSearch.
The depth first search uses a recursive function to expand all states
in the search tree. This function calls itself for each successor of the given state, and
terminates only if a goal has been found or all successors have been explored.
This strategy grants a time complexity of O(b^d)
and
a space complexity of O(b*d)
(where b is the branching factor and d the depth of the search tree).
In contrast to other algorithms depth first search is complete only if
there are no infinite branches in the search domain.
In most cases this is no problem because we can limit the maximal search depth
by using the basic.DepthLimitedSearch.
Despite the fact that this strategy can not generate optimal solutions,
it can solve big problems in a memory saving way.
In general this algorithm is only applicable to search domains with a finite
search tree, because otherwise this algorithm would not terminate.
Note: You should prefer basic.IterativeDeepeningSearch whenever
you need an optimal solution or have to deal with an infinite search tree.
To avoid duplicate states during the search a HashSet is used,
which tries to hash all expanded states in order to inhibit further expansion.
In detail there are two important rules for states of type T which allow proper hashing:
(e.hashCode()==t.hashCode()) and (e.equals(t))
implies e==t
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 DepthFirstSearch(problem, false);
Field Summary | |
---|---|
protected java.util.Set<T> |
hash
holds the set used for duplicate handling |
protected Problem<T> |
problem
holds a reference to the problem to be solved |
Fields inherited from class core.Search |
---|
neededSteps, result |
Constructor Summary | |
---|---|
DepthFirstSearch(Problem<T> problem)
Creates a new DepthFirstSearch with the given problem. |
|
DepthFirstSearch(Problem<T> problem,
boolean noHash)
Create a new DepthFirstSearch with the given problem and disabled hashing if the noHash flag is true. |
Method Summary | |
---|---|
int |
branchedNodes()
This method returns the number of expanded nodes during the search. |
protected boolean |
canPrepare()
This method checks if the given problem returns a valid initial state. |
Problem<T> |
getProblem()
This method returns the problem, with which this search has been created. |
protected void |
prepare()
This method clears the hash used for duplicate handling to prepare it for a new search run. |
protected void |
search()
This method looks for a goal in the search domain, by calling a recursive function which expands a state and calls itself for all successor states. |
Methods inherited from class core.Search |
---|
finalize, getResult, initialize, initialized, neededSteps, run, running, stop |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected Problem<T> problem
protected java.util.Set<T> hash
Constructor Detail |
---|
public DepthFirstSearch(Problem<T> problem)
problem
- problem the core.Poblem to be solvedpublic DepthFirstSearch(Problem<T> problem, boolean noHash)
problem
- the core.Poblem to be solvednoHash
- flag indicating that duplicates should not be handledMethod Detail |
---|
public final int branchedNodes()
protected boolean canPrepare()
canPrepare
in class Search<T>
Search.canPrepare()
protected void prepare()
prepare
in class Search<T>
Search.prepare()
protected final void search()
search
in class Search<T>
Search.search()
public final Problem<T> getProblem()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |