Skip to content

Commit

Permalink
Merge ba02bd0 into c660898
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed May 31, 2014
2 parents c660898 + ba02bd0 commit 6fb0993
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
76 changes: 76 additions & 0 deletions algorithms/graph/SPFA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (C) 2014 Felipe Ribeiro
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
'use strict';

/**
* Calculates the shortest paths in a graph to every node from the node s
* with SPFA(Shortest Path Faster Algorithm) algorithm
*
* @param {Object} graph An adjacency list representing the graph
* @param {string} start the starting node
*
*/
function SPFA(graph, s) {
var distance = {};
var previous = {};
var queue = {};
var isInQue = {};
var head = 0;
var tail = 1;
// initialize
distance[s] = 0;
queue[0] = s;
isInQue[s] = true;
graph.vertices.forEach(function (v) {
if (v !== s) {
distance[v] = Infinity;
isInQue[v] = false;
}
});

var currNode;
while (head != tail) {
currNode = queue[head++];
isInQue[currNode] = false;
var neighbors = graph.neighbors(currNode);
for (var i = 0; i < neighbors.length; i++) {
var v = neighbors[i];
// relaxation
var newDistance = distance[currNode] + graph.edge(currNode, v);
if (newDistance < distance[v]) {
distance[v] = newDistance;
previous[v] = currNode;
if (!isInQue[v]){
queue[tail++] = v;
isInQue[v] = true;
}
}
}
}

return {
distance: distance,
previous: previous
};
}

module.exports = SPFA;
49 changes: 49 additions & 0 deletions test/algorithms/graph/SPFA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2014 Felipe Ribeiro
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"], to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
'use strict';

var SPFA = require('../../../algorithms/graph/SPFA'),
Graph = require('../../../data_structures/graph'),
assert = require('assert');

describe('SPFA Algorithm', function () {
it('should return the shortest paths to all nodes from a given origin',
function () {
var g = new Graph();
g.addEdge('a', 'b', 5);
g.addEdge('a', 'c', 10);
g.addEdge('b', 'c', 2);
g.addEdge('b', 'd', 20);
g.addEdge('c', 'd', 1);
g.addEdge('d', 'a', 10);

var shortestPath = SPFA(g, 'a');
assert.equal(shortestPath.distance.b, 5);
assert.equal(shortestPath.previous.b, 'a');

assert.equal(shortestPath.distance.c, 7);
assert.equal(shortestPath.previous.c, 'b');

assert.equal(shortestPath.distance.d, 8);
assert.equal(shortestPath.previous.d, 'c');
});
});

0 comments on commit 6fb0993

Please sign in to comment.