Class Graph<V>
java.lang.Object
com.github.yellowstonegames.path.Graph<V>
- Type Parameters:
V- the vertex type; oftenCoord
- All Implemented Interfaces:
Externalizable, Serializable
- Direct Known Subclasses:
DirectedGraph, UndirectedGraph
Abstract superclass of actual Graph types.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Connection<V> addConnection(Node<V> a, Node<V> b) protected Connection<V> addConnection(Node<V> a, Node<V> b, float weight) Add an edge to the graph, from v to w.Add an edge to the graph, from v to w and with the specified weight.booleanAdds a vertex to the graph.voidaddVertices(Collection<V> vertices) Adds all the vertices in the collection to the graph.final voidaddVertices(V... vertices) Adds all the vertices in the array or varargs to the graph.abstract Algorithms<V> protected booleanconnectionExists(Node<V> u, Node<V> v) booleanCheck if the graph contains a vertex.protected voiddisconnect(Node<V> node) voiddisconnect(V v) booleanedgeExists(V v, V w) Check if the graph contains an edge from v to w.booleanprotected Connection<V> Retrieve the edge which is from v to w.intGet the number of edges in the graph.com.github.tommyettinger.ds.ObjectOrderedSet<? extends Connection<V>> getEdges()Get an ObjectOrderedSet containing all the edges in the graph.List<? extends Connection<V>> Get a List containing all the edges which have v as a source.protected Collection<Node<V>> getNodes()Get a Set containing all the vertices in the graph.inthashCode()booleanCheck if the graph is directed, that is whether the edges form an ordered pair or a set.protected abstract Connection<V> voidMeant for deserialization using Fory.voidRemoves all edges from the graph.voidRemoves all vertices and edges from the graph.protected booleanremoveConnection(Node<V> a, Node<V> b) booleanremoveEdge(Edge<V> edge) booleanremoveEdge(V v, V w) Removes the edge from v to w from the graph.voidremoveEdgeIf(com.github.tommyettinger.function.ObjPredicate<Edge<V>> predicate) voidremoveEdges(Collection<? extends Edge<V>> edges) protected voidremoveNode(Node<V> node) booleanremoveVertex(V v) Removes a vertex from the graph, and any adjacent edges.voidremoveVertexIf(com.github.tommyettinger.function.ObjPredicate<V> predicate) voidremoveVertices(Collection<V> vertices) Removes all the vertices in the collection from the graph, and any adjacent edges.intsize()Get the number of vertices in the graph.voidsortEdges(Comparator<Connection<V>> comparator) Sort the edges using the provided comparator.voidsortVertices(Comparator<V> comparator) Sort the vertices using the provided comparator.voidMeant for serialization using Fory.
-
Field Details
-
vertexMap
-
edgeSet
-
-
Constructor Details
-
Graph
protected Graph() -
Graph
-
-
Method Details
-
obtainEdge
-
createNew
-
algorithms
-
addVertex
Adds a vertex to the graph.- Parameters:
v- the vertex to be added- Returns:
- true if the vertex was not already in the graph, false otherwise
-
addVertices
Adds all the vertices in the collection to the graph.- Parameters:
vertices- a collection of vertices to be added
-
addVertices
Adds all the vertices in the array or varargs to the graph.- Parameters:
vertices- an array or varargs of vertices to be added
-
removeVertex
Removes a vertex from the graph, and any adjacent edges.- Parameters:
v- the vertex to be removed- Returns:
- true if the vertex was in the graph, false otherwise
-
removeVertices
Removes all the vertices in the collection from the graph, and any adjacent edges.- Parameters:
vertices- vertices a collection of vertices to be removed
-
removeVertexIf
-
disconnect
-
disconnect
-
addEdge
Add an edge to the graph, from v to w. The edge will have a default weight of 1. If there is already an edge between v and w, its weight will be set to 1.- Parameters:
v- the source vertex of the edgew- the destination vertex of the edge- Returns:
- the edge
-
addEdge
Add an edge to the graph, from v to w and with the specified weight. If there is already an edge between v and w, its weight will be set to the specified weight.- Parameters:
v- the source vertex of the edgew- the destination vertex of the edgeweight- the weight of the edge- Returns:
- the edge
-
removeEdge
-
removeEdge
-
removeEdges
-
removeEdgeIf
-
removeAllEdges
public void removeAllEdges()Removes all edges from the graph. -
removeAllVertices
public void removeAllVertices()Removes all vertices and edges from the graph. -
sortVertices
Sort the vertices using the provided comparator. This is reflected in the iteration order of the collection returned bygetVertices(), as well as algorithms which involve iterating over all vertices.- Parameters:
comparator- a comparator for comparing vertices
-
sortEdges
Sort the edges using the provided comparator. This is reflected in the iteration order of the collection returned bygetEdges(), as well as algorithms which involve iterating over all edges.- Parameters:
comparator- a comparator for comparing edges
-
removeNode
-
addConnection
-
addConnection
-
removeConnection
-
contains
Check if the graph contains a vertex.- Parameters:
v- the vertex with which to check- Returns:
- true if the graph contains the vertex, false otherwise
-
getEdge
Retrieve the edge which is from v to w.- Parameters:
v- the source vertex of the edgew- the destination vertex of the edge- Returns:
- the edge if it is in the graph, otherwise null
-
edgeExists
-
getEdges
Get a List containing all the edges which have v as a source.- Parameters:
v- the source vertex of all the edges- Returns:
- a List of edges
-
getEdges
Get an ObjectOrderedSet containing all the edges in the graph.- Returns:
- an ObjectOrderedSet of all the edges in the graph
-
getVertices
-
isDirected
public boolean isDirected()Check if the graph is directed, that is whether the edges form an ordered pair or a set.- Returns:
- whether the graph is directed
-
size
public int size()Get the number of vertices in the graph.- Returns:
- the number of vertices
-
getEdgeCount
public int getEdgeCount()Get the number of edges in the graph.- Returns:
- the number of edges
-
writeExternal
Meant for serialization using Fory. If a class overrides this with different behavior,readExternal(ObjectInput)must also be overridden to match that behavior.- Specified by:
writeExternalin interfaceExternalizable- Parameters:
out- the stream to write the object to- Throws:
IOException- Includes any I/O exceptions that may occur
-
readExternal
Meant for deserialization using Fory. If a class overrides this with different behavior,writeExternal(ObjectOutput)must also be overridden to match that behavior.- Specified by:
readExternalin interfaceExternalizable- Parameters:
in- the stream to read data from in order to restore the object- Throws:
IOException- if I/O errors occurClassNotFoundException- If the class for an object being restored cannot be found.
-
getNode
-
getNodes
-
connectionExists
-
getEdge
-
equals
-
hashCode
-