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

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

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

    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

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

    • V

      private final int V
    • E

      private int E
    • adj

      private final Bag<Integer>[] adj
  • Constructor Details

    • Graph

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

    • V

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

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

      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

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

      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

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

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

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