public class EdgeWeightedGraph extends Object
EdgeWeightedGraph
class represents an edgeweighted
graph of vertices named 0 through V – 1, where each
undirected edge is of type Edge
and has a realvalued weight.
It supports the following two primary operations: add an edge to the graph,
iterate over all of the edges incident to a vertex. It also provides
methods for returning the number of vertices V and the number
of edges E. Parallel edges and selfloops are permitted.
By convention, a selfloop vv appears in the
adjacency list of v twice and contributes two to the degree
of v.
This implementation uses an adjacencylists representation, which
is a vertexindexed array of Bag
objects.
All operations take constant time (in the worst case) except
iterating over the edges incident to a given vertex, which takes
time proportional to the number of such edges.
For additional documentation, see Section 4.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
Constructor and Description 

EdgeWeightedGraph(EdgeWeightedGraph G)
Initializes a new edgeweighted graph that is a deep copy of
G . 
EdgeWeightedGraph(In in)
Initializes an edgeweighted graph from an input stream.

EdgeWeightedGraph(int V)
Initializes an empty edgeweighted graph with
V vertices and 0 edges. 
EdgeWeightedGraph(int V,
int E)
Initializes a random edgeweighted graph with
V vertices and E edges. 
Modifier and Type  Method and Description 

void 
addEdge(Edge e)
Adds the undirected edge
e to this edgeweighted graph. 
Iterable<Edge> 
adj(int v)
Returns the edges incident on vertex
v . 
int 
degree(int v)
Returns the degree of vertex
v . 
int 
E()
Returns the number of edges in this edgeweighted graph.

Iterable<Edge> 
edges()
Returns all edges in this edgeweighted graph.

static void 
main(String[] args)
Unit tests the
EdgeWeightedGraph data type. 
String 
toString()
Returns a string representation of the edgeweighted graph.

int 
V()
Returns the number of vertices in this edgeweighted graph.

public EdgeWeightedGraph(int V)
V
vertices and 0 edges.V
 the number of verticesIllegalArgumentException
 if V < 0
public EdgeWeightedGraph(int V, int E)
V
vertices and E edges.V
 the number of verticesE
 the number of edgesIllegalArgumentException
 if V < 0
IllegalArgumentException
 if E < 0
public EdgeWeightedGraph(In in)
in
 the input streamIllegalArgumentException
 if the endpoints of any edge are not in prescribed rangeIllegalArgumentException
 if the number of vertices or edges is negativepublic EdgeWeightedGraph(EdgeWeightedGraph G)
G
.G
 the edgeweighted graph to copypublic int V()
public int E()
public void addEdge(Edge e)
e
to this edgeweighted graph.e
 the edgeIllegalArgumentException
 unless both endpoints are between 0
and V1
public Iterable<Edge> adj(int v)
v
.v
 the vertexv
as an IterableIllegalArgumentException
 unless 0 <= v < V
public int degree(int v)
v
.v
 the vertexv
IllegalArgumentException
 unless 0 <= v < V
public Iterable<Edge> edges()
for (Edge e : G.edges())
.public String toString()
public static void main(String[] args)
EdgeWeightedGraph
data type.args
 the commandline arguments