Package algs41
Class Graph
java.lang.Object
algs41.Graph
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.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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 vertexv
.int
E()
Return the number of edges in the graph.static void
Test client.void
toGraphviz
(String filename) Save a graphviz representation of the graph.toString()
Return a string representation of the graph.int
V()
Return the number of vertices in the graph.
-
Constructor Details
-
Graph
Create an empty graph with V vertices.
-
-
Method Details
-
V
Return the number of vertices in the graph. -
E
Return the number of edges in the graph. -
addEdge
Add the undirected edge v-w to graph.- Throws:
IndexOutOfBoundsException
- unless both0 <= v < V
and0 <= w < V
-
adj
Return the list of neighbors of vertex v as in Iterable.- Throws:
IndexOutOfBoundsException
- unless0 <= v < V
-
degree
Returns the degree of vertexv
.- Parameters:
v
- the vertex- Returns:
- the degree of vertex
v
- Throws:
IllegalArgumentException
- unless0 <= v < V
-
toString
Return a string representation of the graph. -
toGraphviz
Save a graphviz representation of the graph. See graphviz.org. -
main
Test client.
-