V
- Type parameter for vertices (type of objects attached to each vertex)E
- Type parameter for edges (type of objects attached to each edge)public class Graph<V extends SequenceElement,E extends java.lang.Number> extends java.lang.Object implements IGraph<V,E>
allowMultipleEdges
, this graph implementation may or may not allow
multiple edges between any two adjacent nodes. If multiple edges are required (such that two or more distinct edges
between vertices X and Y exist simultaneously) then allowMultipleEdges
should be set to true
.IGraph
, this graph representation can have arbitrary objects attachedVertexFactory
. Edges are added using one of the
addEdge methods.Constructor and Description |
---|
Graph() |
Graph(java.util.Collection<V> elements,
boolean allowMultipleEdges) |
Graph(int numVertices,
boolean allowMultipleEdges,
VertexFactory<V> vertexFactory) |
Graph(int numVertices,
VertexFactory<V> vertexFactory) |
Graph(java.util.List<Vertex<V>> vertices) |
Graph(java.util.List<Vertex<V>> vertices,
boolean allowMultipleEdges) |
Modifier and Type | Method and Description |
---|---|
void |
addEdge(Edge<E> edge)
Add an edge to the graph.
|
void |
addEdge(int from,
int to,
E value,
boolean directed)
Convenience method for adding an edge (directed or undirected) to graph
|
void |
addVertex(Vertex<V> vertex,
java.util.Collection<Edge<E>> edges) |
void |
addVertex(Vertex<V> vertex,
Edge<E> edge) |
boolean |
equals(java.lang.Object o) |
int[] |
getConnectedVertexIndices(int vertex)
Return an array of indexes of vertices that the specified vertex is connected to.
Specifically, for undirected graphs return int[] of all X.vertexID() such that (vertex -- X) exists For directed graphs, return int[] of all X.vertexID() such that (vertex -> X) exists |
java.util.List<Vertex<V>> |
getConnectedVertices(int vertex)
Get a list of all of the vertices that the specified vertex is connected to
Specifically, for undirected graphs return list of all X such that (vertex -- X) exists For directed graphs, return list of all X such that (vertex -> X) exists |
java.util.List<Edge<E>> |
getEdgesOut(int vertex)
Returns a list of edges for a vertex with a given index
For undirected graphs, returns all edges incident on the vertex
For directed graphs, only returns outward directed edges
|
Vertex<V> |
getRandomConnectedVertex(int vertex,
java.util.Random rng)
Randomly sample a vertex connected to a given vertex.
|
Vertex<V> |
getVertex(int idx)
Get a vertex in the graph for a given index
|
int |
getVertexDegree(int vertex)
Returns the degree of the vertex.
For undirected graphs, this is just the degree. For directed graphs, this returns the outdegree |
java.util.List<Vertex<V>> |
getVertices(int[] indexes)
Get multiple vertices in the graph
|
java.util.List<Vertex<V>> |
getVertices(int from,
int to)
Get multiple vertices in the graph, with secified indices
|
int |
hashCode() |
int |
numVertices()
Number of vertices in the graph
|
java.lang.String |
toString() |
public Graph(int numVertices, VertexFactory<V> vertexFactory)
public Graph(int numVertices, boolean allowMultipleEdges, VertexFactory<V> vertexFactory)
public Graph(java.util.Collection<V> elements, boolean allowMultipleEdges)
public Graph()
public int numVertices()
IGraph
numVertices
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
public Vertex<V> getVertex(int idx)
IGraph
getVertex
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
idx
- integer index of the vertex to get. must be in range 0 to numVertices()public java.util.List<Vertex<V>> getVertices(int[] indexes)
IGraph
getVertices
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
indexes
- the indexes of the vertices to retrievepublic java.util.List<Vertex<V>> getVertices(int from, int to)
IGraph
getVertices
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
from
- first vertex to get, inclusiveto
- last vertex to get, inclusivepublic void addEdge(Edge<E> edge)
IGraph
addEdge
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
public void addEdge(int from, int to, E value, boolean directed)
addEdge
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
from
- to
- value
- directed
- public java.util.List<Edge<E>> getEdgesOut(int vertex)
IGraph
getEdgesOut
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
vertex
- index of the vertex topublic int getVertexDegree(int vertex)
IGraph
getVertexDegree
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
vertex
- vertex to get degree forpublic Vertex<V> getRandomConnectedVertex(int vertex, java.util.Random rng) throws NoEdgesException
IGraph
getRandomConnectedVertex
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
vertex
- vertex to randomly sample fromrng
- Random number generator to useNoEdgesException
- thrown if the specified vertex has no edges, or no outgoing edges (in the case
of a directed graph).public java.util.List<Vertex<V>> getConnectedVertices(int vertex)
IGraph
getConnectedVertices
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
vertex
- Index of the vertexpublic int[] getConnectedVertexIndices(int vertex)
IGraph
getConnectedVertexIndices
in interface IGraph<V extends SequenceElement,E extends java.lang.Number>
vertex
- index of the vertexIGraph.getConnectedVertices(int)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object