public class Graph extends Object
This implementation uses an adjacency-lists representation, which
is a vertex-indexed array of Bag objects.
All operations take constant time (in the worst case) except
iterating over the vertices adjacent to a given vertex, which takes
time proportional to the number of such vertices.
For additional documentation, see Section 4.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
Graph(Graph G)
Initializes a new graph that is a deep copy of G.
|
Graph(edu.princeton.cs.introcs.In in)
Initializes a graph from an input stream.
|
Graph(int V)
Initializes an empty graph with V vertices and 0 edges.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addEdge(int v,
int w)
Adds the undirected edge v-w to the graph.
|
Iterable<Integer> |
adj(int v)
Returns the vertices adjacent to vertex v.
|
int |
E()
Returns the number of edges in the graph.
|
static void |
main(String[] args)
Unit tests the Graph data type.
|
String |
toString()
Returns a string representation of the graph.
|
int |
V()
Returns the number of vertices in the graph.
|
public Graph(int V)
IllegalArgumentException - if V < 0public Graph(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 Graph(Graph G)
G - the graph to copypublic int V()
public int E()
public void addEdge(int v,
int w)
v - one vertex in the edgew - the other vertex in the edgeIndexOutOfBoundsException - unless both 0 <= v < V and 0 <= w < Vpublic Iterable<Integer> adj(int v)
v - the vertexIndexOutOfBoundsException - unless 0 <= v < Vpublic String toString()
public static void main(String[] args)
Copyright © 2014. All Rights Reserved.