Bellman Ford algorithm for node.js
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.
npm install bellman-ford
var graph = -ford;
graphadd_edge"a" "b" -0.1;graphadd_edge"b" "a" 0.2;graphadd_edge"a" "c" 0.9;graphadd_edge"c" "e" -0.1;graphadd_edge"e" "c" 0.2;graphadd_edge"e" "d" 0.2;graphadd_edge"d" "e" 0.1;graphadd_edge"d" "a" 0.4;graphadd_edge"b" "d" 0.3;
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
the distance to the source node
c whcich in this case is
This function adds a node to directed graph.
It takes one parameter
If a node with
node_name already exists then nothing wil happen
This function adds edges to directed graph.
It takes three parameters 'add_edge(node_from, node_to, edge_weight)'
node_to has not yet beed added to the graph then
the function call will fail
This function updates edge weights between nodes It takes three parameters 'update_edge(node_from, node_to, edge_weight)'
This function prints out the current graph
for the given example will output
a:weight: -0.1 to: bweight: 0.9 to: cb:weight: 0.2 to: aweight: 0.3 to: dd:weight: 0.1 to: eweight: 0.4 to: ae:weight: 0.2 to: cweight: 0.2 to: dc:weight: -0.1 to: e
This function removes all nodes with less then two edges as well as removing all associated edges.
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
If the graph contains negative weight cycles then it will return an empty array
To compile the code from source you must have
installed on your machine.