Skip to content

Simple DAG (Directed Acyclic Graph) with edge tagging

License

Notifications You must be signed in to change notification settings

cjongseok/dag.js

Repository files navigation

dag.js npm version Build Status Coverage Status

Simple DAG (Directed Acyclic Graph) module with edge tagging.

Install

$ npm install dagjs

Usage

let Dag = require('dagjs');

let dag = new Dag();
// ...

Analytics

Examples

Adding edges:

let dag = new Dag();
// add(from, to, tags, weight)
dag.add('Mike', 'Josh', 'follows', 3);
dag.add('Mary', 'Josh', ['follows', 'likes'], 50);
dag.add('Josh', 'John', ['follows', 'admires']);
dag.add('Mike', 'Mary', 'likes', 100);
// It results in a DAG:
//           follows        admires
//     Mike ----3---> Josh --------> John
//       |             ^
//       |             |
//       |            50 follows and likes
//       |   likes     |
//       -----100---> Mary

Filtering by tag:

let likeDag = dag.filterByTag('likes');
// likeDag =
//       likes         follows and likes
// Mike --100--> Mary ---------50--------> Josh

Neighbouring:

let edgesToJosh = dag.edgesTo('Josh');
// edgesToJosh =
// [
//      {from:'Mike', to:'Josh', tags:['follows'], weight:3},
//      {from: 'Mary', to:'Josh', tags:['follows', 'likes'], weight: 50}
// ]

let edgesFromMary = dag.edgesFrom('Mary');
// edgesFromMary =
// [
//      {from: 'Mary', to:'Josh', tags:['follows', 'likes'], weight: 50}
// ]

let neighbourhoodOfJosh = dag.neighbourhood('Josh');
// neighbourhoodOfJosh =
//           follows        admires
//     Mike ----3---> Josh --------> John
//                     ^
//                     |
//                    50 follows and likes
//                     |
//                    Mary

Clones:

// shallow-clone
let shallowDag = dag.clone();

// deep-clone
let deepDag = dag.deepClone();

About

Simple DAG (Directed Acyclic Graph) with edge tagging

Resources

License

Stars

Watchers

Forks

Packages

No packages published