Class MysteryUnweightedGraphImplementation
- java.lang.Object
-
- MysteryUnweightedGraphImplementation
-
- All Implemented Interfaces:
Graph,UnweightedGraph
public class MysteryUnweightedGraphImplementation extends java.lang.Object implements UnweightedGraph
An implementation of the Unweighted Graph ADT. This class can represent both directed and undirected graphs, but the choice must be made at construction time, and is final. Technically, this implementation supports self-loops; it makes no effort to prevent these. Any method that takes one or more vertex IDs as arguments may throw an IndexOutOfBoundsException if any input ID is out of bounds.
-
-
Constructor Summary
Constructors Constructor Description MysteryUnweightedGraphImplementation()Default constructor: an empty directed graph.MysteryUnweightedGraphImplementation(boolean directed)Constructs an empty graph with the specified directedness.MysteryUnweightedGraphImplementation(boolean directed, int N)Constructs a graph with N vertices and the specified directedness.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddEdge(int begin, int end)Adds an edge between two vertices.intaddVertex()Adds a new vertex.voidclear()Removes all vertices and edges from the graph.intgetDegree(int v)Returns the out-degree of the specified vertex.intgetInDegree(int v)Returns the in-degree of the specified vertex.java.lang.Iterable<java.lang.Integer>getNeighbors(int v)Returns an iterator over the neighbors of the specified vertex.booleanhasEdge(int begin, int end)Checks whether an edge exists between two vertices.booleanisDirected()Returns true if the graph is directed.booleanisEmpty()Returns true if there are no vertices in the graph.intnumEdges()Returns the number of edges in the graph.intnumVerts()Returns the number of vertices in the graph.
-
-
-
Constructor Detail
-
MysteryUnweightedGraphImplementation
public MysteryUnweightedGraphImplementation()
Default constructor: an empty directed graph.
-
MysteryUnweightedGraphImplementation
public MysteryUnweightedGraphImplementation(boolean directed)
Constructs an empty graph with the specified directedness.
-
MysteryUnweightedGraphImplementation
public MysteryUnweightedGraphImplementation(boolean directed, int N)Constructs a graph with N vertices and the specified directedness.
-
-
Method Detail
-
addVertex
public int addVertex()
Adds a new vertex.
-
addEdge
public boolean addEdge(int begin, int end)Adds an edge between two vertices. In an undirected graph, this has the same effect as addEdge(end, begin).- Specified by:
addEdgein interfaceUnweightedGraph- Returns:
- false if the edge was already in the graph.
-
hasEdge
public boolean hasEdge(int begin, int end)Checks whether an edge exists between two vertices. In an undirected graph, this returns the same as hasEdge(end, begin).
-
getDegree
public int getDegree(int v)
Returns the out-degree of the specified vertex.
-
getInDegree
public int getInDegree(int v)
Returns the in-degree of the specified vertex.- Specified by:
getInDegreein interfaceGraph
-
getNeighbors
public java.lang.Iterable<java.lang.Integer> getNeighbors(int v)
Returns an iterator over the neighbors of the specified vertex. In particular, the vertex u is included in the returned iterator's sequence if and only if there is an edge from v to u in the graph.- Specified by:
getNeighborsin interfaceGraph
-
numVerts
public int numVerts()
Returns the number of vertices in the graph.
-
numEdges
public int numEdges()
Returns the number of edges in the graph. The result does *not* double-count edges in undirected graphs.
-
isDirected
public boolean isDirected()
Returns true if the graph is directed.- Specified by:
isDirectedin interfaceGraph
-
isEmpty
public boolean isEmpty()
Returns true if there are no vertices in the graph.
-
-