Simple dependency graph
This is a simple dependency graph useful for determining the order to do a list of things that depend on certain items being done before they are.
To use, npm install dependency-graph
and then require('dependency-graph').DepGraph
Nodes in the graph are just simple strings.
addNode(name)
- add a node in the graphremoveNode(name)
- remove a node from the graphhasNode(name)
- check if a node exists in the graphaddDependency(from, to)
- add a dependency between two nodes (will throw an Error if one of the nodes does not exist)removeDependency(from, to)
- remove a dependency between two nodesdependenciesOf(name, leavesOnly)
- get an array containing the nodes that the specified node depends on (transitively). IfleavesOnly
is true, only nodes that do not depend on any other nodes will be returned in the array.dependantsOf(name, leavesOnly)
- get an array containing the nodes that depend on the specified node (transitively). IfleavesOnly
is true, only nodes that do not have any dependants will be returned in the array.overallOrder(leavesOnly)
- construct the overall processing order for the dependency graph. IfleavesOnly
is true, only nodes that do not depend on any other nodes will be returned.
var DepGraph = require('dependency-graph').DepGraph;
var graph = new DepGraph();
graph.addNode('a');
graph.addNode('b');
graph.addNode('c');
graph.addDependency('a', 'b');
graph.addDependency('b', 'c');
graph.dependenciesOf('a'); // ['c', 'b']
graph.dependenciesOf('b'); // ['c']
graph.dependantsOf('c'); // ['a', 'b']
graph.overallOrder(); // ['c', 'b', 'a']
graph.overallOrder(true); // ['c']