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

graph-dijkstra

v1.2.0

Published

A simple undirected graph that allows for finding the shortest path between nodes via Dijkstra's algorithm

Downloads

36

Readme

graph-dijkstra

A simple undirected graph that allows for finding the shortest path between nodes via Dijkstra's algorithm. This portable package can easily be wrapper into an angular service (as seen in our demo).

Developed for use in our Office Employee Locator application.

Version: 1.2.0

Installation & Typical Usage

  1. Install via npm
    • $ npm install --save-dev https://github.com/LincolnTechOpenSource/graph-dijkstra
  2. Include the JavaScript file
    • <link rel="stylesheet" href="node_modules/graph-dijkstra/dist/graph-dijkstra.js">
    • or <link rel="stylesheet" href="node_modules/graph-dijkstra/dist/graph-dijkstra.min.js">
  3. Use the available API - for example:
    var graph = new Graph();
    graph.addNode(1, {weight: 1, nType: 1});
    // graph.nodes     => { '1': GraphNode { id: 1, weight: 1, nType: 1, neighbors: [] } }
    // graph.nodeCount => 1
    
    graph.addNode(2, {weight: 1, nType: 1});
    // graph.nodes     => { '1': GraphNode { id: 1, ... }, '2': GraphNode { id: 2, ... } }
    // graph.nodeCount =>  2
    
    graph.addNode(3, {weight: 4, nType: 1});
    // graph.nodes     => { '1': GraphNode { id: 1, ... }, '2': GraphNode { id: 2, ... }, '3': GraphNode { id: 3, weight: 4, ... } }
    // graph.nodeCount => 3
    
    graph.addEdge(1, 2);
    // graph.nodes     => { '1': GraphNode { id: 1, ..., neighbors: [ 2 ] }, '2': GraphNode { id: 2, ..., neighbors: [ 1 ] }, '3': GraphNode { id: 3, ...} }
    // graph.edgeCount => 1
    
    graph.addEdge(2, 3);
    // graph.nodes     => { '1': GraphNode { id: 1, ... }, '2': GraphNode { id: 2, ..., neighbors: [ 1, 3 ] }, '3': GraphNode { id: 3, ..., neighbors: [ 2 ] } }
    // graph.edgeCount => 2
    
    graph.addEdge(1, 3);
    // graph.nodes     => { '1': GraphNode { id: 1, ..., neighbors: [ 2, 3 ] }, '2': GraphNode { id: 2, ... }, '3': GraphNode { id: 3, ..., neighbors: [ 2, 1 ] } }
    // graph.edgeCount => 3
    
    var results   = Dijkstra.run(graph, 1, 3);
    var path      = Dijkstra.getPath(results.prev, 3);
    var printDist = 'node 1 is ' + results.dist[3] + ' unit from node 3'
    
    // results   => { source: 1, target: 3, dist: { '1': 0, '2': 1, '3': 1 }, prev: { '1': 1, '2': 1, '3': 1 } }
    // path      => [ 1, 3 ]
    // printDist => node 1 is 1 unit from node 3

Demos

  • See the package in an angular application with our simple demo.

    Here we create a simple angular service to wrap the Graph and Dijkstra libraries. Using this service we make a 36 node graph to serve as a grid and demonstrate how to go about finding and acting on the shortest path.

  • For an example of this package at work in a larger project, see our Office Employee Locator application.

    In this project we again create a service to wrap Graph and Dijkstra. Graph serves as the underlying graph of location objects (nodes) on which we run Dijkstra to find the shortest paths between them and generate directions.

Public API

See our project page for the full documentation (generated by documentationjs)

Graph

(prototype)
  • Graph(graph)
  • nodes
  • nodeCount
  • edgeCount
  • find(id)
  • exists(id)
  • addNode(id, props)
  • deleteNode(id)
  • eachNode(fn)
  • eachNeighbor(id, fn)
  • addEdge(source, target)
  • addOrCreateEdge(source, target)
  • deleteEdge(source, target)
  • connected(source, target)
  • update(id, props)

GraphNode

(prototype)
  • id
  • weight
  • nType
  • neighbors

Dijkstra

(static)
  • run(graph, pathType, source, target)
  • getPath(prevList, target)

How to Contribute

Please see our Contributing Guide

This project was created for use in the Office Employee Locator application. We encourage and appreciate any contributions that aim to enhance the robustness of this module.

Credits

Authors: Matthew Vasseur and David Tahvildaran

Adapted Resources:

Library Resources:

License

See the LICENSE file for license rights and limitations (MIT).