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 Number> extends 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(Collection<V> elements,
boolean allowMultipleEdges) |
Graph(int numVertices,
boolean allowMultipleEdges,
VertexFactory<V> vertexFactory) |
Graph(int numVertices,
VertexFactory<V> vertexFactory) |
Graph(List<Vertex<V>> vertices) |
Graph(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,
Collection<Edge<E>> edges) |
void |
addVertex(Vertex<V> vertex,
Edge<E> edge) |
boolean |
equals(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 |
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 |
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,
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 |
List<Vertex<V>> |
getVertices(int[] indexes)
Get multiple vertices in the graph
|
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
|
String |
toString() |
public Graph(int numVertices, VertexFactory<V> vertexFactory)
public Graph(int numVertices, boolean allowMultipleEdges, VertexFactory<V> vertexFactory)
public Graph(Collection<V> elements, boolean allowMultipleEdges)
public Graph()
public void addVertex(Vertex<V> vertex, Collection<Edge<E>> edges)
public int numVertices()
IGraph
numVertices
in interface IGraph<V extends SequenceElement,E extends Number>
public Vertex<V> getVertex(int idx)
IGraph
public List<Vertex<V>> getVertices(int[] indexes)
IGraph
getVertices
in interface IGraph<V extends SequenceElement,E extends Number>
indexes
- the indexes of the vertices to retrievepublic List<Vertex<V>> getVertices(int from, int to)
IGraph
getVertices
in interface IGraph<V extends SequenceElement,E extends Number>
from
- first vertex to get, inclusiveto
- last vertex to get, inclusivepublic void addEdge(Edge<E> edge)
IGraph
public void addEdge(int from, int to, E value, boolean directed)
public List<Edge<E>> getEdgesOut(int vertex)
IGraph
getEdgesOut
in interface IGraph<V extends SequenceElement,E extends Number>
vertex
- index of the vertex topublic int getVertexDegree(int vertex)
IGraph
getVertexDegree
in interface IGraph<V extends SequenceElement,E extends Number>
vertex
- vertex to get degree forpublic Vertex<V> getRandomConnectedVertex(int vertex, Random rng) throws NoEdgesException
IGraph
getRandomConnectedVertex
in interface IGraph<V extends SequenceElement,E extends 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 List<Vertex<V>> getConnectedVertices(int vertex)
IGraph
getConnectedVertices
in interface IGraph<V extends SequenceElement,E extends Number>
vertex
- Index of the vertexpublic int[] getConnectedVertexIndices(int vertex)
IGraph
getConnectedVertexIndices
in interface IGraph<V extends SequenceElement,E extends Number>
vertex
- index of the vertexIGraph.getConnectedVertices(int)
Copyright © 2017. All rights reserved.