|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javautils.graph.templates.BfsTemplate
An abstract Template Method [Gamma1995] for breadth-first search [Cormen2001].
Note:
search
-method.This example shows how the BFS template could be used to compute a shortest path between two nodes. The following algorithm is asymptotically optimal in the worst case.
// First we need a method to compute an inverted BFS-tree representation. public static Map mapOfInvertedBfsTree(finalGraph
graph, List roots) { // Here we define a map that holds the inverted BFS-tree. final Map nodeToParentMap = new HashMap(); // Here we specialize an algorithm from the BFS template... new BfsTemplate() { // For each tree edge, we update the inverted BFS-tree. protected voidtreeEdge
(Object edge) { nodeToParentMap.put(graph.targetOf
(edge), graph.sourceOf
(edge)); } }.search
(graph, roots); // ...and above we invoke the specialized template, searching // only the specified roots. return nodeToParentMap; } // The actual shortest path finding method. public static List shortestUnweightedPath(Graph
graph, Object source, final Object target) { final Map nodeToParentMap = mapOfInvertedBfsTree(graph, Collections.singletonList(source)); // Is there a path? return nodeToParentMap.containsKey(target) ? newUnfold
() { // Here we traverse the inverted BFS-tree starting at the // target node towards the source node and construct the path. Object work = target; protected booleanmore
() {return null != work;} protected Objectvalue
() {return work;} protected voidadvance
() {work = nodeToParentMap.get(work);} }.unfoldRight
() : null; }
Constructor Summary | |
BfsTemplate()
|
Method Summary | |
protected void |
discoverNode(java.lang.Object node)
Called once for each discovered node either after the node has been discovered as a root or after all edges in the previous level of the search tree have been examined, but before any edges in the current level are examined. |
protected void |
discoverRoot(java.lang.Object node)
Called once for each node that is first encountered from the root list. |
protected void |
finishNode(java.lang.Object node)
Called once for each node in the searh tree after all edges from the node have been examined but before any nodes on the next level of the search tree are examined. |
protected void |
finishRoot(java.lang.Object node)
Called once for each discovered root node after the entire search tree rooted at the node has been examined. |
protected void |
initNode(java.lang.Object node)
Called once for each node before the search. |
protected void |
nonTreeEdge(java.lang.Object edge)
Called for each edge that is not part of the search tree after discovering all the nodes in the current level of the search tree, but before any nodes in the next level are discovered. |
void |
search(Graph graph)
Performs breadth-first search on the given graph and calls the event point methods. |
void |
search(Graph graph,
java.util.Collection rootOrder)
|
void |
search(Graph graph,
java.util.Iterator roots)
Performs breadth-first search on the given graph, examines root nodes in the given sequence, and calls the event point methods. |
void |
search(Graph graph,
java.lang.Object rootNode)
|
protected void |
treeEdge(java.lang.Object edge)
Called for each search tree edge after discovering all the nodes in the current level of the search tree, but before any nodes in the next level are discovered. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public BfsTemplate()
Method Detail |
protected void initNode(java.lang.Object node)
Called once for each node before the search.
protected void discoverRoot(java.lang.Object node)
Called once for each node that is first encountered from the root list.
protected void finishRoot(java.lang.Object node)
Called once for each discovered root node after the entire search tree rooted at the node has been examined.
protected void discoverNode(java.lang.Object node)
Called once for each discovered node either after the node has been discovered as a root or after all edges in the previous level of the search tree have been examined, but before any edges in the current level are examined.
protected void finishNode(java.lang.Object node)
Called once for each node in the searh tree after all edges from the node have been examined but before any nodes on the next level of the search tree are examined.
protected void treeEdge(java.lang.Object edge)
Called for each search tree edge after discovering all the nodes in the current level of the search tree, but before any nodes in the next level are discovered.
protected void nonTreeEdge(java.lang.Object edge)
Called for each edge that is not part of the search tree after discovering all the nodes in the current level of the search tree, but before any nodes in the next level are discovered.
public final void search(Graph graph)
Performs breadth-first search on the given graph and calls the event point methods.
public final void search(Graph graph, java.util.Iterator roots)
Performs breadth-first search on the given graph, examines root nodes in the given sequence, and calls the event point methods.
public final void search(Graph graph, java.util.Collection rootOrder)
public final void search(Graph graph, java.lang.Object rootNode)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |