Package dev.roanh.gmark.util
Class SimpleGraph<T extends IDable,M>
java.lang.Object
dev.roanh.gmark.util.SimpleGraph<T,M>
- Type Parameters:
T
- The vertex data typeM
- The metadata data type
Implementation of a simple graph that only supports unlabelled undirected edges.
Parallel edges are not supported, but self loops are allowed. Vertices in the
graph are uniquely identified by some piece of data.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
SimpleGraph.SimpleEdge<T extends IDable,
M> Represents a single undirected edge between two nodes in the graph.static final class
SimpleGraph.SimpleVertex<T extends IDable,
M> Represents a single vertex in the graph. -
Field Summary
-
Constructor Summary
ConstructorDescriptionSimpleGraph
(int vertexCount) Constructs a new simple graph with capacity for the given number of vertices. -
Method Summary
Modifier and TypeMethodDescriptionAdds a new edge to this graph between the given two vertices.addEdge
(SimpleGraph.SimpleVertex<T, M> a, T b) Adds a new edge to this graph between the given vertex and the vertex associated with the given data.Adds a new edge to this graph between the vertices identified by the given pieces of data.Adds a new vertex to this graph with the given piece of uniquely identifying data.void
deleteVertex
(SimpleGraph.SimpleVertex<T, M> vertex) Deletes the given vertex from the graph.void
Deletes all edges in this graph.int
Gets the total number of edges in this graph.getEdges()
Gets all the edges in this graph.Gets a vertex from this graph using the data that uniquely identifies it.int
Gets the total number of vertices in this graph.Gets all the vertices in this graph.Converts this simple graph to a unique graph instance.
-
Field Details
-
vertexMap
Map from identifying data to associated vertex. -
edges
Set of all edges in the graph. -
vertices
private int verticesThe total number of vertices in this graph.
-
-
Constructor Details
-
SimpleGraph
public SimpleGraph(int vertexCount) Constructs a new simple graph with capacity for the given number of vertices. Note that all vertices added should have an ID that is lower than the vertex count.- Parameters:
vertexCount
- The number of vertices to allocate space for.
-
-
Method Details
-
deleteVertex
Deletes the given vertex from the graph.- Parameters:
vertex
- The vertex to delete
-
getVertex
Gets a vertex from this graph using the data that uniquely identifies it.- Parameters:
data
- The data uniquely identifying the requested vertex.- Returns:
- The vertex associated with the passed data.
-
getEdges
Gets all the edges in this graph.- Returns:
- All the edges in this graph.
-
addVertex
Adds a new vertex to this graph with the given piece of uniquely identifying data.- Parameters:
data
- The data that uniquely identifies this vertex,- Returns:
- The newly added vertex or the already existing vertex if a vertex identified by the same piece of data was already present in the graph.
-
addEdge
Adds a new edge to this graph between the vertices identified by the given pieces of data.- Parameters:
a
- The data uniquely identifying the first vertex.b
- The data uniquely identifying the second vertex.- Returns:
- The newly added edge.
-
addEdge
Adds a new edge to this graph between the given vertex and the vertex associated with the given data.- Parameters:
a
- The first vertexb
- The data uniquely identifying the second vertex.- Returns:
- The newly added edge.
-
addEdge
public SimpleGraph.SimpleEdge<T,M> addEdge(SimpleGraph.SimpleVertex<T, M> a, SimpleGraph.SimpleVertex<T, M> b) Adds a new edge to this graph between the given two vertices.- Parameters:
a
- The first vertex.b
- The second vertex.- Returns:
- The newly added edge.
-
getVertexCount
public int getVertexCount()Gets the total number of vertices in this graph.- Returns:
- The total number of vertices in this graph.
-
getEdgeCount
public int getEdgeCount()Gets the total number of edges in this graph.- Returns:
- The total number of edges in this graph.
-
getVertices
Gets all the vertices in this graph.- Returns:
- All the vertices in this graph.
-
toUniqueGraph
Converts this simple graph to a unique graph instance. For this conversion edges will remain unlabelled and a directed edge will be added in both directions between vertices that are connected in this graph.- Returns:
- The newly constructed unique graph instance.
-
dropAllEdges
public void dropAllEdges()Deletes all edges in this graph.
-