Class Graph
- Object
-
- Graph
-
public class Graph extends Object
TheGraph
class represents an undirected graph of vertices with string names. It supports the following operations: add an edge, add a vertex, get all the vertices, iterate over all the neighbors adjacent to a vertex, is there a vertex, is there an edge between two vertices. Self-loops are permitted; parallel edges are discarded.For additional documentation, see Section 4.5 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEdge(String v, String w)
Adds the edge v-w to this graph (if it is not already an edge).void
addVertex(String v)
Adds vertex v to this graph (if it is not already a vertex).Iterable<String>
adjacentTo(String v)
Returns the set of vertices adjacent to v in this graph.int
degree(String v)
Returns the degree of vertex v in this graph.int
E()
Returns the number of edges in this graph.boolean
hasEdge(String v, String w)
Returns true if v-w is an edge in this graph.boolean
hasVertex(String v)
Returns true if v is a vertex in this graph.static void
main(String[] args)
Unit tests theGraph
data type.String
toString()
Returns a string representation of this graph.int
V()
Returns the number of vertices in this graph.Iterable<String>
vertices()
Returns the 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
-
degree
public int degree(String v)
Returns the degree of vertex v in this graph.- Parameters:
v
- the vertex- Returns:
- the degree of
v
in this graph - Throws:
IllegalArgumentException
- ifv
is not a vertex in this graph
-
addEdge
public void addEdge(String v, String w)
Adds the edge v-w to this graph (if it is not already an edge).- Parameters:
v
- one vertex in the edgew
- the other vertex in the edge
-
addVertex
public void addVertex(String v)
Adds vertex v to this graph (if it is not already a vertex).- Parameters:
v
- the vertex
-
vertices
public Iterable<String> vertices()
Returns the vertices in this graph.- Returns:
- the set of vertices in this graph
-
adjacentTo
public Iterable<String> adjacentTo(String v)
Returns the set of vertices adjacent to v in this graph.- Parameters:
v
- the vertex- Returns:
- the set of vertices adjacent to vertex
v
in this graph - Throws:
IllegalArgumentException
- ifv
is not a vertex in this graph
-
hasVertex
public boolean hasVertex(String v)
Returns true if v is a vertex in this graph.- Parameters:
v
- the vertex- Returns:
true
ifv
is a vertex in this graph,false
otherwise
-
hasEdge
public boolean hasEdge(String v, String w)
Returns true if v-w is an edge in this graph.- Parameters:
v
- one vertex in the edgew
- the other vertex in the edge- Returns:
true
ifv-w
is a vertex in this graph,false
otherwise- Throws:
IllegalArgumentException
- if eitherv
orw
is not a vertex in this graph
-
toString
public String toString()
Returns a string representation of this graph.
-
main
public static void main(String[] args)
Unit tests theGraph
data type.- Parameters:
args
- the command-line arguments
-
-