Interface DfsIter<V,E>
- Type Parameters:
V
- the vertices typeE
- the edges type
- All Superinterfaces:
Iterator<V>
- All Known Subinterfaces:
DfsIter.Int
The DFS iterator is used to iterate over the vertices of a graph is a depth first manner, namely it explore as far as possible along each branch before backtracking. The iterator will visit every vertex \(v\) for which there is a path from the source(s) to \(v\). Each such vertex will be visited exactly once.
The graph should not be modified during the DFS iteration.
Graph<String, Integer> g = ...;
String sourceVertex = ...;
for (DfsIter<String, Integer> iter = DfsIter.newInstance(g, sourceVertex); iter.hasNext();) {
String v = iter.next();
List<E> edgePath = iter.edgePath();
System.out.println("Reached vertex " + v + " using the edges: " + edgePath.edges());
}
- Author:
- Barak Ugav
- See Also:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V,
E> Graph <V, E> Create a tree from all the vertices and edges traversed by a depth first search.static <V,
E> Graph <V, E> Create a tree from all the vertices and edges traversed by a depth first search, optionally directed or undirected.edgePath()
Get the path from the source to the last vertex returned bynext()
.boolean
hasNext()
Check whether there is more vertices to iterate over.static <V,
E> DfsIter <V, E> newInstance
(Graph<V, E> g, V source) Create a DFS iterator.static DfsIter.Int
newInstance
(IntGraph g, int source) Create a DFS iterator for an int graph.next()
Advance the iterator and return a vertex that was not visited by the iterator yet.Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Method Details
-
hasNext
boolean hasNext()Check whether there is more vertices to iterate over. -
next
V next()Advance the iterator and return a vertex that was not visited by the iterator yet. -
edgePath
Get the path from the source to the last vertex returned bynext()
.The behavior is undefined if
next()
was not called yet.- Returns:
- list of edges forming a path from the source to the last vertex returned by
next()
. The returned list should not be modified
-
newInstance
Create a DFS iterator.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsource
- a vertex in the graph from which the search will start from- Returns:
- a DFS iterator that iterate over the vertices of the graph
- Throws:
NoSuchVertexException
- if the source vertex is not in the graph
-
newInstance
Create a DFS iterator for an int graph.- Parameters:
g
- a graphsource
- a vertex in the graph from which the search will start from- Returns:
- a DFS iterator that iterate over the vertices of the graph
- Throws:
NoSuchVertexException
- if the source vertex is not in the graph
-
dfsTree
Create a tree from all the vertices and edges traversed by a depth first search.The created graph will contain only the vertices reachable from the source vertex. For each such vertex other than the source vertex, the graph will contain the edge that led to it during the search. If there are \(k\) reachable vertices, the graph will contain \(k-1\) edges.
The returned graph will be directed if the original graph is directed. In such case, the tree is directed from the source to the other vertices. To control the directionality of the returned graph, use
dfsTree(Graph, Object, boolean)
.If an
IntGraph
is passed as an argument,IntGraph
is returned.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsource
- a vertex in the graph from which the search will start from- Returns:
- a tree graph that contains all the vertices and edges traversed by a depth first search rooted at the source vertex
- Throws:
NoSuchVertexException
- if the source vertex is not in the graph
-
dfsTree
Create a tree from all the vertices and edges traversed by a depth first search, optionally directed or undirected.The created graph will contain only the vertices reachable from the source vertex. For each such vertex other than the source vertex, the graph will contain the edge that led to it during the search. If there are \(k\) reachable vertices, the graph will contain \(k-1\) edges.
If an
IntGraph
is passed as an argument,IntGraph
is returned.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphsource
- a vertex in the graph from which the search will start fromdirected
- iftrue
the returned tree will be directed. If the original graph was undirected and a directed tree is created, the edges in the tree will be directed from the source towards the other vertices- Returns:
- a tree graph that contains all the vertices and edges traversed by a depth first search rooted at the source vertex
- Throws:
NoSuchVertexException
- if the source vertex is not in the graph
-