Package com.gengoai.graph
Interface Graph<V>
-
- Type Parameters:
V
- the vertex type
- All Superinterfaces:
Iterable<V>
- All Known Implementing Classes:
DefaultGraphImpl
public interface Graph<V> extends Iterable<V>
Interface defining a graph data structure.
- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
addEdge(Edge<V> edge)
Adds an edge to the graph.Edge<V>
addEdge(V fromVertex, V toVertex)
Adds an edge to the graphEdge<V>
addEdge(V fromVertex, V toVertex, double weight)
Adds an edge to the graphdefault void
addEdges(Collection<? extends Edge<V>> edges)
Adds a collection of edges to the graph.boolean
addVertex(V vertex)
Adds a vertex to the graphvoid
addVertices(Collection<V> vertices)
Adds all vertices in a collection to the graphdefault boolean
containsEdge(Edge<V> edge)
Checks if an edge in the graphboolean
containsEdge(V fromVertex, V toVertex)
Checks if an edge in the graphboolean
containsVertex(V vertex)
Checks if a vertex in the graphstatic <V> Graph<V>
create(EdgeFactory<V> edgeFactory)
Creates a graph with verticesV
and edges defined by the givenEdgeFactory
.int
degree(V vertex)
The number of neighborsstatic <V> Graph<V>
directed()
Creates a graph with verticesV
and edges defined byDirectedEdgeFactory
.Set<? extends Edge<V>>
edges()
Edges set.Edge<V>
getEdge(V v1, V v2)
Gets the edge if one, between the two given verticesEdgeFactory<V>
getEdgeFactory()
Gets the edge factory used in this grapydefault Set<? extends Edge<V>>
getEdges(V vertex)
Gets all edges incident to the given vertex.Set<? extends Edge<V>>
getInEdges(V vertex)
Gets the edges coming in to the given vertex (i.e.default Set<V>
getNeighbors(V vertex)
Gets the set of vertices that share an edge with the given vertexSet<? extends Edge<V>>
getOutEdges(V vertex)
Gets the edges coming out out of the given vertex (i.e.Set<V>
getPredecessors(V vertex)
Gets the neighbors associated with the incoming edges for the given vertex.default Counter<V>
getPredecessorsWeights(V vertex)
Gets the weights associated with the edges to the predecessors of the given vertex.Set<V>
getSuccessors(V vertex)
Gets the neighbors associated with the outgoing edges for the given vertex.default Counter<V>
getSuccessorWeights(V vertex)
Gets the weights associated with the edges to the successors of the given vertex.default double
getWeight(V v1, V v2)
Gets the weight of the edge if one, between the two given verticesdefault Counter<V>
getWeights(V vertex)
Gets the weights associated with the edges of the given vertex.int
inDegree(V vertex)
The number of predecessorsboolean
isDirected()
Is directed.boolean
isEmpty()
Determines if the graph is empty (no vertices no edges)default void
merge(Graph<V> other)
Merges another graph into this one ignoring any duplicate edgesdefault void
merge(Graph<V> other, EdgeMergeFunction<V> mergeFunction)
Merges another graph into this one combining edges using the supplied merge functionint
numberOfEdges()
Number of edges.int
numberOfVertices()
Number of vertices.int
outDegree(V vertex)
The number of successorsdefault Stream<V>
parallelstream()
Returns a stream of the vertices in this graphboolean
removeEdge(Edge<V> edge)
Removes an edge from the graphEdge<V>
removeEdge(V fromVertex, V toVertex)
Removes an edge from the graphboolean
removeVertex(V vertex)
Removes a vertex from the graphdefault Stream<V>
stream()
Returns a stream of the vertices in this graphstatic <V> Graph<V>
undirected()
Creates a graph with verticesV
and edges defined byUndirectedEdgeFactory
.Set<V>
vertices()
Vertices set.-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
-
-
-
Method Detail
-
create
static <V> Graph<V> create(EdgeFactory<V> edgeFactory)
Creates a graph with verticesV
and edges defined by the givenEdgeFactory
.- Type Parameters:
V
- the vertex type parameter- Parameters:
edgeFactory
- the edge factory to use for creating and validating edges- Returns:
- the graph
-
directed
static <V> Graph<V> directed()
Creates a graph with verticesV
and edges defined byDirectedEdgeFactory
.- Type Parameters:
V
- the vertex type parameter- Returns:
- the graph
-
undirected
static <V> Graph<V> undirected()
Creates a graph with verticesV
and edges defined byUndirectedEdgeFactory
.- Type Parameters:
V
- the vertex type parameter- Returns:
- the graph
-
addEdge
Edge<V> addEdge(V fromVertex, V toVertex)
Adds an edge to the graph- Parameters:
fromVertex
- The first (from) vertextoVertex
- The second (to) vertex- Returns:
- The edge that was created
-
addEdge
Edge<V> addEdge(V fromVertex, V toVertex, double weight)
Adds an edge to the graph- Parameters:
fromVertex
- The first (from) vertextoVertex
- The second (to) vertexweight
- The weight of the edge- Returns:
- The edge that was created
-
addEdges
default void addEdges(Collection<? extends Edge<V>> edges)
Adds a collection of edges to the graph.- Parameters:
edges
- the edges
-
addVertex
boolean addVertex(V vertex)
Adds a vertex to the graph- Parameters:
vertex
- The vertex- Returns:
- True if the vertex was added, False if not
-
addVertices
void addVertices(Collection<V> vertices)
Adds all vertices in a collection to the graph- Parameters:
vertices
- The vertices to add
-
containsEdge
boolean containsEdge(V fromVertex, V toVertex)
Checks if an edge in the graph- Parameters:
fromVertex
- The first (from) vertextoVertex
- The second (to) vertex- Returns:
- True if the edge is in the graph, false if not
-
containsEdge
default boolean containsEdge(Edge<V> edge)
Checks if an edge in the graph- Parameters:
edge
- The edge to check- Returns:
- True if the edge is in the graph, false if not
-
containsVertex
boolean containsVertex(V vertex)
Checks if a vertex in the graph- Parameters:
vertex
- The vertex- Returns:
- True if the vertex is in the graph, false if not
-
degree
int degree(V vertex)
The number of neighbors- Parameters:
vertex
- The vertex- Returns:
- The degree
-
getEdge
Edge<V> getEdge(V v1, V v2)
Gets the edge if one, between the two given vertices- Parameters:
v1
- vertex 1v2
- vertex 2- Returns:
- The edge if one, null otherwise
-
getEdgeFactory
EdgeFactory<V> getEdgeFactory()
Gets the edge factory used in this grapy- Returns:
- The edge factory
-
getEdges
default Set<? extends Edge<V>> getEdges(V vertex)
Gets all edges incident to the given vertex.- Parameters:
vertex
- The vertex- Returns:
- The set of incident edges
-
getInEdges
Set<? extends Edge<V>> getInEdges(V vertex)
Gets the edges coming in to the given vertex (i.e. in-links)- Parameters:
vertex
- The vertex- Returns:
- The set of incoming edges
-
getNeighbors
default Set<V> getNeighbors(V vertex)
Gets the set of vertices that share an edge with the given vertex- Parameters:
vertex
- The vertex- Returns:
- The set of vertices which share an edge with the given vertex.
-
getOutEdges
Set<? extends Edge<V>> getOutEdges(V vertex)
Gets the edges coming out out of the given vertex (i.e. out-links)- Parameters:
vertex
- The vertex- Returns:
- The set of outgoing edges
-
getPredecessors
Set<V> getPredecessors(V vertex)
Gets the neighbors associated with the incoming edges for the given vertex.- Parameters:
vertex
- The vertex- Returns:
- The set of vertices which contain an outgoing edge to the given vertex.
-
getPredecessorsWeights
default Counter<V> getPredecessorsWeights(V vertex)
Gets the weights associated with the edges to the predecessors of the given vertex.- Parameters:
vertex
- The vertex- Returns:
- The weights associated with the edges to the predecessors
-
getSuccessorWeights
default Counter<V> getSuccessorWeights(V vertex)
Gets the weights associated with the edges to the successors of the given vertex.- Parameters:
vertex
- The vertex- Returns:
- The weights associated with the edges to the successors
-
getSuccessors
Set<V> getSuccessors(V vertex)
Gets the neighbors associated with the outgoing edges for the given vertex.- Parameters:
vertex
- The vertex- Returns:
- The set of vertices which contain an incoming edge from the given vertex.
-
getWeight
default double getWeight(V v1, V v2)
Gets the weight of the edge if one, between the two given vertices- Parameters:
v1
- vertex 1v2
- vertex 2- Returns:
- The weight if one, 0 otherwise
-
getWeights
default Counter<V> getWeights(V vertex)
Gets the weights associated with the edges of the given vertex.- Parameters:
vertex
- The vertex- Returns:
- The weights associated with the edges
-
inDegree
int inDegree(V vertex)
The number of predecessors- Parameters:
vertex
- The vertex- Returns:
- The in degree
-
isDirected
boolean isDirected()
Is directed.- Returns:
- True if the graph is directed, false if it is undirected
-
isEmpty
boolean isEmpty()
Determines if the graph is empty (no vertices no edges)- Returns:
- True if the graph is empty
-
merge
default void merge(Graph<V> other)
Merges another graph into this one ignoring any duplicate edges- Parameters:
other
- The graph to merge
-
merge
default void merge(Graph<V> other, EdgeMergeFunction<V> mergeFunction)
Merges another graph into this one combining edges using the supplied merge function- Parameters:
other
- The graph to mergemergeFunction
- The function to use to merge duplicate edges
-
numberOfEdges
int numberOfEdges()
Number of edges.- Returns:
- Number of edges in the graph
-
numberOfVertices
int numberOfVertices()
Number of vertices.- Returns:
- Number of vertices in the graph
-
outDegree
int outDegree(V vertex)
The number of successors- Parameters:
vertex
- The vertex- Returns:
- The out degree
-
parallelstream
default Stream<V> parallelstream()
Returns a stream of the vertices in this graph- Returns:
- A stream of the vertices in this graph
-
removeEdge
Edge<V> removeEdge(V fromVertex, V toVertex)
Removes an edge from the graph- Parameters:
fromVertex
- The first (from) vertextoVertex
- The second (to) vertex- Returns:
- The edge that was removed or null if there was no edge
-
removeEdge
boolean removeEdge(Edge<V> edge)
Removes an edge from the graph- Parameters:
edge
- The edge to remove- Returns:
- True if the edge was removed, false if not
-
removeVertex
boolean removeVertex(V vertex)
Removes a vertex from the graph- Parameters:
vertex
- The vertex to remove- Returns:
- True if the vertex was removed, false if not
-
stream
default Stream<V> stream()
Returns a stream of the vertices in this graph- Returns:
- A stream of the vertices in this graph
-
-