aima.core.search.uninformed.DepthFirstSearch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
The newest version!
package aima.core.search.uninformed;
import java.util.List;
import aima.core.agent.Action;
import aima.core.search.framework.Metrics;
import aima.core.search.framework.Node;
import aima.core.search.framework.NodeExpander;
import aima.core.search.framework.QueueFactory;
import aima.core.search.framework.SearchForActions;
import aima.core.search.framework.SearchForStates;
import aima.core.search.framework.SearchUtils;
import aima.core.search.framework.problem.Problem;
import aima.core.search.framework.qsearch.QueueSearch;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): page 85.
*
* Depth-first search always expands the deepest node in the current frontier of
* the search tree.
*
* Note: Supports TreeSearch, GraphSearch, and BidirectionalSearch. Just
* provide an instance of the desired QueueSearch implementation to the
* constructor!
*
* @author Ravi Mohan
* @author Ruediger Lunde
*
*/
public class DepthFirstSearch implements SearchForActions, SearchForStates {
QueueSearch implementation;
public DepthFirstSearch(QueueSearch impl) {
implementation = impl;
}
@Override
public List findActions(Problem p) {
implementation.getNodeExpander().useParentLinks(true);
Node node = implementation.findNode(p, QueueFactory.createLifoQueue());
return node == null ? SearchUtils.failure() : SearchUtils.getSequenceOfActions(node);
}
@Override
public Object findState(Problem p) {
implementation.getNodeExpander().useParentLinks(false);
Node node = implementation.findNode(p, QueueFactory.createLifoQueue());
return node == null ? null : node.getState();
}
@Override
public NodeExpander getNodeExpander() {
return implementation.getNodeExpander();
}
@Override
public Metrics getMetrics() {
return implementation.getMetrics();
}
}