项目作者: TimeWz667

项目描述 :
A library for (di)graph creating, analysing, and inference
高级语言: JavaScript
项目地址: git://github.com/TimeWz667/infergraph.js.git
创建时间: 2018-04-17T13:49:50Z
项目社区:https://github.com/TimeWz667/infergraph.js

开源协议:MIT License

下载


infergraph.js

A library for (di)graph creating, analysing, and inference.

Get Started

Load

  1. var ig = require("infergraph.js");

Undirected graph

Generate a graph

  1. var g = ig.newGraph();

Add nodes

infergraph.js provides three ways to add a node

  1. // 1. By name
  2. g.addNode("Node1");
  3. // 2. By name and attributes
  4. g.addNode(["Node2", {A1: "a1", A2: "a2"}]);
  5. // 3. By dictionary ("id" is necessary)
  6. g.addNode({id: "Node3", A1: "a1", A2: "a2"});

Adding many nodes at once is possible

  1. g.addNodes(["Node1", ["Node2", {A1: "a1"}], {id: "Node3", A1: "a2"}]);

Get nodes and edit attributes of nodes

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

  1. var node = g.getNode("Node1")
  2. .attr("A1", 1) // edit an attribute with a value
  3. .attr("A2", 2)
  4. .attr("A3", d => d.A1 + d.A2) // edit an attribute with a function
  5. .get(); // finally get the node

Or getting and editing many nodes at one time by names or a filter function.

  1. var nodes = g.getNodes(["Node1", "Node2"])
  2. .attr("A1", 1)
  3. .get(); // get all the selected nodes as an array
  4. g.getNodes(node => {...})
  5. .attr("A1", 1);

Add edges

Giving the names of source node, target node, and weight (optional, default=0), an edge will be build between nodes

  1. g.addEdge("Src1", "Tar1", weight);
  2. g.addEdge("Src2", "Tar2");

As in undirected graphs, you can add edges as cycles or complete graphs

  1. g.addCycle(["Node1", ..., "NodeN"], weight)
  2. g.linkCompletely(["Node1", ..., "NodeN"], weight)

Query relations

  1. // check if two nodes are neighbours to each other
  2. g.isNeighbour("Neighbour", "ofWhom");
  3. // get a selector with all neighbour nodes
  4. g.getNeighbours("ofWhom");
  5. // get a key list of all neighbour nodes
  6. g.getNeighbourKeys("ofWhom");

Directed graph (DiGraph)

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.

Query relations

You and find parents, children, ancestors, and descendants of nodes

  1. // check relations
  2. g.isParent("parent", "ofWhom");
  3. g.isChild("child", "ofWhom");
  4. g.isAncestor("ancestor", "ofWhom");
  5. g.isDescendant("descendant", "ofWhom");
  6. // get a selector with all neighbour nodes
  7. g.getParents("ofWhom");
  8. g.getChildren("ofWhom");
  9. g.getAncestors("ofWhom");
  10. g.getDescendants("ofWhom");
  11. // get a key list of all neighbour nodes
  12. g.getParentKeys("ofWhom");
  13. g.getChildKeys("ofWhom");
  14. g.getAncestorKeys("ofWhom");
  15. g.getDescendantKeys("ofWhom");

Query the order of a directed graph (acyclic graph only)

You can get the topological order of a DiGraph

  1. g.getOrder()

License

This project is licensed under the MIT License - see the LICENSE file for details