Directed Graph Map
Directed graph data structure implemented using native Map
and Set
objects. Similiar to multi-key maps or bidirectional maps.
const dg = ; // Adg; // ├── Xdg; // ├── Ydg; // └── Z dg; // X, Y, Z dgsize; // 3dgedges; // [['A', 'X'], ['A', 'Y'], ['A', 'Z']]dgsources; // ['A']dgtargets; // ['X', 'Y', 'z']
Install
yarn add directed-graph-map
Usage
const DirectedGraphMap = ; const dgm = 'A' 'B'; // A// └── B dgm; // true dgm; // A// └── B// └── C dgm; // truedgm; // new Set(['B']);dgm; // new Set(['C']);dgm; // new Set();dgm; // new Set();dgm; // new Set(['A']);dgm; // new Set(['B']); dgm; // B// └── C dgm; // falsedgm; // new Set(); dgm; // Empty dgm; // new Set();dgm; // false dgm; // A// └── B dgm; // true dgm; // Empty dgm; // false
API
Table of Contents
- DirectedGraphMap
- DirectedGraphMap#edges
- DirectedGraphMap#size
- DirectedGraphMap#sources
- DirectedGraphMap#targets
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 edgetarget
T Target of the edge
Returns void
removeEdge
Remove an edge from the graph map.
Parameters
source
S Source of the edgetarget
T Target of the edge
Returns void
hasEdge
Test if a edge exists in the graph map.
Parameters
source
S Source of the edgetarget
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