Package dsa
Class Graph
- java.lang.Object
-
- dsa.Graph
-
public class Graph extends Object
A data type to represent an undirected graph.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEdge(int v, int w)
Adds an undirected edge between verticesv
andw
in this graph.Iterable<Integer>
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
main(String[] args)
Unit tests the data type.String
toString()
Returns a string representation of this graph.int
V()
Returns the number of vertices in this graph.
-
-
-
Method Detail
-
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
public Iterable<Integer> adj(int v)
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
public String toString()
Returns a string representation of this graph.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-