What key characteristic of Breadth-First Search is implemented by using a queue?
function bfs(graph, start) {
const visited = new Set();
const queue = [start];
visited.add(start);
while (queue.length) {
const vertex = queue.shift();
for (let neighbor of graph[vertex]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
return visited;
}
Queue ensures level-by-level exploration because: 1) Processes vertices in order of their distance from start, 2) Visits all vertices at current level before moving to next level, 3) Maintains FIFO order for vertex processing, 4) Guarantees shortest path in unweighted graphs, 5) Uses O(V) extra space for queue and visited set, 6) Essential for level-order traversal algorithms, 7) Important for shortest path problems, 8) Common in social network connection algorithms.