Instantiating a graphology Graph object is merely an issue of requiring an implementation and calling it with some options.

Note that if you need to create a Graph using some serialized data or another Graph's data, you should probably check the static #.from.

import Graph from 'graphology';

// Here you go:
const graph = new Graph();

// With options:
const graph = new Graph(options);


  • allowSelfLoops [boolean] true: should the graph allow self-loops?
  • defaultEdgeAttributes [object]: default edge attributes merged with the provided ones.
  • defaultNodeAttributes [object]: default node attributes merged with the provided ones.
  • edgeKeyGenerator [function]: Function used internally by the graph to produce keys for key-less edges. By default, the graph will produce keys as UUID v4. For more information concerning the function you can provide, see this.
  • indices [object]: Options regarding index computation. For more information, see this.
  • multi [boolean] false: Should the graph allow parallel edges?
  • type [string] "mixed": Type of the graph. One of directed, undirected or mixed.


// Creating a multi-graph with no self-loops
const graph = new Graph({multi: true, allowSelfLoops: false});

// Creating a graph with default node attributes
const graph = new Graph({defaultNodeAttributes: {size: 0}});

Typed constructors

Rather than providing tedious options to the constructor, one can use one of the many handy constructors provided by the implementation to create the desired graph:

import {MultiDirectedGraph} from 'graphology';

const myCustomGraph = new MultiDirectedGraph();

By default, the Graph object is a simple mixed graph, but here are the different naming "components" that you can use to instantiate a more complex graph:

  • Type of the graph?: Directed, Undirected or none (mixed graph).
  • Graph with parallel edges?: Multi or none (simple graph).

Then to build the name, one must order the components likewise:

Multi? + Type? + Graph



Static #.from method

Alternatively, one can create a graph from a serialized graph or another Graph instance using the static #.from method:


const graph = Graph.from(data);

// Need some options?
const graph = Graph.from(data, options);

// Also works with typed constructors
const graph = UndirectedGraph.from(data);


  • data Graph|SerializedGraph: pre-existing data to give to the constructor. This data can either be an existing Graph instance, and in this case both nodes & edges will be imported from the given graph, or a serialized graph whose format is described here.
  • options [object]: options passed to the created graph.

Edge key generator function

The provided function takes a single object describing the created edge & having the following properties:

  • undirected boolean: whether the edge is undirected.
  • source any: the source of the edge.
  • target any: the target of the edge.
  • attributes object: optional attributes.

Example - Incremental id

const generator = (function() {
  let id = 0;

  return () => id++;

const graph = new Graph({edgeKeyGenerator: generator});

graph.addNodesFrom(['John', 'Martha']);
graph.addEdge('John', 'Martha');

graph.edges('John', 'Martha');
>>> ['0']

Example - Id based on edge data

const generator = function({undirected, source, target, attributes}) {
  return `${source}->${target}`;

const graph = new Graph({edgeKeyGenerator: generator});

graph.addNodesFrom(['John', 'Martha']);
graph.addEdge('John', 'Martha');

graph.edges('John', 'Martha');
>>> ['John->Martha']