Install from npm:
npm install jsnetworkx
Download jsnetworkx.js and include it in your page with
This will create the global variable
jsnx, with which all functions can be
JSNetworkX consists of multiple parts which work closely together:
- Graph classes (
MultiDiGraph) to model the data
- Graph generators for common graphs
- Various graph algorithms
- Graph visualization (in the browser)
Most classes and functions are available on the root object (
require('jsnetworkx') in Node).
Information about which algorithms are available and the API of the classes, can be found in the auto-generated API documentation.
// var jsnx = require('jsnetworkx'); // in Node// a tree of height 4 with fan-out 2var G = jsnx;// Compute the shortest path between node 2 and 7var path = jsnx;// [ 2, 0, 1, 3, 7 ]// or asynchronouslyjsnx;
More examples can be found on the website.
All the algorithms are implemented in a synchronous fashion (for now at least).
However, many algorithms are also available as asynchronous version. Their
gen<SyncFunctionName> (see example above) and they return a
This is achieved in browsers by creating a WebWorker. The WebWorker has
to be passed the path to the
jsnetworkx.js file. You have to set the path
explicitly if the file is not located at the root:
jsnxworkerPath = '/path/to/jsnetworkx.js';
In Node, a subprocess will be spawned (no setup is required).
Caveat: In both cases the input data has to be serialized before it can be sent to the worker or subprocess. However, not every value can be serialized, in which case JSNetworkX will use the synchronous version instead. If you encounter a situation where a value is not serialized, but it should be serializable, please file an issue.
If those are not available to you, JSNetworkX provides two helper methods for
iterating iterables and converting them to arrays:
How to contribute
You can contribute by:
- Porting code from Python
- Improving the documentation/website
If you plan on converting/porting a specific part, please create an issue beforehand.
To build JSNetworkX, all dependencies have to be installed via
Build for the browser
npm run build:browser
jsnetworkx.js, a minified version for production.
npm run build:browser:dev npm run watch:browser
jsnetworkx-dev.js, an unminified version with inline source maps for
development. The second version automatically rebuilds the file on change.
Build for Node
npm run build:node
Transforms all modules to ES5 and saves them inside the
npm run build:node:dev
Same as above but with inline source maps. These modules are also used to tun the unit tests.
npm run watch:node
Incrementally transform modules when files change.
Create and run tests
Tests are stored in the respective
__tests__ directories and have to follow
the naming convention
<testname>-test.js. The tests can be run with
npm test # or npm run test:fast # if you also run `npm run watch:node`
This will run all tests by default. To consider only those files whose path
matches a specific string, pass the
# Runs all digraph tests but no graph tests npm run test:fast -- -g digraph
The difference between
npm test and
npm run test:fast is that the former
will always transplile all files from ES6 to ES5 first. This is slow and
annoying during development. Therefore you can use
npm run watch:node
to automatically convert only the changed file and run
npm run test:fast to
quickly test them.
Ideally, every module has corresponding unit test. If you port a module from NetworkX, make sure to implement the same tests.
We use istanbul to generate a coverage report. We are not enforcing any coverage yet, but there should not be a regression. The report can be created via
npm run cover
and written to