basic
Class DepthFirstSearch<T>

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

public class DepthFirstSearch<T>
extends Search<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:

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 DepthFirstSearch(problem, false);
 

Author:
eden06

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

problem

protected Problem<T> problem
holds a reference to the problem to be solved


hash

protected java.util.Set<T> hash
holds the set used for duplicate handling

Constructor Detail

DepthFirstSearch

public DepthFirstSearch(Problem<T> problem)
Creates a new DepthFirstSearch with the given problem. The search will use implicit duplicate handling.

Parameters:
problem - problem the core.Poblem to be solved

DepthFirstSearch

public DepthFirstSearch(Problem<T> problem,
                        boolean noHash)
Create a new DepthFirstSearch with the given problem and disabled hashing if the noHash flag is true.

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

branchedNodes

public final int branchedNodes()
This method returns the number of expanded nodes during the search. Note: This method is a alias for neededSteps() and has the same result.

Returns:
the number of expanded nodes

canPrepare

protected boolean canPrepare()
This method checks if the given problem returns a valid initial state.

Specified by:
canPrepare in class Search<T>
Returns:
true if the search can be prepared
See Also:
Search.canPrepare()

prepare

protected void prepare()
This method clears the hash used for duplicate handling to prepare it for a new search run.

Specified by:
prepare in class Search<T>
See Also:
Search.prepare()

search

protected final 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.
Note: A more accurate description is given in basic.DepthFirstSearch.

Specified by:
search in class Search<T>
See Also:
Search.search()

getProblem

public final Problem<T> getProblem()
This method returns the problem, with which this search has been created.

Returns:
the problem to be solved