Package algs42

Class Digraph

java.lang.Object
algs42.Digraph

public class Digraph extends Object
The Digraph class represents an directed graph of vertices named 0 through V-1. It supports the following operations: add an edge to the graph, iterate over all of the neighbors incident to a vertex. Parallel edges and self-loops are permitted.

For additional documentation, see Section 5.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

  • Field Summary Link icon

    Fields
    Modifier and Type
    Field
    Description
    private final Bag<Integer>[]
     
    private int
     
    private int[]
     
    private final int
     
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Digraph(int V)
    Create an empty digraph with V vertices.
    Initializes a new digraph that is a deep copy of the specified digraph.
    Digraph(In in)
    Initializes a digraph from the specified input stream.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    addEdge(int v, int w)
    Add the directed edge v->w to the digraph.
    adj(int v)
    Return the list of vertices pointed to from vertex v as an Iterable.
    int
    E()
    Return the number of edges in the digraph.
    int
    indegree(int v)
    Returns the number of directed edges incident to vertex v.
    static void
    main(String[] args)
    Test client.
    int
    outdegree(int v)
    Returns the number of directed edges incident from vertex v.
    Return the reverse of the digraph.
    void
    toGraphviz(String filename)
    Save a graphviz representation of the graph.
    Return a string representation of the digraph.
    int
    V()
    Return the number of vertices in the digraph.

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details Link icon

    • V Link icon

      private final int V
    • E Link icon

      private int E
    • adj Link icon

      private final Bag<Integer>[] adj
    • indegree Link icon

      private int[] indegree
  • Constructor Details Link icon

    • Digraph Link icon

      public Digraph(int V)
      Create an empty digraph with V vertices.
    • Digraph Link icon

      public Digraph(In in)
      Initializes a digraph from the specified input stream. The format is the number of vertices V, followed by the number of edges E, followed by E pairs of vertices, with each entry separated by whitespace.
      Parameters:
      in - the input stream
      Throws:
      IllegalArgumentException - if in is null
      IllegalArgumentException - if the endpoints of any edge are not in prescribed range
      IllegalArgumentException - if the number of vertices or edges is negative
      IllegalArgumentException - if the input stream is in the wrong format
    • Digraph Link icon

      public Digraph(Digraph G)
      Initializes a new digraph that is a deep copy of the specified digraph.
      Parameters:
      G - the digraph to copy
      Throws:
      IllegalArgumentException - if G is null
  • Method Details Link icon

    • V Link icon

      public int V()
      Return the number of vertices in the digraph.
    • E Link icon

      public int E()
      Return the number of edges in the digraph.
    • addEdge Link icon

      public void addEdge(int v, int w)
      Add the directed edge v->w to the digraph.
      Throws:
      IndexOutOfBoundsException - unless both 0 <= v < V and 0 <= w < V
    • adj Link icon

      public Iterable<Integer> adj(int v)
      Return the list of vertices pointed to from vertex v as an Iterable.
      Throws:
      IndexOutOfBoundsException - unless 0 <= v < V
    • outdegree Link icon

      public int outdegree(int v)
      Returns the number of directed edges incident from vertex v. This is known as the outdegree of vertex v.
      Parameters:
      v - the vertex
      Returns:
      the outdegree of vertex v
      Throws:
      IllegalArgumentException - unless {@code 0 <= v < V}
    • indegree Link icon

      public int indegree(int v)
      Returns the number of directed edges incident to vertex v. This is known as the indegree of vertex v.
      Parameters:
      v - the vertex
      Returns:
      the indegree of vertex v
      Throws:
      IllegalArgumentException - unless 0 <= v < V
    • reverse Link icon

      public Digraph reverse()
      Return the reverse of the digraph.
    • toString Link icon

      public String toString()
      Return a string representation of the digraph.
      Overrides:
      toString in class Object
    • toGraphviz Link icon

      public void toGraphviz(String filename)
      Save a graphviz representation of the graph. See graphviz.org.
    • main Link icon

      public static void main(String[] args)
      Test client.