Interview concept

What is breadth-first search (BFS)?

Breadth-first search explores nodes in order of distance from the start using a queue, visiting all neighbors before going deeper. It finds shortest paths in unweighted graphs.

Interview concept

How it works

Queue-based traversal

Use a queue and a visited set to process nodes in expanding layers from the source.

Shortest paths

Because BFS expands by distance, the first time it reaches a node is via a shortest unweighted path.

When to use it

Prefer BFS for level-order traversal, shortest path in unweighted graphs, and minimum-step problems.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When should I use BFS over DFS?

Use BFS when you need the shortest path in an unweighted graph or level-order processing.

What is the complexity of BFS?

BFS runs in O(V + E) time, visiting every vertex and edge once, with O(V) space for the queue and visited set.