Package dsa
Class Graph
java.lang.Object
dsa.Graph
A data type to represent an undirected graph.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addEdge
(int v, int w) Adds an undirected edge between verticesv
andw
in this graph.adj
(int v) Returns the vertices adjacent to vertexv
in this graph.int
degree
(int v) Returns the degree of vertexv
in this graph.int
E()
Returns the number of edges in this graph.static void
Unit tests the data type.toString()
Returns a string representation of this graph.int
V()
Returns the number of vertices in this graph.
-
Constructor Details
-
Graph
public Graph(int V) Constructs an empty graph withV
vertices and 0 edges.- Parameters:
V
- number of vertices.
-
Graph
public Graph(stdlib.In in) Constructs a graph from the input streamin
.- Parameters:
in
- the input stream.
-
-
Method Details
-
V
public int V()Returns the number of vertices in this graph.- Returns:
- the number of vertices in this graph.
-
E
public int E()Returns the number of edges in this graph.- Returns:
- the number of edges in this graph.
-
addEdge
public void addEdge(int v, int w) Adds an undirected edge between verticesv
andw
in this graph.- Parameters:
v
- one vertex in the edge.w
- the other vertex in the edge.
-
adj
Returns the vertices adjacent to vertexv
in this graph.- Parameters:
v
- the vertex.- Returns:
- the vertices adjacent to vertex
v
in this graph.
-
degree
public int degree(int v) Returns the degree of vertexv
in this graph.- Parameters:
v
- the vertex.- Returns:
- the degree of vertex
v
in this graph.
-
toString
Returns a string representation of this graph. -
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-