This is a `playground`

to test code. It runs a full `Node.js`

environment and already has all of `npm`

’s 400,000 packages pre-installed, including `ngraph.generators`

with all `npm`

packages installed. **Try it out**:

var ngraphGenerators = require("ngraph.generators")

- all documents on RunKit are public
`require()`

any package directly from npm- use arrow functions, classes, template strings, and most of ES6
`await`

any promise instead of using callbacks (example)- create your own embedded
**node.js**snippets

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

This module generates various graphs. It is part of larger ngraph family. If you need something not as simple as generated graphs, please check out ngraph.sparce-collection repository which contains graphs from University of Florida collection.

All images below are clickable and point to interactive 3d visualization, done by `ngraph.three`

library.

```
// Creates a ladder with 10 steps
var graph = require('ngraph.generators').ladder(10);
```

```
// Creates complete graph K6
var graph = require('ngraph.generators').complete(6);
```

```
// Creates complete bipartite graph K 3,3.
var graph = require('ngraph.generators').completeBipartite(3, 3);
```

```
// Creates balanced binary tree with n levels.
var graph = require('ngraph.generators').balancedBinTree(5);
```

```
// Generates a path-graph with 10 steps.
var graph = require('ngraph.generators').path(10);
```

```
// Generates a graph in a form of a circular ladder with 5 steps.
var graph = require('ngraph.generators').circularLadder(5);
```

```
// Generates a graph in a form of a grid with 10 rows and 10 columns.
var graph = require('ngraph.generators').grid(10, 10);
```

```
// Generates a graph in a form of a 3d grid with 5 rows and 5 columns and 5 levels.
var graph = require('ngraph.generators').grid3(5, 5, 5);
```

```
// Creates graph with 100 nodes and 0 links
var graph = require('ngraph.generators').noLinks(100);
```

```
var cliqueCount = 10;
var cliqueSize = 5;
// create a circle, with `cliqueCount` nodes. Each node is a fully connected
// graph with `cliqueSize` nodes
var graph = require('ngraph.generators').cliqueCircle(cliqueCount, cliqueSize);
```

This is a "small world" random graph, generated by Watts and Strogatz model. In this model generator takes three arguments:

`n`

- number of nodes`k`

- number of edges for each node. Originally node is connected with`k`

nearest neighbours on a circle graph`b`

- probability of an edge rewrite. In other words node changes it's nearest neighbor to a random node inside graph with probability`b`

.

```
// Creates graph with 100 nodes, each node is connected with 20 neighbours,
// and probability of neighbour to be outside of local node community is 1%.
var g1 = require('ngraph.generators').wattsStrogatz(100, 20, 0.00);
var g2 = require('ngraph.generators').wattsStrogatz(100, 20, 0.01);
var g3 = require('ngraph.generators').wattsStrogatz(100, 20, 0.10);
var g4 = require('ngraph.generators').wattsStrogatz(100, 20, 0.50);
var g5 = require('ngraph.generators').wattsStrogatz(20, 4, 0.02);
```

`createGraph()`

By default this library uses `ngraph.graph`

module to create new instances
of a graph. If you want to use your own module, you can use `factory`

method:

```
var generate = require('ngraph.generators').functory(function createGraph() {
// the following methods are required from the createGraph api:
return {
addLink(from, to) {
// ...
},
addNode(nodeId) {
},
getNodesCount() {
}
}
});
// now generators have the same methods as regular ngraph.generators:
var graph = generate.ladder(3);
generate.grid(10, 10);
generate.balancedBinTree(4);
// etc.
```

- package on npmhttps://npmjs.com/package/ngraph.generators
- licenseBSD-3-Clause

RunKit is a free, in-browser JavaScript dev environment for prototyping **Node.js** code, with every **npm** package installed. Sign up to share your code.

Sign Up for Free