directed-graph-map

1.2.8 • Public • Published

Directed Graph Map

CircleCI npm version codecov

Directed graph data structure implemented using native Map and Set objects. Similiar to multi-key maps or bidirectional maps.

const dg = new DirectedGraphMap();
                      //  A
dg.addEdge('A', 'X'); //  ├── X
dg.addEdge('A', 'Y'); //  ├── Y
dg.addEdge('A', 'Z'); //  └── Z
 
dg.getTargets('A');   //  X, Y, Z
 
dg.size; // 3
dg.edges; // [['A', 'X'], ['A', 'Y'], ['A', 'Z']]
dg.sources; // ['A']
dg.targets; // ['X', 'Y', 'z']

Install

yarn add directed-graph-map

Usage

const DirectedGraphMap = require('directed-graph-map');
 
const dgm = new DirectedGraphMap([['A', 'B']]);
 
//  A
//  └── B
 
dgm.hasEdge('A', 'B'); // true
 
dgm.addEdge('B', 'C');
 
//  A
//  └── B
//      └── C
 
dgm.hasEdge('B', 'C'); // true
dgm.getTargets('A'); // new Set(['B']);
dgm.getTargets('B'); // new Set(['C']);
dgm.getTargets('C'); // new Set();
dgm.getSources('A'); // new Set();
dgm.getSources('B'); // new Set(['A']);
dgm.getSources('C'); // new Set(['B']);
 
dgm.removeSource('A');
 
//  B
//  └── C
 
dgm.hasEdge('A', 'B'); // false
dgm.getTargets('A'); // new Set();
 
dgm.removeTarget('C');
 
//  Empty
 
dgm.getTargets('B'); // new Set();
dgm.hasEdge('B', 'C'); // false
 
dgm.addEdge('A', 'B');
 
//  A
//  └── B
 
dgm.hasEdge('A', 'B'); // true
 
dgm.removeEdge('A', 'B');
 
//  Empty
 
dgm.hasEdge('A', 'B'); // false

API

Table of Contents

DirectedGraphMap

Class representing a Directed Graph Map

Parameters

  • edges Iterable<[S, T]> Iterable containing source -> target pairs (optional, default [])

addEdge

Add an edge to the graph map.

Parameters
  • source S Source of the edge
  • target T Target of the edge

Returns void

removeEdge

Remove an edge from the graph map.

Parameters
  • source S Source of the edge
  • target T Target of the edge

Returns void

hasEdge

Test if a edge exists in the graph map.

Parameters
  • source S Source of the edge
  • target T Target of the edge

Returns boolean Whether the edge exists in the graph map.

hasSource

Test if a source exists in the graph map.

Parameters
  • source S Source of the edge

Returns boolean

hasTarget

Test if a target exists in the graph map.

Parameters
  • target T Target of the edge

Returns boolean

removeSource

Remove all edges from a source.

Parameters
  • source S Source of the edge

Returns void

removeTarget

Remove all edges to a target.

Parameters
  • target T Target of the edge

Returns void

getSources

Get all sources with edges to a target.

Parameters
  • target T Target of the edge

Returns Set<S> Set of sources

getTargets

Get all targets with edges from a source.

Parameters
  • source S Source of the edge

Returns Set<T> Set of targets

DirectedGraphMap#edges

Array of edges

DirectedGraphMap#size

Edge count

DirectedGraphMap#sources

Set of sources

DirectedGraphMap#targets

Set of targets

Readme

Keywords

none

Package Sidebar

Install

npm i directed-graph-map

Weekly Downloads

11

Version

1.2.8

License

MIT

Unpacked Size

317 kB

Total Files

31

Last publish

Collaborators

  • wehriam