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>>
edgeMap
protected OrderedMap<V,Node<V>>
vertexMap
-
Constructor Summary
Constructors Modifier Constructor Description protected
Graph()
protected
Graph(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.boolean
addVertex(V v)
Adds a vertex to the graph.void
addVertices(Collection<V> vertices)
Adds all the vertices in the collection to the graph.abstract Algorithms<V>
algorithms()
protected boolean
connectionExists(Node<V> u, Node<V> v)
boolean
contains(V v)
Check if the graph contains a vertex.protected abstract Graph<V>
createNew()
boolean
edgeExists(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.int
getEdgeCount()
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.boolean
isDirected()
Check if the graph is directed, that is whether the edges form an ordered pair or a set.protected abstract Connection<V>
obtainEdge()
void
removeAllEdges()
Removes all edges from the graph.void
removeAllVertices()
Removes all vertices and edges from the graph.protected boolean
removeConnection(Node<V> a, Node<V> b)
boolean
removeEdge(Edge<V> edge)
boolean
removeEdge(V v, V w)
Removes the edge from v to w from the graph.protected void
removeNode(Node<V> node)
boolean
removeVertex(V v)
Removes a vertex from the graph, and any adjacent edges.void
removeVertices(Collection<V> vertices)
Removes all the vertices in the collection from the graph, and any adjacent edges.int
size()
Get the number of vertices in the graph.void
sortEdges(Comparator<Connection<V>> comparator)
Sort the edges using the provided comparator.void
sortVertices(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
-