public class Dfs extends Object
A depth first search starts from a specified node and traverses the neighbors within a branch as deeply as possible (i.e., without visiting any vertex twice) before backtracking.
A depth first search traversal induces a DFS forest that may consist of more than one DFS tree. Based on these trees, the edges are divided into four categories (see example):
During a DFS run each node of the graph can be in one of the following states:
Graph algorithms, which are based on a depth first search, can extend this class and override appropriate callback methods provided by this class.
Example of a DFS traversal. Node labels indicate the order in which nodes are visited. Solid edges are the edges of the DFS tree while dashed edges represent back, forward and cross edges.
Modifier and Type | Field and Description |
---|---|
protected static Object |
BLACK
A constant indicating a node has been completed.
|
protected static Object |
GRAY
A constant indicating a node has already been visited, but has not been completed yet.
|
protected INodeMap |
stateMap
|
protected static Object |
WHITE
A constant indicating a node has not been visited yet.
|
Constructor and Description |
---|
Dfs()
Creates a new
Dfs instance with default settings. |
Modifier and Type | Method and Description |
---|---|
protected void |
cancel()
Cancels the depth first search.
|
protected void |
lookFurther(Node v)
Callback method that will be invoked whenever DFS continues its search at a new root node.
|
protected void |
postTraverse(Edge edge,
Node node)
Callback method that will be invoked after the DFS has returned from the given node.
|
protected void |
postVisit(Node node,
int dfsNumber,
int compNumber)
Callback method that will be invoked whenever a node visit has been completed.
|
protected void |
preTraverse(Edge edge,
Node node,
boolean treeEdge)
Callback method that will be invoked if the given edge will be considered the first (and only) time during the DFS.
|
protected void |
preVisit(Node node,
int dfsNumber)
Callback method that will be invoked whenever a formerly unvisited node gets visited for the first time.
|
void |
setDirectedMode(boolean value)
Sets whether or not to interpret the edges of the graph as directed.
|
void |
setLookFurtherMode(boolean value)
Sets whether or not to continue the depth first search after all nodes reachable from the first node have been visited.
|
void |
start(Graph graph)
Starts a depth first search on the given graph.
|
void |
start(Graph graph,
Node start)
Starts a depth first search from a given
Node of the input graph. |
protected static final Object BLACK
Such a node has been visited before and is no longer part of an active path of the DFS tree.
protected static final Object GRAY
Such a node is still part of an active path of the DFS tree.
protected INodeMap stateMap
protected static final Object WHITE
public Dfs()
Dfs
instance with default settings.protected void cancel()
It may be overridden to support custom implementations.
protected void lookFurther(Node v)
By default, this method does nothing. It may be overridden to support custom implementations.
v
- the new root nodeprotected void postTraverse(Edge edge, Node node)
By default, this method does nothing. It may be overridden to support custom implementations.
edge
- the given edgenode
- the node that has been reached via the given edgeprotected void postVisit(Node node, int dfsNumber, int compNumber)
By default, this method does nothing. It may be overridden to support custom implementations.
node
- the given nodedfsNumber
- the DFS number of the given nodecompNumber
- the completion number of the given nodeprotected void preTraverse(Edge edge, Node node, boolean treeEdge)
By default, this method does nothing. It may be overridden to support custom implementations.
edge
- the given edgenode
- the node to be visited next only if treeEdge == true
treeEdge
- true
if the node
will be visited, false
otherwiseprotected void preVisit(Node node, int dfsNumber)
By default, this method does nothing. It may be overridden to support custom implementations.
node
- the given nodedfsNumber
- the DFS number of the given nodepublic void setDirectedMode(boolean value)
false
. Graphs are considered undirected.value
- true
if the edges are considered as directed, false
otherwisepublic void setLookFurtherMode(boolean value)
true
. value
- true
if the depth first search should continue, false
otherwisepublic void start(Graph graph)
The first node of the graph will be visited first.
graph
- the input graph