NodeNameType
- The type for the node namespublic class DenseMemoryGraph<NodeNameType> extends java.lang.Object implements DirectedNodeEdgeGraph<NodeNameType>, java.io.Serializable
Constructor and Description |
---|
DenseMemoryGraph()
Initializes an empty graph.
|
DenseMemoryGraph(int expectedNumNodes,
int expectedNumEdges)
Initializes an empty graph, but sets aside O(m + n) memory.
|
Modifier and Type | Method and Description |
---|---|
void |
addEdge(NodeNameType left,
NodeNameType right)
Adds a directed edge from left to right.
|
void |
addNode(NodeNameType node)
Adds the input node to the graph.
|
void |
clear()
Clears the graph back to the original, empty state
|
boolean |
containsNode(NodeNameType node)
Returns whether or not the graph contains the specified vertex.
|
static DenseMemoryGraph<?> |
deserialize(java.lang.String fileName)
Helper method for deserializing from a file
|
Pair<java.lang.Integer,java.lang.Integer> |
getEdgeEndpointIds(int i)
Helper that returns the endpoint values for the input edge id.
|
protected int |
getFirstEdgeFrom(int nodeVal)
Helper that returns the first edge that node nodeVal (id) participates
in.
|
NodeNameType |
getNode(int nodeId)
Helper that returns the node for the internal ID.
|
int |
getNodeId(NodeNameType node)
Helper that returns the internal ID for the input node.
|
java.util.Collection<NodeNameType> |
getNodes()
Returns all of the nodes contained in the graph.
|
int |
getNumEdges()
Return the number of edges.
|
int |
getNumNodes()
Return the number of nodes
|
java.util.Collection<NodeNameType> |
getSuccessors(NodeNameType node)
Returns all the nodes this node connects to (outgoing edges only).
|
protected void |
optimizeEdges()
This private helper sorts all edges for quicker edge-lookup times.
|
static void |
serialize(java.lang.String fileName,
DenseMemoryGraph<?> graph)
Helper method for serializing the input to file.
|
protected void |
swap(int i,
int j)
Helper that swaps the values at i and j in the edge list (handles the
fact that the edge list is 2 values in a row for each edge).
|
public DenseMemoryGraph()
public DenseMemoryGraph(int expectedNumNodes, int expectedNumEdges)
expectedNumNodes
- The expected number of nodes for the graphexpectedNumEdges
- The expected number of edges for the graphpublic java.util.Collection<NodeNameType> getNodes()
DirectedNodeEdgeGraph
getNodes
in interface DirectedNodeEdgeGraph<NodeNameType>
Execution: O(1) time
public int getNumNodes()
DirectedNodeEdgeGraph
getNumNodes
in interface DirectedNodeEdgeGraph<NodeNameType>
Execution: O(1) time
public int getNumEdges()
DirectedNodeEdgeGraph
getNumEdges
in interface DirectedNodeEdgeGraph<NodeNameType>
Execution: O(1) time
public void addEdge(NodeNameType left, NodeNameType right)
DirectedNodeEdgeGraph
addEdge
in interface DirectedNodeEdgeGraph<NodeNameType>
left
- The source of the edgeright
- The destination of the edgeExecution: O(1) time
public void addNode(NodeNameType node)
DirectedNodeEdgeGraph
DirectedNodeEdgeGraph.addEdge(Object, Object)
, it handles adding new nodes.addNode
in interface DirectedNodeEdgeGraph<NodeNameType>
node
- The node to add to the graphExecution: O(1) time
public boolean containsNode(NodeNameType node)
DirectedNodeEdgeGraph
containsNode
in interface DirectedNodeEdgeGraph<NodeNameType>
node
- The node to check forExecution: O(1) time
public java.util.Collection<NodeNameType> getSuccessors(NodeNameType node)
DirectedNodeEdgeGraph
getSuccessors
in interface DirectedNodeEdgeGraph<NodeNameType>
node
- The node whose successors are requestedExecution: O(log m + d) where d is the degree of the input node (d can be
O(m) but usually is closer to O(1)) (With a O(m log m) one-time cost
after edges are newly added)
public int getNodeId(NodeNameType node)
getNodeId
in interface DirectedNodeEdgeGraph<NodeNameType>
node
- The node to search forpublic NodeNameType getNode(int nodeId)
getNode
in interface DirectedNodeEdgeGraph<NodeNameType>
nodeId
- The id for the sought-after nodepublic Pair<java.lang.Integer,java.lang.Integer> getEdgeEndpointIds(int i)
getEdgeEndpointIds
in interface DirectedNodeEdgeGraph<NodeNameType>
i
- The edge id to search forprotected int getFirstEdgeFrom(int nodeVal)
nodeVal
- The id of the node whose first edge is soughtprotected void optimizeEdges()
protected void swap(int i, int j)
i
- The edge index to swap with jj
- The edge index to swap with ipublic void clear()
DirectedNodeEdgeGraph
clear
in interface DirectedNodeEdgeGraph<NodeNameType>
gov.sandia.graph.IDirectedNodeEdgeGraph#clear()
public static void serialize(java.lang.String fileName, DenseMemoryGraph<?> graph)
fileName
- The filename (with optional path) to write this out tograph
- The graph to writepublic static DenseMemoryGraph<?> deserialize(java.lang.String fileName)
fileName
- The file (with path if necessary) to read a graph from