@sha1n/dagraph
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

CI Coverage GitHub npm type definitions npm

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

Package Sidebar

Install

npm i @sha1n/dagraph

Weekly Downloads

10

Version

0.1.0

License

MIT

Unpacked Size

13.7 kB

Total Files

6

Last publish

Collaborators

  • sha1n