API documentation

Documentation about all available methods can be found on the API page.

Node values: How different data types are handled

Node values are used internally as keys, e.g. nodeValue -> nodeAttribute or nodeValue -> neighbors. There are also methods which return nodeValue -> result mappings, such as G.degree().

Since v0.2.0, instead of simple objects we use a custom Map implementation which preservers the actual node values. This is how different data types are treated:

Numbers and strings work as expected:

var G = new jsnx.Graph();
G.addNodesFrom([1,2,3, "foo"]);
// [1, 2, 3, "foo"]

For everything else, such as objects and arrays a string key is generated by calling the object's toString method. This means that any two objects are considered equal if their toString method returns the same value. It also has the nice side-effect that simple arrays (arrays containing primitive values, like numbers or string) behave like Python's tuples.


// Use arrays as tuples
var G = new jsnx.Graph();
G.addNode([1,2], {data: 'foo'});
console.log(G.node.get([1,2]).data); // because [1,2].toString() === "1, 2"
// "foo"
Note: The built-in array toString method does not format string values in any special way. That means that [1,2] and ["1","2"] cannot be distinguished, since both produce the same string "1, 2".
// Overwrite toString of objects
var nodeA = {toString: function() { return 'nodeA'; }};
var nodeB = {toString: function() { return 'nodeB'; }};

var G = new jsnx.Graph();
G.addNodesFrom([nodeA, nodeB]);
// [Object, Object]


Many methods in JSNetworkX either accept or return an iterable/iterator. It follows the Iterable and Iterator protocols of the next JavaScript version, ES2015.

For example, each graph object is an iterable, which returns an iterator over its nodes:

// ES2015 for/of loop to iterate over iterables
var G = jsnx.pathGraph(3);
for (var n of G) {
// logs 0, 1, 2

In case you cannot use for/of loops, JSNetworkX exposes a forEach method to conveniently iterate over iterables:

jsnx.forEach(G, function(n) {

It also exposes a toArray method for converting iterables to arrays, in case the environment does not support Array.from:

// [0, 1, 2, 3, 4]

Asynchronous Functions

Graph algorithms can take a long time to run, especially on larger graphs. This is usually not a problem for code running on the server (though it can be), but it is definitely a problem in the browser, where the run time per script is limited.

To solve that issue, most algorithms are provided as synchronous and as asynchronous functions. The asynchronous function has the same name as the synchronous function, only prefixed with gen, and returns a Promise.

You may have to set jsnx.workerPath to the path of the jsnetworkx.js file in order for the WebWorker to work.

In the browser, the asynchronous version will create a WebWorker, if available.

In Node.js, the asynchronous version will spawn a new process.

// synchronous
var cliques = jsnx.findCliques(G);

// asynchronous
jsnx.genFindCliques(G).then(function(cliques) {

Because it is not decided yet how asynchronous iterators should be consumed, JSNetworkX will return an array from an asynchronous method, if its synchronous version returns an iterator.

Optional arguments

Some methods have one or more optional arguments. Optional arguments can be omitted, as long as the arguments are of different types and therefore distinguishable.

For example, this is OK:


but this is ambiguous:

G.degree('weight'); // get node with name 'weight' or is the weight attribute name 'weight' ?

In this case we decided to interpret the argument as node name. To set the weight attribute name and to get all nodes, null or undefined has to be passed as first argument:

G.degree(null, 'weight');

Migration from v0.1.1 to v0.2.0

v0.1.1 had the disadvantage that every node value was implicitly converted to a string and the original value could not be restored (without additional, external logic).

In v.0.2.0, instead of using simple objects to store node values, JSNetworkX uses instances of jsnx.contrib.Map.

This means that whenever a node was used as a property name, like


the .get() method has to be used instead:


Instead of assigning to the property:

degrees[n] = 42;

the .set() method has to be used:

degress.set(n, 42);

If you use any of the following properties or methods, then you will have to adjust the code accordingly.

Changed properties
These properties hold a map instead of an object keyed by node.

  • Graph#node
  • Graph#adj
  • Graph#edge
  • DiGraph#succ
  • DiGraph#pred

Changed methods/functions
These methods/functions return a map instead of an object keyed by node or edge.

  • Graph#degree
  • Graph#get (previously Graph#get_node)
  • DiGraph#in_degree
  • DiGraph#out_degree
  • jsnx.all_pairs_shortest_path
  • jsnx.all_pairs_shortest_path_length
  • jsnx.betweenness_centrality
  • jsnx.clustering
  • jsnx.degree
  • jsnx.edge_betweenness_centrality
  • jsnx.get_edge_attributes
  • jsnx.get_node_attributes
  • jsnx.number_of_cliques
  • jsnx.predecessor
  • jsnx.single_source_shortest_path
  • jsnx.single_source_shortest_path_length
  • jsnx.square_clustering
  • jsnx.triangles

No adjustments have to be made for graph creation. The following will still work as expected, because numeric property names of objects containing node data are implicitly converted to numbers.

var G = jsnx.Graph({1: [2, 3], 2: [3]});