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

graphs-adt

v0.1.9

Published

Graph data structure with path finding and traversing algorithms

Downloads

33

Readme

Graphs - Abstract Data Type

This library contains a collection of Graph abstract data types, each with a set of algorithms.

Graphs

A graph is a structure amounting to a set of objects in which some pairs of the objects are in some sense "related" (Wikipedia, 2019).

Graph

A directed or undirected graph data structure.

Options

  • directed - default: false
const g = new Graph({
  directed: true
})

Methods

  • g.addNode(key: string)
    • Creates and adds a new node into the graph
  • g.getNode(key: string)
    • Returns the requested node
  • g.addEdge(key1: string, key2: string, weight: number)
    • Creates a new edge between key1 and key2 with the desired weighting. In a directed graph the edge will be directed from key1 to key2
  • g.getEdge(key1: string, key2: string)
    • Returns the weighting of the requested edge. If the graph is directed it will return the weighting from key1 to key2. If the graph is undirected the ordering of the node keys doesn't matter.
  • g.getPath(key1: string, key2: string)
    • Returns an array of nodes ordered by the shortest path based on the weight of the edges
  • g.dijkstra(key: string)
    • Returns an object keyed by all nodes possible to connect to the node represented by key. Each node key maps to data representing the distance from key and the last traveled
  • g.bfs(key: string, fn: Function)
    • Breadth first traversal where key is the starting node and fn is the callback function to run on each visited node
  • g.dfs(key: string, fn: Function)
    • Depth first traversal where key is the starting node and fn is the callback function to run on each visited node

Underdevelopment:

  • Graph
    • A*
  • Tree
    • Binary
    • Red-black

Implementing a Graph

Install the package

$ npm install -S graphs-adt

Create a graph and add some nodes and edges. In this example we will add locations in England and the distance between them to represent the weight.

import { Graph } from 'graphs-adt';

// create a new instance of an undirected graph
const g = new Graph();

// adding nodes (vertices)
g.addNode('London');
g.addNode('Bristol');
g.addNode('Bath');
g.addNode('Bournemouth');

// adding edges (links)
g.addEdge('London', 'Bath', 114);
g.addEdge('Bath', 'Bristol', 23);
g.addEdge('Bristol', 'Bournemouth', 73);
g.addEdge('Bournemouth', 'London', 101);
g.addEdge('Bournemouth', 'Bath', 62);

Now we have a simple graph that represents the distance between each City. A visualisation of this graph may look like the following.

Note the length of the edges do NOT represent the weighting, but the numbers do. Further, there are no arrow heads on the diagram as the graph is undirected so the edges are bidirectional.

Shortest Path

If we now want to shortest distance from London to Bristol we can use the getPath method. Currently, by default this uses Dijkstra's algorithm.

g.getPath('London', 'Bristol'); 

// [ 'London', 'Bath', 'Bristol' ]

It is also possible just to run the Dijkstra algorithm on a node and get the distances to each node it can reach.

g.dijkstra('London')

// { London: { distance: 0, previous: null },
//   Bristol: { distance: 137, previous: 'Bath' },
//   Bath: { distance: 114, previous: 'London' },
//   Bournemouth: { distance: 101, previous: 'London' } }

Traversing / Searching

All graphs support breadth first and depth first searching methods. Just call the method with a key of the node you wish to start at and a callback function.

The following will log out each node to the console starting from the London Node.

// depth first search
g.dfs('London', node => {
  console.log('Node:', node)
});

// breadth first search
g.bfs('London', node => {
  console.log('Node:', node)
});