A library for (di)graph creating, analysing, and inference
A library for (di)graph creating, analysing, and inference.
var ig = require("infergraph.js");
var g = ig.newGraph();
infergraph.js provides three ways to add a node
// 1. By name
g.addNode("Node1");
// 2. By name and attributes
g.addNode(["Node2", {A1: "a1", A2: "a2"}]);
// 3. By dictionary ("id" is necessary)
g.addNode({id: "Node3", A1: "a1", A2: "a2"});
Adding many nodes at once is possible
g.addNodes(["Node1", ["Node2", {A1: "a1"}], {id: "Node3", A1: "a2"}]);
When you request nodes from a graph, infergraph.js will firstly render a selector.
With the selector, you can modify and query attributes of the targeted nodes.
You can edit the attributes of a node by chaining. Attributes can be values of output of functions
var node = g.getNode("Node1")
.attr("A1", 1) // edit an attribute with a value
.attr("A2", 2)
.attr("A3", d => d.A1 + d.A2) // edit an attribute with a function
.get(); // finally get the node
Or getting and editing many nodes at one time by names or a filter function.
var nodes = g.getNodes(["Node1", "Node2"])
.attr("A1", 1)
.get(); // get all the selected nodes as an array
g.getNodes(node => {...})
.attr("A1", 1);
Giving the names of source node, target node, and weight (optional, default=0), an edge will be build between nodes
g.addEdge("Src1", "Tar1", weight);
g.addEdge("Src2", "Tar2");
As in undirected graphs, you can add edges as cycles or complete graphs
g.addCycle(["Node1", ..., "NodeN"], weight)
g.linkCompletely(["Node1", ..., "NodeN"], weight)
// check if two nodes are neighbours to each other
g.isNeighbour("Neighbour", "ofWhom");
// get a selector with all neighbour nodes
g.getNeighbours("ofWhom");
// get a key list of all neighbour nodes
g.getNeighbourKeys("ofWhom");
The rules of adding nodes and edges in directed graph are exactly the same the undirected graph version.
However, the shortcut functions of addCycle and linkCompleted does not support DiGraph.
The major difference is the relation querying.
You and find parents, children, ancestors, and descendants of nodes
// check relations
g.isParent("parent", "ofWhom");
g.isChild("child", "ofWhom");
g.isAncestor("ancestor", "ofWhom");
g.isDescendant("descendant", "ofWhom");
// get a selector with all neighbour nodes
g.getParents("ofWhom");
g.getChildren("ofWhom");
g.getAncestors("ofWhom");
g.getDescendants("ofWhom");
// get a key list of all neighbour nodes
g.getParentKeys("ofWhom");
g.getChildKeys("ofWhom");
g.getAncestorKeys("ofWhom");
g.getDescendantKeys("ofWhom");
You can get the topological order of a DiGraph
g.getOrder()
This project is licensed under the MIT License - see the LICENSE file for details