Methods
addEdges(Edges) → {Void}
Adds Edges to graph.
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
Edges |
Array.<Object> |
Properties
|
||||||
PartialEdge.source |
Node | string | Node { nodeID, ...otherStuff} or nodeID string |
||||||
PartialEdge.target |
Node | string | Node { nodeID, ...otherStuff} or nodeID string |
||||||
PartialEdge.edgeData |
any |
Returns:
- Type
- Void
addNodes(nodes) → {Void}
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
nodes |
nodes |
Properties
|
Returns:
- Type
- Void
destroy() → {Void}
Clears nodes and edges in state.
Returns:
- Type
- Void
getDistanceTo(input) → {number}
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object | sourceNodeID and targetNodeID Properties
|
Returns:
numberOfHops - distance between nodes or number of hops required to go from source to target
- Type
- number
getEdge(VersatileEdge) → {Edge}
Gets edge by source and target nodes or nodeIDs
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
VersatileEdge |
Object | { source, target } Properties
|
Returns:
{ edgeID, nodeData, sourceID, targetID }
- Type
- Edge
getEdgeID(PartialEdge) → {string}
Gets Edge ID from partial edge information.
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
PartialEdge |
Object | { source, target } Properties
|
Returns:
edgeID
- Type
- string
getEdges() → {Edges|Edge}
Gets all edges in graph
Returns:
-
graph.edges
- Type
- Edges
-
graph.edges[edgeID] = { edgeID, nodeData, sourceID, targetID }
- Type
- Edge
getNode({nodeID}) → {Node}
Gets Node by nodeID.
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
{nodeID} |
Object | Any object that has nodeID in it. Properties
|
Returns:
{ nodeID, nodeData }
- Type
- Node
getNodes() → {Nodes|Node}
Get all nodes in graph
Returns:
-
graph.nodes
- Type
- Nodes
-
graph.nodes[nodeID] = { nodeID, nodeData }
- Type
- Node
getNodesByDistanceTo(input) → {Array.<Object>|string|number}
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object | sourceNodeID and hops Properties
|
Returns:
-
nodes - All node at distance to source = hops
- Type
- Array.<Object>
-
nodes[].nodeID
- Type
- string
-
nodes[].hops
- Type
- number
importGraph(nodes, edges, nodeIDGenerator, edgeSourceIDGenerator, edgeTargetIDGenerator) → {Void}
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
nodes |
Array.<any> |
Properties
|
||||||
edges |
Array.<any> |
Properties
|
||||||
nodeIDGenerator |
function | input : nodes[].node output: nodeID |
||||||
edgeSourceIDGenerator |
function | input : edges[].edge output: edgeSourceID |
||||||
edgeTargetIDGenerator |
function | input : edges[].edge output: edgeTargetID |
Returns:
- Type
- Void
isAcyclic() → {Object|boolean|Array.<Object>|string|string}
Checks if the graph in the object state is acyclic. If it is acyclic : it returns an array of topologically sorted nodes. If it is cyclic : it returns the ID of the node responsible for the cyclicity of the graph.
Returns:
-
acyclicityMetadata
- Type
- Object
-
acyclicityMetadata.isAcyclic
- Type
- boolean
-
acyclicityMetadata.topologicallySortedNodeIDs - A topologically sorted array of node IDs
- Type
- Array.<Object>
-
acyclicityMetadata.topologicallySortedNodeIDs[] - nodeID
- Type
- string
-
acyclicityMetadata.cyclicDependencyID - If the graph is cyclic, returns the node ID responsable for the cycle. This variable is only set if isAcyclic is false
- Type
- string
Example
const myDAG = new TheDAG();
// ... Add some nodes and edges
const { isAcyclic, topologicallySortedNodeIDs } = myDAG.isAcyclic()
toJS() → {Object|Nodes|Node|Edges|Edge}
Returns:
-
graph
- Type
- Object
-
graph.nodes
- Type
- Nodes
-
graph.nodes[nodeID] = { nodeID, nodeData }
- Type
- Node
-
graph.edges
- Type
- Edges
-
graph.edges[edgeID] = { edgeID, nodeData, sourceID, targetID }
- Type
- Edge
traverseBreadthFirst(input) → {Object|boolean|Object|string|string}
Traverses the graph breadth first returning all nodes resulting from the traversal in insertion order. Might result in poor performance on large graphs as it stores it all in memory before returning it.
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object |
Properties
|
Yields:
-
generatorOutput
- Type
- Object
-
generatorOutput.done - Reached sink, done traversing.
- Type
- boolean
-
generatorOutput.value - Node.
- Type
- Object
-
generatorOutput.value.nodeID - Traversed node ID.
- Type
- string
-
generatorOutput.value.nodeData - Traversed node data.
- Type
- string
(generator) traverseBreadthFirstGenerator(input) → {Object|boolean|Object|string|string}
Traverses the graph breadth first yielding nodes as it crawls it. Good for large graphs as it doesnt store it all in memory
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object |
Properties
|
Yields:
-
generatorOutput
- Type
- Object
-
generatorOutput.done - Reached sink, done traversing.
- Type
- boolean
-
generatorOutput.value - Node.
- Type
- Object
-
generatorOutput.value.nodeID - Traversed node ID.
- Type
- string
-
generatorOutput.value.nodeData - Traversed node data.
- Type
- string
(generator) traverseDepthFirstGenerator(input) → {Object|boolean|Object|string|string}
Traverses the graph depth first yielding nodes as it crawls it. Good for large graphs as it doesnt store it all in memory
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object |
Properties
|
Yields:
-
generatorOutput
- Type
- Object
-
generatorOutput.done - Reached sink, done traversing.
- Type
- boolean
-
generatorOutput.value - Node.
- Type
- Object
-
generatorOutput.value.nodeID - Traversed node ID.
- Type
- string
-
generatorOutput.value.nodeData - Traversed node data.
- Type
- string
(generator) traverseDynamicPathGenerator(input) → {Object|function|boolean|Object|string|string}
Allows you to direct the graph traversal, based on node data and possible targets.
Parameters:
Name | Type | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
input |
Object |
Properties
|
Yields:
-
generatorOutput
- Type
- Object
-
generatorOutput.next - First call to start the traversal based on the starting node ID provided. After that next requires the next nodeID to go to.
- Type
- function
-
generatorOutput.done - Reached sink, done traversing.
- Type
- boolean
-
generatorOutput.value - Node.
- Type
- Object
-
generatorOutput.value.nodeID - Traversed node ID.
- Type
- string
-
generatorOutput.value.nodeData - Traversed node data.
- Type
- string
Example
const nodeIterator = traverseDynamicPathGenerator({
startingNodeID: 1
});
expect(nodeIterator.next()).toMatchSnapshot('FSM started');
expect(nodeIterator.next(3)).toMatchSnapshot(
'Custom traversal depending on the passed node id'
);
try {
nodeIterator.next(10);
} catch (err) {
expect(err.message).toMatchSnapshot(
'Throws when trying to go unconnected node'
);
}