Graphology Operators
Miscellaneous operators to be used with graphology
.
Installation
npm install graphologyoperators
Usage
Unary
Binary
Cast
subgraph
Return a graph's subgraph containing the given nodes and their relevant edges.
import {subgraph} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import subgraph from 'graphologyoperators/subgraph';
// From an array of nodes
const sub = subgraph(graph, ['John', 'Mary', 'Sue']);
// From a set of nodes
const sub = subgraph(graph, new Set(['John', 'Mary', 'Sue']));
// From a filtering function
const sub = subgraph(graph, function (key, attr) {
return key.startsWith('J')  attr.color === 'red';
});
Arguments
 graph Graph: target graph.
 nodes arraySetfunction: either an array of nodes to keep, or a set of nodes to keep or a function taking a node's key and its attributes and tasked to filter the nodes to keep.
reverse
Reverse the given graph's directed edges.
import {reverse} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import reverse from 'graphologyoperators/reverse';
const reversedGraph = reverse(graph);
Arguments
 graph Graph: target graph.
disjointUnion
Returns the disjoin union of the given graphs. To do so, the function will relabel your nodes & edges so both graphs can remain disjoint.
import {disjointUnion} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import disjointUnion from 'graphologyoperators/disjointunion';
const R = disjointUnion(G, H);
Arguments
 G Graph: first graph.
 H Graph: second graph.
union
Returns the union of the given graphs. Nodes & edges present in both graph will have their attributes merges with precedence given to the second graph.
import {union} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import union from 'graphologyoperators/union';
const R = union(G, H);
Arguments
 G Graph: first graph.
 H Graph: second graph.
toDirected
Returns the directed version of the given graph where any undirected edge will be now considered as mutual.
Note that you can pass a function to merge edge attributes in case of mixed edges conflicts. This can be useful to sum weights and so on...
If an already directed graph is passed, the function will only return a copy of it.
If passing a multi graph, undirected edges will only be converted as pairs ofmutual ones and will never be merged.
import {toDirected} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import toDirected from 'graphologyoperators/todirected';
const directedGraph = toDirected(graph);
// Using a merging function
const directedGraph = toDirected(graph, (currentAttr, nextAttr) => {
return {
...currentAttr,
weight: currentAttr.weight + nextAttr.weight
};
});
Arguments
 graph Graph: target graph.

mergeOrOptions ?functionobject: either a merging function or an options object:
 mergeEdge ?function: merging function to use.
toMixed
Returns the given graph as mixed.
If an already mixed graph is passed, the function will only return a copy of it.
import {toMixed} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import toMixed from 'graphologyoperators/tomixed';
const mixedGraph = toMixed(graph);
toMulti
Returns the given graph as multi.
If an already multi graph is passed, the function will only return a copy of it.
import {toMulti} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import toMulti from 'graphologyoperators/tomulti';
const mixedGraph = toMulti(graph);
toSimple
Returns the simple version of the given multigraph where we only keep a single edge of each type between nodes.
Note that you can pass a function to merge edge attributes in case of mutual edges or mixed edges conflicts. This can be useful to sum weights and so on...
If an already simple graph is passed, the function will only return a copy of it.
import {toSimple} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import toSimple from 'graphologyoperators/tosimple';
const simpleGraph = toSimple(multiGraph);
// Using a merging function
const simpleGraph = toSimple(graph, (currentAttr, nextAttr) => {
return {
...currentAttr,
weight: currentAttr.weight + nextAttr.weight
};
});
Arguments
 graph Graph: target graph.

mergeOrOptions ?functionobject: either a merging function or an options object:
 mergeEdge ?function: merging function to use.
toUndirected
Returns the undirected version of the given graph where any directed edge will be now considered as undirected.
Note that you can pass a function to merge edge attributes in case of mutual edges or mixed edges conflicts. This can be useful to sum weights and so on...
If an already undirected graph is passed, the function will only return a copy of it.
If passing a multi graph, directed edges will only be converted as undirected ones and will never be merged.
import {toUndirected} from 'graphologyoperators';
// Alternatively, to load only the relevant code:
import toUndirected from 'graphologyoperators/toundirected';
const undirectedGraph = toUndirected(graph);
// Using a merging function
const undirectedGraph = toUndirected(graph, (currentAttr, nextAttr) => {
return {
...currentAttr,
weight: currentAttr.weight + nextAttr.weight
};
});
Arguments
 graph Graph: target graph.

mergeOrOptions ?functionobject: either a merging function or an options object:
 mergeEdge ?function: merging function to use.