Generating Graphs
nodus.generate.* produces synthetic graphs procedurally, without any input
data. They’re ideal for demos, tests, benchmarks, and trying out layouts and
styling before you wire up real data. Every generator is deterministic and
seeded — the same inputs always produce the same graph — so they’re safe to use
in snapshot tests and reproducible examples.
Each generator is async. Generators return a { nodes, edges } graph —
pass it to setGraph to load it. The usual flow is
generate → setGraph → layout → frame.
The generators
| Generator | Shape it produces |
|---|---|
balancedTree | A tree where every internal node has the same number of children. |
barabasiAlbert | A scale-free network (preferential attachment) — a few high-degree hubs. |
erdosRenyi | A random graph where each possible edge exists with a fixed probability. |
flower | A regular “flower” / petal structure. |
grid | A regular lattice of rows and columns. |
path | A single chain of nodes connected end to end. |
random | A random graph from counts you supply. |
randomTree | A random tree (connected, acyclic). |
const grid = await nodus.generate.grid({ rows: 6, columns: 6 });const scaleFree = await nodus.generate.barabasiAlbert({ nodes: 40, m0: 2, m: 1 });const chain = await nodus.generate.path({ nodes: 20 });// Each call returns a { nodes, edges } graph — load it with setGraph.await nodus.setGraph(scaleFree);Try it live — pick any of the 8 generators and watch it build, lay out and frame:
The generate → setGraph → layout → frame pattern
A generated graph is returned to you, so load it with setGraph. It has
structure but no useful positions, so apply a
layout and then frame it with the
camera:
import { Nodus } from '@kortexya/nodus';
const nodus = new Nodus({ container: document.getElementById('graph') });
// 1. Generate structure — returns a { nodes, edges } graph.const g = await nodus.generate.barabasiAlbert({ nodes: 40, m0: 2, m: 1 });
// 2. Load the returned graph into the instance.await nodus.setGraph(g);
// 3. Lay it out (duration: 0 jumps straight to final positions).await nodus.layouts.force({ duration: 0 });
// 4. Fit the whole graph in the viewport.await nodus.view.locateGraph();Swap the generator and the layout to taste — a tree pairs naturally with a hierarchical layout, a grid with the grid layout, a path with almost anything:
const tree = await nodus.generate.balancedTree({ children: 2, height: 4 });await nodus.setGraph(tree);await nodus.layouts.hierarchical({ duration: 0 });await nodus.view.locateGraph();Picking a generator
- Trees (
balancedTree,randomTree) — good for testing hierarchical and radial layouts, expand/collapse, and breadcrumb-style navigation. - Grid — a predictable lattice; handy for visual regression and for checking that spacing and sizing look right.
- Path — the simplest non-trivial graph; great for animations and for illustrating a single concept.
erdosRenyi/random— uniform random structure; useful for stress tests where you control density via probability or edge count.barabasiAlbert— a scale-free network with hubs; realistic for social or citation-style demos where degree distribution matters.flower— a regular, visually distinctive structure for showcases.
Deterministic output
Because the generators are seeded, two runs with the same options produce identical graphs. That makes them dependable building blocks for tests:
// Generate a known graph, then assert on its size.const p = await nodus.generate.path({ nodes: 20 });await nodus.setGraph(p);console.assert(nodus.getEdges().size === nodus.getNodes().size - 1);If you want a fresh random shape, vary the seed (or the relevant size/probability options) between runs.
After generating
A generated graph is an ordinary Nodus graph — everything else works on it:
const g = await nodus.generate.grid({ rows: 6, columns: 6 });await nodus.setGraph(g);await nodus.layouts.grid({ duration: 0 });await nodus.view.locateGraph();
// Style it like any other graph.nodus.styles.addNodeRule({ color: '#0f766e', radius: 8 });nodus.styles.addEdgeRule({ color: '#cbd5e1', width: 1 });
// And run algorithms on it.const cyclic = nodus.algorithms.hasCycle();For loading real data instead, see Parsing Graph Data and Building a Graph.
Next
- Applying Layouts — arrange whatever you generate.
- Parsing Graph Data — load external data instead of generating it.
- API: Utilities — the generator reference.