Class: TheDAG

TheDAG()

new TheDAG()

Source:
Example
let a = new TheDAG()

Methods

addEdges(Edges) → {Void}

Adds Edges to graph.

Parameters:
Name Type Description
Edges Array.<Object>
Properties
Name Type Description
PartialEdge Object

{ source, target, edgeData }

PartialEdge.source Node | string

Node { nodeID, ...otherStuff} or nodeID string

PartialEdge.target Node | string

Node { nodeID, ...otherStuff} or nodeID string

PartialEdge.edgeData any
Source:
Returns:
Type
Void

addNodes(nodes) → {Void}

Parameters:
Name Type Description
nodes nodes
Properties
Name Type Description
node Node

{ nodeID, nodeData }

Properties
Name Type Description
nodeID string
nodeData any
Source:
Returns:
Type
Void

destroy() → {Void}

Clears nodes and edges in state.

Source:
Returns:
Type
Void

getDistanceTo(input) → {number}

Parameters:
Name Type Description
input Object

sourceNodeID and targetNodeID

Properties
Name Type Description
sourceNodeID string

ID of the source node

targetNodeID string

ID of the target node

Source:
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
Name Type Description
source Node | string

Node { nodeID, ...otherStuff} or nodeID string

target Node | string

Node { nodeID, ...otherStuff} or nodeID string

Source:
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
Name Type Description
source Node | string

Node { nodeID, ...otherStuff} or nodeID string

target Node | string

Node { nodeID, ...otherStuff} or nodeID string

Source:
Returns:

edgeID

Type
string

getEdges() → {Edges|Edge}

Gets all edges in graph

Source:
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
Name Type Description
nodeID string

nodeID

Source:
Returns:

{ nodeID, nodeData }

Type
Node

getNodes() → {Nodes|Node}

Get all nodes in graph

Source:
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
Name Type Description
sourceNodeID string

ID of the source node

hops string

Distance or number of hops from source node

Source:
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
Name Type Description
node any
edges Array.<any>
Properties
Name Type Description
edge any
nodeIDGenerator function

input : nodes[].node output: nodeID

edgeSourceIDGenerator function

input : edges[].edge output: edgeSourceID

edgeTargetIDGenerator function

input : edges[].edge output: edgeTargetID

Source:
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.

Source:
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}

Source:
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
Name Type Description
startingNodeID string

ID of the node from which to start traversing

visitNode function

function called everytime a new node is traversed

Source:
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
Name Type Description
startingNodeID string

ID of the node from which to start traversing

visitNode function

function called everytime a new node is traversed

Source:
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
Name Type Description
startingNodeID string

ID of the node from which to start traversing

visitNode function

function called everytime a new node is traversed

Source:
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
Name Type Description
startingNodeID string

ID of the node from which to start traversing

visitNode function

function called everytime a new node is traversed

Source:
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'
   );
 }