GithubHelp home page GithubHelp logo

getditto / dijkstra-calculator Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 2.0 2.25 MB

A simple JS calculator for finding the shortest path in a graph of nodes.

Home Page: https://getditto.github.io/dijkstra-calculator/

License: MIT License

TypeScript 100.00%
dijkstra-algorithm javascript nodejs

dijkstra-calculator's Introduction

Dijkstra's Calculator

A Typescript implementation of Dijkstra's shortest path algorithm

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

dijkstra-calculator's People

Contributors

mbalex99 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

aeolun lpdetilly

dijkstra-calculator's Issues

Algorithm assumes undirected graphs

  • I'm submitting a ...
    [ ] bug report
    [x] feature request
    [x] question about the decisions made in the repository
    [ ] question about how to use this project

  • Summary
    It seems that the current implementation assumes graph is undirected, so calling addEdge("A", "B") is symetric to addEdge("B", "A") (and actually does both).
    This means that it's not possible to calculate Dijkstra for directed graphs.

  • Other information (e.g. detailed explanation, stack traces, related issues, suggestions how to fix, links for us to have context, eg. StackOverflow, personal fork, etc.)

I compared this behavior with the dijkstrajs module, which does support directed graphs.
Here's the comparison code:

const { DijkstraCalculator } = require("dijkstra-calculator");
const { find_path } = require ("dijkstrajs");

// Directed graph
// Vertex: 
// A, B, C, D
// Edges:
// B -> A
// C -> B
// D -> B
// D -> C

// Test #1
// D => A expected D -> B -> A

// Test #2
// A => D expected to throw / return empty


// dijkstra-calculator

const graph = new DijkstraCalculator();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addEdge("B", "A");
graph.addEdge("C", "B");
graph.addEdge("D", "B");
graph.addEdge("D", "C");

console.log(`dijkstra-calculator: D->A: ${graph.calculateShortestPath("D", "A")}`); // should be ["D", "B", "A"]
console.log(`dijkstra-calculator: A->D: ${graph.calculateShortestPath("A", "D")}`); // should be [] or thorw


// dijkstra-js

const graph2 = {
  "A": {},
  "B": {
    "A": 1
  },
  "C": {
    "B": 1
  },
  "D": {
    "B": 1,
    "C": 1
  }
}

console.log(`dijkstra-js: D->A: ${find_path(graph2, "D", "A")}`); // should be ["D", "B", "A"]
console.log(`dijkstra-js: A->D: ${find_path(graph2, "A", "D")}`); // should throw

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.