Package com.gengoai.graph
Class DefaultGraphImpl<V>
- java.lang.Object
-
- com.gengoai.graph.DefaultGraphImpl<V>
-
- All Implemented Interfaces:
Graph<V>
,Serializable
,Iterable<V>
public class DefaultGraphImpl<V> extends Object implements Graph<V>, Serializable
Default graph implementation that uses aTable
to store edges and keeps track of the vertices in aSet
. By default aLinkedHashSet
is used so that the order of the vertices is the same as when added.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DefaultGraphImpl(EdgeFactory<V> edgeFactory)
Instantiates a new Adjacency matrix.
-
Method Summary
All Methods Instance Methods Concrete 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 graphboolean
addVertex(V vertex)
Adds a vertex to the graphvoid
addVertices(Collection<V> vertices)
Adds all vertices in a collection to the graphboolean
containsEdge(V fromVertex, V toVertex)
Checks if an edge in the graphboolean
containsVertex(V vertex)
Checks if a vertex in the graphint
degree(V vertex)
The number of neighborsSet<? extends Edge<V>>
edges()
Edges set.boolean
equals(Object o)
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 grapySet<? extends Edge<V>>
getInEdges(V vertex)
Gets the edges coming in to the given vertex (i.e.Set<? 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.Set<V>
getSuccessors(V vertex)
Gets the neighbors associated with the outgoing edges for the given vertex.int
hashCode()
int
inDegree(V vertex)
The number of predecessorsboolean
isDirected()
Is directed.boolean
isEmpty()
Determines if the graph is empty (no vertices no edges)Iterator<V>
iterator()
int
numberOfEdges()
Number of edges.int
numberOfVertices()
Number of vertices.int
outDegree(V vertex)
The number of successorsboolean
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 graphString
toString()
Set<V>
vertices()
Vertices set.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.gengoai.graph.Graph
addEdges, containsEdge, getEdges, getNeighbors, getPredecessorsWeights, getSuccessorWeights, getWeight, getWeights, merge, merge, parallelstream, stream
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
DefaultGraphImpl
public DefaultGraphImpl(EdgeFactory<V> edgeFactory)
Instantiates a new Adjacency matrix.- Parameters:
edgeFactory
- the edge factory
-
-
Method Detail
-
addEdge
public Edge<V> addEdge(V fromVertex, V toVertex)
Description copied from interface:Graph
Adds an edge to the graph
-
addEdge
public Edge<V> addEdge(V fromVertex, V toVertex, double weight)
Description copied from interface:Graph
Adds an edge to the graph
-
addEdge
public void addEdge(Edge<V> edge)
Description copied from interface:Graph
Adds an edge to the graph.
-
addVertex
public boolean addVertex(V vertex)
Description copied from interface:Graph
Adds a vertex to the graph
-
addVertices
public void addVertices(Collection<V> vertices)
Description copied from interface:Graph
Adds all vertices in a collection to the graph- Specified by:
addVertices
in interfaceGraph<V>
- Parameters:
vertices
- The vertices to add
-
containsEdge
public boolean containsEdge(V fromVertex, V toVertex)
Description copied from interface:Graph
Checks if an edge in the graph- Specified by:
containsEdge
in interfaceGraph<V>
- Parameters:
fromVertex
- The first (from) vertextoVertex
- The second (to) vertex- Returns:
- True if the edge is in the graph, false if not
-
containsVertex
public boolean containsVertex(V vertex)
Description copied from interface:Graph
Checks if a vertex in the graph- Specified by:
containsVertex
in interfaceGraph<V>
- Parameters:
vertex
- The vertex- Returns:
- True if the vertex is in the graph, false if not
-
degree
public int degree(V vertex)
Description copied from interface:Graph
The number of neighbors
-
getEdge
public Edge<V> getEdge(V v1, V v2)
Description copied from interface:Graph
Gets the edge if one, between the two given vertices
-
getEdgeFactory
public EdgeFactory<V> getEdgeFactory()
Description copied from interface:Graph
Gets the edge factory used in this grapy- Specified by:
getEdgeFactory
in interfaceGraph<V>
- Returns:
- The edge factory
-
getInEdges
public Set<? extends Edge<V>> getInEdges(V vertex)
Description copied from interface:Graph
Gets the edges coming in to the given vertex (i.e. in-links)- Specified by:
getInEdges
in interfaceGraph<V>
- Parameters:
vertex
- The vertex- Returns:
- The set of incoming edges
-
getOutEdges
public Set<? extends Edge<V>> getOutEdges(V vertex)
Description copied from interface:Graph
Gets the edges coming out out of the given vertex (i.e. out-links)- Specified by:
getOutEdges
in interfaceGraph<V>
- Parameters:
vertex
- The vertex- Returns:
- The set of outgoing edges
-
getPredecessors
public Set<V> getPredecessors(V vertex)
Description copied from interface:Graph
Gets the neighbors associated with the incoming edges for the given vertex.- Specified by:
getPredecessors
in interfaceGraph<V>
- Parameters:
vertex
- The vertex- Returns:
- The set of vertices which contain an outgoing edge to the given vertex.
-
getSuccessors
public Set<V> getSuccessors(V vertex)
Description copied from interface:Graph
Gets the neighbors associated with the outgoing edges for the given vertex.- Specified by:
getSuccessors
in interfaceGraph<V>
- Parameters:
vertex
- The vertex- Returns:
- The set of vertices which contain an incoming edge from the given vertex.
-
inDegree
public int inDegree(V vertex)
Description copied from interface:Graph
The number of predecessors
-
isDirected
public boolean isDirected()
Description copied from interface:Graph
Is directed.- Specified by:
isDirected
in interfaceGraph<V>
- Returns:
- True if the graph is directed, false if it is undirected
-
isEmpty
public boolean isEmpty()
Description copied from interface:Graph
Determines if the graph is empty (no vertices no edges)
-
numberOfEdges
public int numberOfEdges()
Description copied from interface:Graph
Number of edges.- Specified by:
numberOfEdges
in interfaceGraph<V>
- Returns:
- Number of edges in the graph
-
numberOfVertices
public int numberOfVertices()
Description copied from interface:Graph
Number of vertices.- Specified by:
numberOfVertices
in interfaceGraph<V>
- Returns:
- Number of vertices in the graph
-
outDegree
public int outDegree(V vertex)
Description copied from interface:Graph
The number of successors
-
removeEdge
public Edge<V> removeEdge(V fromVertex, V toVertex)
Description copied from interface:Graph
Removes an edge from the graph- Specified by:
removeEdge
in interfaceGraph<V>
- Parameters:
fromVertex
- The first (from) vertextoVertex
- The second (to) vertex- Returns:
- The edge that was removed or null if there was no edge
-
removeEdge
public boolean removeEdge(Edge<V> edge)
Description copied from interface:Graph
Removes an edge from the graph- Specified by:
removeEdge
in interfaceGraph<V>
- Parameters:
edge
- The edge to remove- Returns:
- True if the edge was removed, false if not
-
removeVertex
public boolean removeVertex(V vertex)
Description copied from interface:Graph
Removes a vertex from the graph- Specified by:
removeVertex
in interfaceGraph<V>
- Parameters:
vertex
- The vertex to remove- Returns:
- True if the vertex was removed, false if not
-
-