Package algs43

Class EdgeWeightedGraph

java.lang.Object
algs43.EdgeWeightedGraph

public class EdgeWeightedGraph extends Object
The EdgeWeightedGraph class represents an undirected graph of vertices named 0 through V-1, where each edge has a real-valued weight. It supports the following operations: add an edge to the graph, in the graph, iterate over all of the neighbors incident to a vertex. Parallel edges and self-loops are permitted.

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

  • Field Summary Link icon

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

    Constructors
    Constructor
    Description
    Create an empty edge-weighted graph with V vertices.
    EdgeWeightedGraph(int V, int E)
    Create a random edge-weighted graph with V vertices and E edges with no parallel edges or self loops.
    EdgeWeightedGraph(int V, int E, boolean allowParallelEdgesAndSelfLoops)
    Create a random edge-weighted graph with V vertices and E edges.
    Copy constructor.
    Create a weighted graph from input stream.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    Add the edge e to this graph.
    adj(int v)
    Return the edges incident to vertex v as an Iterable.
    int
    E()
    Return the number of edges in this graph.
    Return all edges in this graph as an Iterable.
    static void
    main(String[] args)
    Test client.
    void
    toGraphviz(String filename)
    Save a graphviz representation of the graph.
    Return a string representation of this graph.
    int
    V()
    Return the number of vertices in this 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<Edge>[] adj
  • Constructor Details Link icon

    • EdgeWeightedGraph Link icon

      public EdgeWeightedGraph(int V)
      Create an empty edge-weighted graph with V vertices.
    • EdgeWeightedGraph Link icon

      public EdgeWeightedGraph(int V, int E)
      Create a random edge-weighted graph with V vertices and E edges with no parallel edges or self loops. The expected running time is proportional to V + E.
    • EdgeWeightedGraph Link icon

      public EdgeWeightedGraph(int V, int E, boolean allowParallelEdgesAndSelfLoops)
      Create a random edge-weighted graph with V vertices and E edges. The expected running time is proportional to V + E.
    • EdgeWeightedGraph Link icon

      public EdgeWeightedGraph(In in)
      Create a weighted graph from input stream.
    • EdgeWeightedGraph Link icon

      Copy constructor.
  • Method Details Link icon

    • V Link icon

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

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

      public void addEdge(Edge e)
      Add the edge e to this graph.
    • adj Link icon

      public Iterable<Edge> adj(int v)
      Return the edges incident to vertex v as an Iterable. To iterate over the edges incident to vertex v, use foreach notation: for (Edge e : graph.adj(v)).
    • edges Link icon

      public Iterable<Edge> edges()
      Return all edges in this graph as an Iterable. To iterate over the edges, use foreach notation: for (Edge e : graph.edges()).
    • toString Link icon

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