public class MysteryWeightedGraphImplementation extends java.lang.Object implements WeightedGraph
This class can represent both directed and undirected graphs, but the choice must be made at construction time, and is final.
Another choice that must be made at construction time is that of the "sentinel" value for weights. This is the value that getWeight() returns for edges that are not in the graph. The default is Double.POSITIVE_INFINITY, but other common choices are negative infinity, zero, or NaN.
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 and Description |
---|
MysteryWeightedGraphImplementation()
Default constructor: an empty directed graph.
|
MysteryWeightedGraphImplementation(boolean directed)
Constructs an empty graph with the specified directedness, with a
sentinel value of Double.POSITIVE_INFINITY.
|
MysteryWeightedGraphImplementation(boolean directed,
int N)
Constructs a graph with N vertices and the specified directedness, with a
sentinel value of Double.POSITIVE_INFINITY.
|
MysteryWeightedGraphImplementation(boolean directed,
int N,
double sentinelWeight)
Constructs a graph with N vertices and the specified directedness and
sentinel value.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addEdge(int begin,
int end,
double weight)
Adds a weighted edge between two vertices.
|
int |
addVertex()
Adds a new vertex.
|
void |
clear()
Removes all vertices and edges from the graph.
|
int |
getDegree(int v)
Returns the out-degree of the specified vertex.
|
int |
getInDegree(int v)
Returns the in-degree of the specified vertex.
|
java.lang.Iterable<java.lang.Integer> |
getNeighbors(int v)
Returns an iterable object that allows iteration over the neighbors of
the specified vertex.
|
double |
getWeight(int begin,
int end)
Gets the weight of the edge between two vertices.
|
boolean |
hasEdge(int begin,
int end)
Returns whether an edge exists between two vertices.
|
boolean |
isDirected()
Returns true if the graph is directed.
|
boolean |
isEmpty()
Returns true if there are no vertices in the graph.
|
double |
missingEdgeSentinel()
Returns the "sentinel" weight value for edges not in the graph.
|
int |
numEdges()
Returns the number of edges in the graph.
|
int |
numVerts()
Returns the number of vertices in the graph.
|
boolean |
setWeight(int begin,
int end,
double weight)
Sets the weight of an edge already in the graph.
|
java.lang.String |
toString() |
public MysteryWeightedGraphImplementation()
public MysteryWeightedGraphImplementation(boolean directed)
public MysteryWeightedGraphImplementation(boolean directed, int N)
public MysteryWeightedGraphImplementation(boolean directed, int N, double sentinelWeight)
public int addVertex()
Graph
public boolean addEdge(int begin, int end, double weight)
WeightedGraph
addEdge
in interface WeightedGraph
public boolean hasEdge(int begin, int end)
Graph
public boolean setWeight(int begin, int end, double weight)
WeightedGraph
setWeight
in interface WeightedGraph
public double getWeight(int begin, int end)
WeightedGraph
getWeight
in interface WeightedGraph
public int getDegree(int v)
Graph
public int getInDegree(int v)
Graph
getInDegree
in interface Graph
public java.lang.Iterable<java.lang.Integer> getNeighbors(int v)
Graph
getNeighbors
in interface Graph
public int numVerts()
Graph
public int numEdges()
Graph
public boolean isDirected()
Graph
isDirected
in interface Graph
public boolean isEmpty()
Graph
public void clear()
Graph
public double missingEdgeSentinel()
WeightedGraph
missingEdgeSentinel
in interface WeightedGraph
public java.lang.String toString()
toString
in class java.lang.Object