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

bellman-ford

v0.0.6

Published

Bellman Ford algorithm for node.js

Downloads

32

Readme

bellman-ford

bellman-ford allows you to run the bellman ford algorithm in node.js.

It is written in C++ and ported to node.js. It uses a directed graph as it's underlying data structure.

Install

npm install bellman-ford

Examples

Create Graph

var graph = new bellman-ford();

Add Nodes

graph.add_node("a");
graph.add_node("b");
graph.add_node("c");
graph.add_node("d");
graph.add_node("e");

Add Edges

graph.add_edge("a", "b", -0.1);
graph.add_edge("b", "a", 0.2);
graph.add_edge("a", "c", 0.9);
graph.add_edge("c", "e", -0.1);
graph.add_edge("e", "c", 0.2);
graph.add_edge("e", "d", 0.2);
graph.add_edge("d", "e", 0.1);
graph.add_edge("d", "a", 0.4);
graph.add_edge("b", "d", 0.3);

Run Bellman-Ford

console.log(graph.bellmanford("c"));

which will output

[ [ 'a', '0.5' ],
  [ 'b', '0.4' ],
  [ 'd', '0.1' ],
  [ 'e', '-0.1' ],
  [ 'c', '0' ] ]

where each subarray contains a value in the graph say a and the distance to the source node c whcich in this case is 0.5

Functions

add_node

This function adds a node to directed graph. It takes one parameter add_node(node_name) If a node with node_name already exists then nothing wil happen

add_edge

This function adds edges to directed graph. It takes three parameters 'add_edge(node_from, node_to, edge_weight)' If node_from or node_to has not yet beed added to the graph then the function call will fail

update_edge

This function updates edge weights between nodes It takes three parameters 'update_edge(node_from, node_to, edge_weight)'

print

This function prints out the current graph

graph.print();

for the given example will output

a:
	weight: -0.1 to: b
	weight: 0.9 to: c
b:
	weight: 0.2 to: a
	weight: 0.3 to: d
d:
	weight: 0.1 to: e
	weight: 0.4 to: a
e:
	weight: 0.2 to: c
	weight: 0.2 to: d
c:
	weight: -0.1 to: e

trim

This function removes all nodes with less then two edges as well as removing all associated edges.

bellmanford

This is the main function which will run the bellman ford algorithm on the current graph. It will return a 2d array of the form [node_name, distance_from_source]

If the graph contains negative weight cycles then it will return an empty array

Compiling from Source

To compile the code from source you must have python gcc and node-gyp installed on your machine.

Then run

node-gyp configure

and

node-gyp build