This library contain an implementation of graph data structure with various method that can be useful in operations by graphs.
Graphs can directed and undirected, weighted and unweighted. So we have 4 variations and this library can works with them.
npm install graphs-all
Import the library
var graphs = ;var g = graphs;g;g;g;g;g // trueg // trueg;g // ["A", "B"]
Create a graph
You can create four variations of graphs with shortcuts functions:
var simpleGraph = graphsdirectedGraph = graphsweightedGraph = graphcomplexGraph = graph;
new Graph(isDirected, isWeighted)
var simpleGraph = false falsedirectedGraph = true falseweightedGraph = false truecomplexGraph = true true;
Add a node in the graph with key
nodeKey. If node is exist already,
false, for successive addition returns
simpleGraphsimpleGraph // truesimpleGraph // false
Check is a node with key
nodekey in the graph
simpleGraph // truesimpleGraph // false
Graph.addLink(key1, key2, weight)
Add a link between
key2 nodes. If nodes are not existed, then they are created.
For directed graphs, it's created a directed link from
For weighted graphs you can add the third argument
weight (default == 1).
weight must be a number.
simpleGraph;simpleGraph; // trueweightedGraph;
Check the existence of the link between
key2 nodes (the order is important only for directed graphs).
true if the link is exist,
Also you can use
linkWeight - it has the same interface and returns
undefined if the link doesn't exist,
otherwise the weight of the link (1 for unweighted graphs).
simpleGraph;simpleGraph; // trueweightedGraph;weightedGraph; // 5simpleGraph; // undefined
Get node list
Returns an array on node keys.
simpleGraph;simpleGraph;simpleGraph; // [1, 2, 3, 4]
Get node connections
Returns an array of keys of nodes, which are connected with the given node. For directed graphs return only nodes that are available from from the given node.
simpleGraph;simpleGraph;simpleGraph;simpleGraph; // ["E", D", "C"]directedGraph;directedGraph;directedGraph; // ["B"]
Removes the node from the graph, also remove all connected links (input and output).
directedGraph;directedGraph;directedGraph;directedGraph;directedGraph; // falsedirectedGraph; // falsedirectedGraph; // false
Removes the link from the graph. The nodes are not removed, only the link.
For directed graphs remove only one-way link from
simpleGraph;simpleGraph;simpleGraph; // falsesimpleGraph; // true