-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from eush77/bfs-shortest-path
Add shortest path algorithm based on Breadth-First Search
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
var breadthFirstSearch = require('./breadth_first_search'); | ||
|
||
|
||
/** | ||
* Shortest-path algorithm based on Breadth-First Search. | ||
* Works solely on graphs with equal edge weights (but works fast). | ||
* Complexity: O(V + E). | ||
* | ||
* @param {Graph} graph | ||
* @param {string} source | ||
* @return {{distance: Object.<string, number>, | ||
* previous: Object.<string, string>}} | ||
*/ | ||
var bfsShortestPath = function (graph, source) { | ||
var distance = {}, previous = {}; | ||
distance[source] = 0; | ||
|
||
breadthFirstSearch(graph, source, { | ||
onTraversal: function (vertex, neighbor) { | ||
distance[neighbor] = distance[vertex] + 1; | ||
previous[neighbor] = vertex; | ||
} | ||
}); | ||
|
||
return { | ||
distance: distance, | ||
previous: previous | ||
}; | ||
}; | ||
|
||
|
||
module.exports = bfsShortestPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
var bfsShortestPath = require('../../../algorithms/graph/bfs_shortest_path'), | ||
Graph = require('../../../data_structures/graph'), | ||
assert = require('assert'); | ||
|
||
|
||
describe('BFS Shortest Path Algorithm', function () { | ||
it('should return the shortest paths to all nodes from a given origin', | ||
function () { | ||
var graph = new Graph(); | ||
graph.addEdge(0, 1); | ||
graph.addEdge(1, 2); | ||
graph.addEdge(0, 2); | ||
graph.addEdge(2, 3); | ||
graph.addEdge(3, 6); | ||
graph.addEdge(6, 2); | ||
graph.addEdge(6, 0); | ||
graph.addEdge(0, 4); | ||
graph.addEdge(4, 6); | ||
graph.addEdge(4, 5); | ||
graph.addEdge(5, 0); | ||
graph.addEdge('a', 'b'); | ||
|
||
var shortestPath = bfsShortestPath(graph, 0); | ||
|
||
assert.deepEqual(shortestPath.distance, { | ||
0: 0, | ||
1: 1, | ||
2: 1, | ||
3: 2, | ||
4: 1, | ||
5: 2, | ||
6: 2 | ||
}); | ||
assert.deepEqual(shortestPath.previous, { | ||
1: 0, | ||
2: 0, | ||
3: 2, | ||
4: 0, | ||
5: 4, | ||
6: 4 | ||
}); | ||
}); | ||
}); |