Class Graph
- Object
-
- Graph
-
public class Graph extends Object
TheGraphclass 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 voidaddEdge(String v, String w)Adds the edge v-w to this graph (if it is not already an edge).voidaddVertex(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.intdegree(String v)Returns the degree of vertex v in this graph.intE()Returns the number of edges in this graph.booleanhasEdge(String v, String w)Returns true if v-w is an edge in this graph.booleanhasVertex(String v)Returns true if v is a vertex in this graph.static voidmain(String[] args)Unit tests theGraphdata type.StringtoString()Returns a string representation of this graph.intV()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
vin this graph - Throws:
IllegalArgumentException- ifvis 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
vin this graph - Throws:
IllegalArgumentException- ifvis 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:
trueifvis a vertex in this graph,falseotherwise
-
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:
trueifv-wis a vertex in this graph,falseotherwise- Throws:
IllegalArgumentException- if eithervorwis 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 theGraphdata type.- Parameters:
args- the command-line arguments
-
-