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
Nested Classes -
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().booleanhasNext()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.IntnewInstance(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
-
next
-
edgePath
-
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
IntGraphis passed as an argument,IntGraphis 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
IntGraphis passed as an argument,IntGraphis 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- iftruethe 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
-