graphology-utils
TypeScript icon, indicating that this package has built-in type declarations

2.5.2 • Public • Published

Graphology Utils

Miscellaneous utility functions to be used with graphology.

Installation

npm install graphology-utils

Usage

Assertions

Introspection

Typical edge patterns

Miscellaneous helpers

#.isGraph

Function returning whether the given value is a graphology implementation's instance.

import {isGraph} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import isGraph from 'graphology-utils/is-graph';

const graph = new Graph();

isGraph(graph);
>>> true

isGraph(45);
>>> false

isGraph({hello: 'world'});
>>> false

Arguments

  • value any: value to test.

#.isGraphConstructor

Function returning whether the given value is a graphology constructor.

import {isGraphConstructor} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import isGraphConstructor from 'graphology-utils/is-graph-constructor';

isGraphConstructor(Graph);
>>> true

isGraphConstructor(45);
>>> false

isGraphConstructor(new Graph());
>>> false

Arguments

  • value any: value to test.

#.inferMulti

Function returning whether the given graph is truly multi, i.e. if we can find at least one occurrence of multiple edges of the same type and direction between two nodes.

import {inferMulti} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import inferMulti from 'graphology-utils/infer-multi';

const graph = new MultiGraph();
graph.addEdge(1, 2);

inferMulti(graph);
>>> false

graph.addEdge(1, 2);

inferMulti(graph);
>>> true

#.inferType

Function returning the inferred type of the given graph. This function is useful to check whether a given mixed graph is in fact a mere directed or undirected graph based on its actual edges.

import {inferType} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import inferType from 'graphology-utils/infer-type';

const graph = new Graph();
graph.addUndirectedEdge(1, 2);

inferType(graph);
>>> 'directed'

#.mergeClique

Function adding a clique to the given graph.

import {mergeClique} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergeClique from 'graphology-utils/merge-clique';

const graph = new Graph();

mergeClique(graph, [1, 2, 3]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [1, 3], [2, 3]]

#.mergeCycle

Function adding a cycle to the given graph.

import {mergeCycle} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergeCycle from 'graphology-utils/merge-cycle';

const graph = new Graph();

mergeCycle(graph, [1, 2, 3, 4, 5]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [2, 3], [3, 4], [4, 5], [5, 1]]

Arguments

  • graph Graph: target graph.
  • cycle array: array of nodes representing the cycle to add.

#.mergePath

Function adding a path to the given graph.

import {mergePath} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergePath from 'graphology-utils/merge-path';

const graph = new Graph();

mergePath(graph, [1, 2, 3, 4, 5]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [2, 3], [3, 4], [4, 5]]

Arguments

  • graph Graph: target graph.
  • path array: array of nodes representing the path to add.

#.mergeStar

Function adding a star to the given graph.

import {mergeStar} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import mergeStar from 'graphology-utils/merge-star';

const graph = new Graph();

mergeStar(graph, [1, 2, 3, 4, 5]);
graph.edges().map(e => graph.extremities(e));
>>> [[1, 2], [1, 3], [1, 4], [1, 5]]

Arguments

  • graph Graph: target graph.
  • star array: array of nodes representing the star to add.

#.renameGraphKeys

Function renaming the nodes & edges key of a graph using mappings and returning a new graph with renamed keys.

import {renameGraphKeys} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import renameGraphKeys from 'graphology-utils/rename-graph-keys';

const graph = new Graph();
graph.addNode('Martha');
graph.addNode('Catherine');
graph.addNode('John');
graph.addEdgeWithKey('M->C', 'Martha', 'Catherine');
graph.addEdgeWithKey('C->J', 'Catherine', 'John');

const renamedGraph = renameGraphKeys(
  graph,
  {Martha: 1, Catherine: 2, John: 3},
  {'M->C': 'rel1', 'C->J': 'rel2'}
);

renamedGraph.nodes();
>>> [1, 2, 3];

renamedGraph.edges();
>>> ['rel1', 'rel2'];

Arguments

  • graph Graph: target graph.
  • nodeKeyMapping object: A key/value map for the node key mapping.
  • edgeKeyMapping ?object: A key/value map for the edge key mapping.

#.updateGraphKeys

Function updating the nodes & edges key of a graph using functions and returning a new graph with updated keys.

import {updateGraphKeys} from 'graphology-utils';
// Alternatively, if you want to only load the relevant code:
import updateGraphKeys from 'graphology-utils/update-graph-keys';

const graph = new Graph();
graph.addNode('Martha');
graph.addNode('Catherine');
graph.addNode('John');
graph.addEdgeWithKey('M->C', 'Martha', 'Catherine');
graph.addEdgeWithKey('C->J', 'Catherine', 'John');

const updatedGraph = updateGraphKeys(
  graph,
  (key)=> {
    if (key === 'Martha') return 1;
    if (key === 'Catherine') return 2;
    return 3;
  },
  (key) => {
    if (key === 'M->C') return 'rel1';
    return 'rel2';
  }
);

updatedGraph.nodes();
>>> [1, 2, 3];

updatedGraph.edges();
>>> ['rel1', 'rel2'];

Arguments

  • graph Graph: target graph.
  • nodeKeyUdater function: A function to compute a new node key from the same arguments that would be given to #.forEachNode.
  • edgeKeyUpdater function: A function to compute a new edge key from the same arguments that would be given to #.forEachEdge.

Readme

Keywords

Package Sidebar

Install

npm i graphology-utils

Weekly Downloads

238,268

Version

2.5.2

License

MIT

Unpacked Size

30.9 kB

Total Files

33

Last publish

Collaborators

  • yomguithereal