@sha1n/dagraph
v0.1.0
Published
Directed acyclic graph utility in TypeScript
Downloads
135
Maintainers
Readme
DAGraph
A direct acyclic graph implementation
Features
- Hosts your data in the graph structure
- Topological sort traversal generator
- Traverse root nodes generator
- Reverse graph API
Usage
Basic
import createDAG from '@sha1n/dagraph';
const dag = createDAG();
dag.addNode({id : 'a'});
dag.addEdge({id : 'b'}, {id : 'a'});
dag.addEdge({id : 'c'}, {id : 'a'});
dag.addEdge({id : 'd'}, {id : 'b'});
for (const node of dap.topologicalSort()) {
...
}
Custom Objects
type MyThing = {
id: string; // <-- implicitly implements the 'Identifiable' interface
name: string;
doSomething(): void;
};
const myThing = {
id: 'a',
name: 'my thing',
doSomething: () => {
console.log('A');
}
};
const myOtherThing = {
id: 'b',
name: 'my other thing',
doSomething: () => {
console.log('B');
}
};
const myDAG = createDAG<MyThing>();
myDAG.addEdge(myThing, myOtherThing);
Install
yarn install @sha1n/dagraph
npm i @sha1n/dagraph