Package algs41

Class Graph

java.lang.Object
algs41.Graph

public class Graph extends Object
The Graph class represents an undirected 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 adjacent to a vertex. Parallel edges and self-loops are permitted.

For additional documentation, see Section 5.1 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 final int
     
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Graph(int V)
    Create an empty graph with V vertices.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    addEdge(int v, int w)
    Add the undirected edge v-w to graph.
    adj(int v)
    Return the list of neighbors of vertex v as in Iterable.
    int
    degree(int v)
    Returns the degree of vertex v.
    int
    E()
    Return the number of edges in the graph.
    static void
    main(String[] args)
    Test client.
    void
    toGraphviz(String filename)
    Save a graphviz representation of the graph.
    Return a string representation of the graph.
    int
    V()
    Return the number of vertices in the graph.

    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
  • Constructor Details Link icon

    • Graph Link icon

      public Graph(int V)
      Create an empty graph with V vertices.
  • Method Details Link icon

    • V Link icon

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

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

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

      public Iterable<Integer> adj(int v)
      Return the list of neighbors of vertex v as in Iterable.
      Throws:
      IndexOutOfBoundsException - unless 0 <= v < V
    • degree Link icon

      public int degree(int v)
      Returns the degree of vertex v.
      Parameters:
      v - the vertex
      Returns:
      the degree of vertex v
      Throws:
      IllegalArgumentException - unless 0 <= v < V
    • toString Link icon

      public String toString()
      Return a string representation of the graph.
      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.