Skip to content

Commit

Permalink
Merge a49a48a into 51a9fff
Browse files Browse the repository at this point in the history
  • Loading branch information
eush77 committed Jul 28, 2014
2 parents 51a9fff + a49a48a commit 85a3178
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion algorithms/graph/bellman_ford.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
var bellmanFord = function (graph, startNode) {
var minDistance = {};
var previousVertex = {};
var edges = [];
var adjacencyListSize = 0;

Expand Down Expand Up @@ -45,6 +46,7 @@ var bellmanFord = function (graph, startNode) {

if (sourceDistance < targetDistance) {
minDistance[edges[j].target] = sourceDistance;
previousVertex[edges[j].target] = edges[j].source;
}
}
}
Expand All @@ -63,7 +65,8 @@ var bellmanFord = function (graph, startNode) {
}

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

Expand Down
2 changes: 2 additions & 0 deletions test/algorithms/graph/bellman_ford.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('Bellman-Ford Algorithm', function () {
assert.equal(shortestPaths.distance.a, 0);
assert.equal(shortestPaths.distance.d, -2);
assert.equal(shortestPaths.distance.e, 1);
assert.equal(shortestPaths.previous.d, 'e');
assert.equal(shortestPaths.previous.e, 'b');

// It'll cause a Negative-Weighted Cycle.
graph.addEdge('c', 'a', -9);
Expand Down

0 comments on commit 85a3178

Please sign in to comment.