Breadth-first search and shortest hop counts
Quick answer
Breadth-first search (BFS) visits vertices in increasing distance from the start in an unweighted graph. A FIFO queue stores discovered vertices whose neighbors still need processing.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function bfs(adj,start){ const dist=Array(adj.length).fill(-1),q=[start];dist[start]=0; for(let head=0;head<q.length;head++){const u=q[head]; for(const v of adj[u])if(dist[v]===-1){dist[v]=dist[u]+1;q.push(v);} } return dist;}console.log(JSON.stringify(bfs([[1,2],[3],[3],[0],[]],0)));Expected console output:
[0,1,1,2,-1]Vertex 3 is two edges away; the cycle does not requeue start; 4 is unreachable.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const adj=[[1,2],[3],[3],[]],q=[0],seen=new Set([0]);for(let h=0;h<q.length;h++)for(const v of adj[q[h]])if(!seen.has(v)){seen.add(v);q.push(v);}console.log(JSON.stringify(q));Expected console output:
[0,1,2,3]Discovery before enqueue avoids duplicate 3.
How it works and cost model
For an indexed adjacency list with valid start, initialize distances to -1, set the start to 0 and enqueue it. Scan the queue with a growing head index. When a neighbor still has -1, assign distance[current]+1 before enqueueing. Marking on discovery avoids duplicate enqueues and handles cycles. Distance -1 means unreachable, not zero. A parent array can reconstruct one shortest path by following predecessors backward. This minimizes edge count, not arbitrary weighted cost. With arrays and constant-time modeled index operations, time O(V+E), including initialization, and O(V) auxiliary cells, excluding graph input.
Common mistakes
Do not mark vertices only after removing them from the queue if you want each enqueued once. BFS hop distance does not solve general weighted shortest paths.
Practice
For [[1,2],[3],[3],[0],[]], find distances from 0. Draw the predecessor tree and explain vertex 4.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Breadth-first search and shortest hop counts — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
At what moment should an undiscovered neighbor be marked in this BFS design?
Which distance does BFS guarantee shortest in an unweighted graph?
What does distance -1 mean in the distance-array implementation?
What is the exact console output in the Breadth-first search distances example 1?
function bfs(adj,start){ const dist=Array(adj.length).fill(-1),q=[start];dist[start]=0; for(let head=0;head<q.length;head++){const u=q[head]; for(const v of adj[u])if(dist[v]===-1){dist[v]=dist[u]+1;q.push(v);} } return dist;}console.log(JSON.stringify(bfs([[1,2],[3],[3],[0],[]],0)));What is the exact console output in the Breadth-first search distances example 2?
const adj=[[1,2],[3],[3],[]],q=[0],seen=new Set([0]);for(let h=0;h<q.length;h++)for(const v of adj[q[h]])if(!seen.has(v)){seen.add(v);q.push(v);}console.log(JSON.stringify(q));