Package squidpony.squidai.graph
Class Graph<V>
java.lang.Object
squidpony.squidai.graph.Graph<V>
- Type Parameters:
V- the vertex type; oftenCoord
- Direct Known Subclasses:
DirectedGraph,UndirectedGraph
public abstract class Graph<V> extends Object
Abstract superclass of actual Graph types, like
DirectedGraph and UndirectedGraph.- Author:
- earlygrey
-
Field Summary
Fields Modifier and Type Field Description protected OrderedMap<Connection<V>,Connection<V>>edgeMapprotected OrderedMap<V,Node<V>>vertexMap -
Constructor Summary
Constructors Modifier Constructor Description protectedGraph()protectedGraph(Collection<V> vertices) -
Method Summary
Modifier and Type Method Description protected Connection<V>addConnection(Node<V> a, Node<V> b)protected Connection<V>addConnection(Node<V> a, Node<V> b, float weight)Connection<V>addEdge(V v, V w)Add an edge to the graph, from v to w.Connection<V>addEdge(V v, V w, float weight)Add an edge to the graph, from v to w and with the specified weight.booleanaddVertex(V v)Adds a vertex to the graph.voidaddVertices(Collection<V> vertices)Adds all the vertices in the collection to the graph.abstract Algorithms<V>algorithms()protected booleanconnectionExists(Node<V> u, Node<V> v)booleancontains(V v)Check if the graph contains a vertex.protected abstract Graph<V>createNew()booleanedgeExists(V v, V w)Check if the graph contains an edge from v to w.protected Connection<V>getEdge(Node<V> a, Node<V> b)Edge<V>getEdge(V v, V w)Retrieve the edge which is from v to w.intgetEdgeCount()Get the number of edges in the graph.SortedSet<? extends Edge<V>>getEdges()Get a collection containing all the edges in the graph.List<? extends Edge<V>>getEdges(V v)Get a collection containing all the edges which have v as a source.protected Node<V>getNode(V v)protected Collection<Node<V>>getNodes()SortedSet<V>getVertices()Get a collection containing all the vertices in the graph.booleanisDirected()Check if the graph is directed, that is whether the edges form an ordered pair or a set.protected abstract Connection<V>obtainEdge()voidremoveAllEdges()Removes all edges from the graph.voidremoveAllVertices()Removes 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.protected voidremoveNode(Node<V> node)booleanremoveVertex(V v)Removes a vertex from the graph, and any adjacent edges.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.
-
Field Details
-
Constructor Details
-
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
-
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
-
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
Removes the edge from v to w from the graph.- Parameters:
v- the source vertex of the edgew- the destination vertex of the edge- Returns:
- the edge if there exists an edge from v to w, or null if there is no edge
-
removeEdge
-
removeAllEdges
Removes all edges from the graph. -
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
Check if the graph contains an edge from v to w.- Parameters:
v- the source vertex of the edgew- the destination vertex of the edge- Returns:
- true if the edge is in the graph, false otherwise
-
getEdges
Get a collection containing all the edges which have v as a source.- Parameters:
v- the source vertex of all the edges- Returns:
- an unmodifiable collection of edges
-
getEdges
Get a collection containing all the edges in the graph.- Returns:
- an unmodifiable collection of all the edges in the graph
-
getVertices
Get a collection containing all the vertices in the graph.- Returns:
- an unmodifiable collection of all the vertices in the graph
-
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
Get the number of vertices in the graph.- Returns:
- the number of vertices
-
getEdgeCount
Get the number of edges in the graph.- Returns:
- the number of edges
-
getNode
-
getNodes
-
connectionExists
-
getEdge
-