Package dev.roanh.gmark.util
Class UniqueGraph<V,E>
java.lang.Object
dev.roanh.gmark.util.UniqueGraph<V,E>
- Type Parameters:
V
- Type of the data stored at vertices.E
- Type of the data stored at edges.
- Direct Known Subclasses:
ConfigGraph
,EdgeGraph
,SchemaGraph
,SelectivityGraph
Utility class to model a graph with directed
edges. Both edges and nodes are assumed to be
unique, that is, no two edges or nodes exist
that cannot be distinguished. Both vertices
and edges support storing some form of data.
The data stored at nodes is assumed to uniquely
identify a given node. This class does not
perform a lot of integrity checks in order
to stay performant.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Represents a single directed edge in the graph that can have associated data.static class
Represents a single node in the graph that is uniquely identifiable by some data. -
Field Summary
Modifier and TypeFieldDescriptionprivate List<UniqueGraph.GraphEdge<V,
E>> List of all edges in this graph.private int
The ID to assign to the next node added to the graph.private Map<V,
UniqueGraph.GraphNode<V, E>> Map for efficient graph node lookup from the data stored a specific node.private List<UniqueGraph.GraphNode<V,
E>> List of all nodes in this graph. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addUniqueEdge
(UniqueGraph.GraphNode<V, E> source, UniqueGraph.GraphNode<V, E> target) Adds a new unique edge without any label from the given source node to the given target node to connect to.void
addUniqueEdge
(UniqueGraph.GraphNode<V, E> source, UniqueGraph.GraphNode<V, E> target, E data) Adds a new unique edge with the given label data from the given source node to the given target node to connect to.void
addUniqueEdge
(UniqueGraph.GraphNode<V, E> source, V target) Adds a new unique edge without any label from the given source node to the existing node associated with the given target data.void
addUniqueEdge
(UniqueGraph.GraphNode<V, E> source, V target, E data) Adds a new unique edge with the given label data from the given source node to the existing node associated with the given target data.void
addUniqueEdge
(V source, UniqueGraph.GraphNode<V, E> target) Adds a new unique edge without any label to this node from the existing node associated with the given source data.void
addUniqueEdge
(V source, UniqueGraph.GraphNode<V, E> target, E data) Adds a new unique edge with the given label data to the given target node from the existing node associated with the given source data.void
addUniqueEdge
(V source, V target) Adds a new unique edge without any label from the node associated with the given source data to the existing node associated with the given target data.void
addUniqueEdge
(V source, V target, E data) Adds a new unique edge with the given label data from the node associated with the given source data to the existing node associated with the given target data.addUniqueNode
(V data) Adds a new unique node to this graph that is associated with the given data.copy()
Makes a deep copy of this graph.Gets the unlabelled edge connecting the node associated with the given source data and the node associated with the given target data.Gets the edge with the given label data connecting the node associated with the given source data and the node associated with the given target data.int
Gets the total number of edges in the graph.getEdges()
Gets a list of all edges in this graph.Gets a single node from this graph by the data that is stored at the node to find.int
Gets the total number of nodes in the graph.getNodes()
Gets a list of all nodes in this graph.removeNodeIf
(Predicate<UniqueGraph.GraphNode<V, E>> predicate) Removes all nodes from the graph that match the given predicate.int[][]
Computes the adjacency list representation of this graph.
-
Field Details
-
nodeMap
Map for efficient graph node lookup from the data stored a specific node. Note that it is assumed that there are no nodes that store the same data. -
nodes
List of all nodes in this graph. -
edges
List of all edges in this graph. -
nextNodeID
private int nextNodeIDThe ID to assign to the next node added to the graph.
-
-
Constructor Details
-
UniqueGraph
public UniqueGraph()
-
-
Method Details
-
removeNodeIf
public List<UniqueGraph.GraphNode<V,E>> removeNodeIf(Predicate<UniqueGraph.GraphNode<V, E>> predicate) Removes all nodes from the graph that match the given predicate. The returned nodes still reference the remaining nodes in the graph but remaining graph nodes no longer reference the removed nodes.- Parameters:
predicate
- The predicate nodes should match to be removed.- Returns:
- A list of removed graph nodes.
-
getNodes
Gets a list of all nodes in this graph.- Returns:
- All nodes in this graph.
-
getEdges
Gets a list of all edges in this graph.- Returns:
- All edges in this graph.
-
getNode
Gets a single node from this graph by the data that is stored at the node to find. Note that nodes are assumed to store information that uniquely identifies them, meaning the given data uniquely identifies a single node.- Parameters:
data
- The data stored at the node to find.- Returns:
- The node that stores the given data or
null
if no such node exists.
-
addUniqueNode
Adds a new unique node to this graph that is associated with the given data. The data given is assumed to uniquely identify a single node in the whole graph. If this graph already contains a node associated with the same data then a new node is not added to the graph and the existing node is returned instead.- Parameters:
data
- The data to associated with the node.- Returns:
- The (newly added) node that is associated with the given data.
-
getEdgeCount
public int getEdgeCount()Gets the total number of edges in the graph.- Returns:
- The total number of edges in the graph.
-
getNodeCount
public int getNodeCount()Gets the total number of nodes in the graph.- Returns:
- The total number of nodes in the graph.
-
getEdge
Gets the unlabelled edge connecting the node associated with the given source data and the node associated with the given target data.- Parameters:
source
- The data to identify the source node.target
- The data to identify the target node.- Returns:
- The edge connecting the source and target node
or
null
if no such node exists.
-
getEdge
Gets the edge with the given label data connecting the node associated with the given source data and the node associated with the given target data.- Parameters:
source
- The data to identify the source node.target
- The data to identify the target node.data
- The edge label data.- Returns:
- The edge connecting the source and target node
or
null
if no such node exists.
-
addUniqueEdge
Adds a new unique edge with the given label data from the node associated with the given source data to the existing node associated with the given target data.- Parameters:
source
- The data to identify the node to connect from with.target
- The data to identify the node to connect to with.data
- The edge label data.
-
addUniqueEdge
Adds a new unique edge with the given label data from the given source node to the existing node associated with the given target data.- Parameters:
source
- The source node to add an edge from.target
- The data to identify the node to connect to with.data
- The edge label data.
-
addUniqueEdge
Adds a new unique edge with the given label data to the given target node from the existing node associated with the given source data.- Parameters:
source
- The data to identify the node to connect from with.target
- The target node to add an edge to.data
- The edge label data.
-
addUniqueEdge
Adds a new unique edge without any label from the node associated with the given source data to the existing node associated with the given target data.- Parameters:
source
- The data to identify the node to connect from with.target
- The data to identify the node to connect to with.
-
addUniqueEdge
Adds a new unique edge without any label from the given source node to the existing node associated with the given target data.- Parameters:
source
- The source node to add an edge from.target
- The data to identify the node to connect to with.
-
addUniqueEdge
Adds a new unique edge without any label to this node from the existing node associated with the given source data.- Parameters:
source
- The data to identify the node to connect from with.target
- The target node to add an edge to.
-
addUniqueEdge
Adds a new unique edge without any label from the given source node to the given target node to connect to.- Parameters:
source
- The source node to add an edge from.target
- The target node to add an edge to.
-
addUniqueEdge
public void addUniqueEdge(UniqueGraph.GraphNode<V, E> source, UniqueGraph.GraphNode<V, E> target, E data) Adds a new unique edge with the given label data from the given source node to the given target node to connect to.- Parameters:
source
- The source node to add an edge from.target
- The target node to add an edge to.data
- The edge label data.
-
toAdjacencyList
public int[][] toAdjacencyList()Computes the adjacency list representation of this graph. For this the unique ID of every graph node is used (seeUniqueGraph.GraphNode.getID()
. The adjacency list is returned as a 2-dimensional array, the first dimension has as many indices as there were nodes added to the graph. Each of these indices corresponds to the unique ID of one of the nodes in the graph. Indices for nodes no longer in the graph will have a value ofnull
. All other indices have an array with the IDs of all the nodes the node has an edge to.- Returns:
- The adjacency list representation of this graph.
-
copy
Makes a deep copy of this graph.- Returns:
- The copy of this graph.
-