-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbfs.js
More file actions
30 lines (24 loc) · 701 Bytes
/
bfs.js
File metadata and controls
30 lines (24 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module.exports = (function () {
'use strict';
function bfs(start, nodes, fn) {
var frontier = [start];
var level = 0, levels = {};
while (0 < frontier.length >>> 0) {
var next = [];
for (var i in frontier) {
var node = frontier[i];
levels[node] = level;
fn(node);
for (var i in nodes[node]) {
var adj = nodes[node][i];
if (void(0) === levels[adj]) {
next.push(adj);
}
}
}
frontier = next;
level += 1;
}
}
return bfs;
})();