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