public class EdgeWeightedGraph extends Object
Edge and has a real-valued 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 self-loops are permitted.
This implementation uses an adjacency-lists representation, which is a vertex-indexed array of @link{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 edge-weighted graph that is a deep copy of G.
|
EdgeWeightedGraph(edu.princeton.cs.introcs.In in)
Initializes an edge-weighted graph from an input stream.
|
EdgeWeightedGraph(int V)
Initializes an empty edge-weighted graph with V vertices and 0 edges.
|
EdgeWeightedGraph(int V,
int E)
Initializes a random edge-weighted graph with V vertices and E edges.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addEdge(Edge e)
Adds the undirected edge e to the edge-weighted graph.
|
Iterable<Edge> |
adj(int v)
Returns the edges incident on vertex v.
|
int |
E()
Returns the number of edges in the edge-weighted graph.
|
Iterable<Edge> |
edges()
Returns all edges in the edge-weighted graph.
|
static void |
main(String[] args)
Unit tests the EdgeWeightedGraph data type.
|
String |
toString()
Returns a string representation of the edge-weighted graph.
|
int |
V()
Returns the number of vertices in the edge-weighted graph.
|
public EdgeWeightedGraph(int V)
IllegalArgumentException - if V < 0public EdgeWeightedGraph(int V,
int E)
IllegalArgumentException - if V < 0IllegalArgumentException - if E < 0public EdgeWeightedGraph(edu.princeton.cs.introcs.In in)
in - the input streamIndexOutOfBoundsException - if the endpoints of any edge are not in prescribed rangeIllegalArgumentException - if the number of vertices or edges is negativepublic EdgeWeightedGraph(EdgeWeightedGraph G)
G - the edge-weighted graph to copypublic int V()
public int E()
public void addEdge(Edge e)
e - the edgeIndexOutOfBoundsException - unless both endpoints are between 0 and V-1public Iterable<Edge> adj(int v)
v - the vertexIndexOutOfBoundsException - unless 0 <= v < Vpublic Iterable<Edge> edges()
public String toString()
public static void main(String[] args)
Copyright © 2014. All Rights Reserved.