Explorer
Node.js

Write a function `bfs(graph, start)` that performs BFS on an adjacency list graph and returns the visit order.

Problem Statement

Write a function `bfs(graph, start)` that performs BFS on an adjacency list graph and returns the visit order.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: BFS uses a queue (FIFO) and visits nodes level by level. šŸ’” Hint 2: Track visited nodes with a Set to avoid cycles. šŸ’” Hint 3: Dequeue a node, add to result, enqueue its unvisited neighbors. āœ… Solution: ```javascript function bfs(graph, start) { const visited = new Set(); const queue = [start]; const result = []; visited.add(start); while (queue.length > 0) { const node = queue.shift(); result.push(node); for (const neighbor of (graph[node] || [])) { if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); } } } return result; } ```

Graph BFS (Breadth-First Search)

Hard
Write a function `bfs(graph, start)` that performs BFS on an adjacency list graph and returns the visit order.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
["A","B","C","D"]