bfs.js
const graph = {
A: ["B", "C"],
B: ["D", "E"],
C: ["F"],
D: [],
E: ["F"],
F: [],
};
function bfs(start) {
const queue = [start];
const seen = {};
const order = [];
while (queue.length > 0) {
const node = queue.shift();
if (seen[node]) continue;
seen[node] = true;
order.push(node);
for (const nb of graph[node]) {
if (!seen[nb]) queue.push(nb);
}
}
return order;
}
bfs("A");
What to watch
The graph draws as a node-link diagram (current node glows); the queue fills from the back and drains from the front.
Reading bfs (graph + queue)on a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how bfs (graph + queue) works.
Run bfs (graph + queue) yourself
Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.
Open NeonFlow