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

dijkstra-calculator

v1.0.8

Published

Dijkstra calculator for the shortest path in a graph of nodes given a weight.

Downloads

97

Readme

Dijkstra's Calculator

A Typescript implementation of Dijkstra's shortest path algorithm

  • GitHub URL: https://github.com/getditto/dijkstra-calculator
  • TypeDoc Link: https://getditto.github.io/dijkstra-calculator
  • Built with TypeScript
  • Continuous Integration Status Continuous Integration

Use this to find the shortest path of nodes in graph using Dijkstra's algorithm. Learn how to pronounce Dijkstra here.

This library a TypeScript port from Alfred Gatsby @Prottoy2938 for the great work done here: https://gist.github.com/Prottoy2938/66849e04b0bac459606059f5f9f3aa1a

At Ditto we do a lot of imagery with react-force-graph to show how our mesh network can establish virtual connections between peers. This library is used to aid in showing the shortest path between peers. Note: we use a modified version of Dijkstra's Algorithm with differing priority per link as a consideration for our system. This library is primarily used for assisting in simple visualizations in our documentation and blog.

Installation

Use either npm or yarn to install the library. This library is targeting ES5 and can be run on either Web, Node, or Electron projects. It does not have any dependencies.

npm install dijkstra-calculator
# or if you're using yarn
yarn add dijkstra-calculator

Usage:

Let's say you want to find the shortest path between two nodes in the graph. Given a series of Nodes in a graph with identifiers "A "to "F" and edges established between each one

import { DijkstraCalculator } from 'dijkstra-calculator';

const graph = new DijkstraCalculator();

graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
graph.addVertex('E');
graph.addVertex('F');

graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('B', 'E');
graph.addEdge('C', 'D');
graph.addEdge('C', 'F');
graph.addEdge('D', 'E');
graph.addEdge('D', 'F');
graph.addEdge('E', 'F');

// Now you can calculate the shortest distance between A and E
const path = graph.calculateShortestPath('A', 'E');
// this will print ['A', 'B', 'E']

Adding weights to edges

There are instances where you'd like to add some priority or weight to an edge. The 3rd parameter

import { DijkstraCalculator } from 'dijkstra-calculator';

const graph = new DijkstraCalculator();
graph.addVertex('A');
graph.addVertex('B');
graph.addVertex('C');
graph.addVertex('D');
graph.addVertex('E');
graph.addVertex('F');

graph.addEdge('A', 'B', 4);
graph.addEdge('A', 'C', 2);
graph.addEdge('B', 'E', 3);
graph.addEdge('C', 'D', 2);
graph.addEdge('C', 'F', 4);
graph.addEdge('D', 'E', 3);
graph.addEdge('D', 'F', 1);
graph.addEdge('E', 'F', 1);

const path = graph.calculateShortestPath('A', 'E');
// with consideration of the weights at the edge, the values will be ['A', 'C', 'D', 'F', 'E']

Getting a Linked List instead of a string array

Libraries like d3 or Vis.js or force-graph will want a structure to specify edges that looks something like this:

[
  { source: 'A', target: 'B' },
  { source: 'C', target: 'D' },
  // etc...
];

You can get something that fits these APIs by calling calculateShortestPathAsLinkedListResult like below:

const linkedList = graph.calculateShortestPathAsLinkedListResult('A', 'E')
// This will result in `linkedList` with the following contents
[
  { source: 'A', target: 'C' },
  { source: 'C', target: 'D' },
  { source: 'D', target: 'F' },
  { source: 'F', target: 'E' },
];

Pronunciation Of Dijkstra

Not sure how to pronounce Dijkstra? https://www.youtube.com/watch?v=lg6uIPSvclU