npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphology-operators

v1.6.0

Published

Miscellaneous operators for graphology.

Downloads

67,830

Readme

Graphology Operators

Miscellaneous operators to be used with graphology.

Installation

npm install graphology-operators

Usage

Unary

Binary

Cast

subgraph

Return a graph's subgraph containing the given nodes and their relevant edges.

import {subgraph} from 'graphology-operators';
// Alternatively, to load only the relevant code:
import subgraph from 'graphology-operators/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 array|Set|function: 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 'graphology-operators';
// Alternatively, to load only the relevant code:
import reverse from 'graphology-operators/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 'graphology-operators';
// Alternatively, to load only the relevant code:
import disjointUnion from 'graphology-operators/disjoint-union';

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 'graphology-operators';
// Alternatively, to load only the relevant code:
import union from 'graphology-operators/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 'graphology-operators';
// Alternatively, to load only the relevant code:
import toDirected from 'graphology-operators/to-directed';

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 ?function|object: 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 'graphology-operators';
// Alternatively, to load only the relevant code:
import toMixed from 'graphology-operators/to-mixed';

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 'graphology-operators';
// Alternatively, to load only the relevant code:
import toMulti from 'graphology-operators/to-multi';

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 'graphology-operators';
// Alternatively, to load only the relevant code:
import toSimple from 'graphology-operators/to-simple';

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 ?function|object: 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 'graphology-operators';
// Alternatively, to load only the relevant code:
import toUndirected from 'graphology-operators/to-undirected';

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 ?function|object: either a merging function or an options object:
    • mergeEdge ?function: merging function to use.