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

undirected

v0.1.0

Published

JavaScript implementation of undirected graphs through adjacency lists

Downloads

13

Readme

UndirectedJS

JavaScript implementation of undirected graphs through adjacency lists

This package is mainly a refined and robust implementation with types of:

https://www.geeksforgeeks.org/implementation-graph-javascript/

It is meant to provide an easy and accessible ("out-of-the-box") work flow and to be minimal.

While there is an array of existing solutions for the given task, they all seem to "fail" for specific reasons - considering the targeted usage paradigm:

  • graphology as a whole specification is over scoped.
  • graphlib as a complete solution for either directed and undirected graphs with tons of admittedly elaborate features is far away from being minimal.
  • Then there are a few packages who look not taken care of a lot and/or abandoned: graphlibrary (a fork from graphlib), undirected-graph, graph.

Usage

import Undirected from 'undirected';

const graph = new Undirected();

// Or just
// const graph = new Undirected(people, friendship);
// whilst defining `people` and `friendship` upfront

const people = ['Alice', 'Bob', 'John', 'Jane'];

const friendship = [
  ['Alice', 'Bob'],
  ['Bob', 'John'],
  ['John', 'Alice'],
  ['Jane', 'John'],
  ['Jane', 'Alice'],
];

// Add bundled data
graph.addNodes(people);
graph.addEdges(friendship);

// Add single data points with the same methods
graph.addNodes('Solomon');
graph.addEdges(['Solomon', 'Bob']);

// Perform lookups
console.log(graph.bfs('Alice', 'Jane'));
console.log(graph.dfs('Alice', 'Solomon'));

// Remove nodes or connections
graph.removeNodes('John');
graph.removeEdges(['Jane', 'Alice']);

console.log(graph);

API

Constructor: new Undirected(nodes?: Nodes, edges?: Edges)

Creates a new instance of an undirected graph from the given nodes and edges. Both can be given as implicit singletons (a single string for a node or an array of two strings for an edge) or an array of strings for the nodes or a list of arrays for the edges. Specifically, the edges are passed as a list of connections beetwen any two edges, i.e. ['nodeA', 'nodeB'] (or [['node1', 'node2'], ...]).

Properties

.graph

Returns the (current) Map representation of the undirected graph, i.e. the adjacency list.

Methods

addNodes

addNodes(keys: Nodes)

Add a node or a set of nodes to the graph.

addEdges

addEdges(pairs: Edges)

Add an edge or a set of edges to the graph.

bfs

bfs(search: string, start: string, callback?: LookupCallback): LookupResult

Performs a breadth-first search. If the lookup is successful, the callback is invoked with the search and path length (from start to search) and an array containing the path length and the path is returned. Otherwise the method returns undefined.

dfs

dfs(search: string, start: string, callback?: LookupCallback): LookupResult

Performs a depth-first search. If the lookup is successful, the callback is invoked with the search and path length (from start to search) and an array containing the path length and the path is returned. Otherwise the method returns undefined.

Keywords

  • undirected
  • graphs
  • graph
  • theory
  • network
  • path
  • algorithm
  • data
  • structures

Dependencies

Related

  • graphology - a specification for a robust & multipurpose JavaScript Graph object
  • graphlib - Graphlib is a JavaScript library that provides data structures for undirected and directed multi-graphs
  • graphlibrary - based on dagrejs/graphlib (see above)
  • undirected-graph - Graph data structure with basic functionality
  • graph - a simple Javascript library for manipulating directed and undirected graphs

Maintainer