What is Breadth-First Search (BFS)?

Answer

BFS explores a graph level by level — visits all neighbors of the current node before moving to their neighbors. Uses a queue. Algorithm: (1) Start: enqueue the source node, mark it visited; (2) While queue not empty: dequeue a node, process it, enqueue all unvisited neighbors and mark them visited. Time: O(V+E) for adjacency list. Space: O(V) for the queue. Properties: Finds shortest path (in terms of number of edges) from source to all reachable nodes in an unweighted graph; processes nodes in order of distance from source. Applications: (1) Shortest path in unweighted graphs (word ladder, knight moves, maze); (2) Level-order tree traversal; (3) Finding connected components; (4) Web crawling; (5) Social network distance (degrees of separation); (6) Bipartite graph checking; (7) Garbage collection (find all reachable objects). BFS vs DFS: BFS uses more memory (stores entire frontier) but guarantees shortest path. DFS uses less memory O(h) but doesn't guarantee shortest path. Use BFS when you need shortest paths or layer-by-layer exploration.