Package com.jgalgo

Interface DFSIter

  • All Superinterfaces:
    IntIterator, Iterator<Integer>, PrimitiveIterator<Integer,​IntConsumer>, PrimitiveIterator.OfInt

    public interface DFSIter
    extends IntIterator
    Depth first search (DFS) iterator.

    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 g = ...;
     int sourceVertex = ...;
     for (DFSIter iter = DFSIter.newInstance(g, sourceVertex); iter.hasNext();) {
         int v = iter.nextInt();
         IntList edgePath = iter.edgePath();
         System.out.println("Reached vertex " + v + " using the edges: " + edgePath);
     }
     
    Author:
    Barak Ugav
    See Also:
    BFSIter, Wikipedia
    • Method Detail

      • newInstance

        static DFSIter newInstance​(Graph g,
                                   int source)
        Create a DFS iterator rooted at some source vertex.
        Parameters:
        g - a graph
        source - a vertex in the graph from which the search will start from
        Returns:
        a DFS iterator that iterate over the vertices of the graph
      • hasNext

        boolean hasNext()
        Check whether there is more vertices to iterate over.
        Specified by:
        hasNext in interface Iterator<Integer>
      • edgePath

        IntList edgePath()
        Get the path from the source to the last vertex returned by nextInt().

        The behavior is undefined if nextInt() was not called yet.

        Returns:
        list of edges forming a path from the source to the last vertex returned by nextInt(). The returned list should not be modified by the user.