CS concepts

BFS vs DFS Explained: Which Graph Search to Use in Interviews

Updated Nov 27, 2026 · 3 min read

BFS explores level by level with a queue, so on an unweighted graph or grid it reaches every node by the fewest moves and finds the shortest path. DFS goes deep down one path and backtracks, using a stack or recursion, which makes it the natural fit for every path, every combination or an order. Both visit everything in O(V + E).

Watch onYouTubeInstagram

Key points

  • ●BFS uses a queue (collections.deque) and spreads out one step at a time.
  • ●DFS uses a stack or recursion and runs down one path before backing up.
  • ●Shortest, fewest steps or minimum moves on an unweighted graph: BFS.
  • ●Every path, every combination, or an order: DFS.
  • ●Just visiting everything, like Number of Islands, is a tie at O(V + E).
Step-by-step visual sheet: BFS vs DFS
The full visual sheet, made with Edithly.Open the interactive sheet

The maze

You're at S in a maze and the exit is at E. There are two classic ways to search for it, and on this maze they give different answers. DFS heads down a long hallway and gets out in 4 moves. BFS finds the exit in 2.

Both are correct searches. Only one found the shortest route. That difference is most of what you need to know to pick between them in an interview.

How BFS works: a ripple

Breadth-first search spreads out like a ripple in water. It visits every spot one step from the start, then every spot two steps away, then three.

To keep that order, it uses a queue: first in, first out. In Python that's collections.deque. You pop from the front, and push new neighbors onto the back.

from collections import deque

def bfs(start, neighbors):
    seen = {start}
    queue = deque([start])
    while queue:
        node = queue.popleft()
        for nxt in neighbors(node):
            if nxt not in seen:
                seen.add(nxt)
                queue.append(nxt)

Because it finishes each distance before moving to the next, the first time BFS reaches a node is always by the fewest edges.

How DFS works: one hallway at a time

Depth-first search runs down one hallway as far as it can. Dead end? It backs up to the last junction and tries the next way.

It uses a stack, either one you manage yourself or the call stack through recursion.

def dfs(node, neighbors, seen):
    seen.add(node)
    for nxt in neighbors(node):
        if nxt not in seen:
            dfs(nxt, neighbors, seen)

DFS will find a path to the exit, but not necessarily the shortest one. In the maze, the first hallway it tried happened to be the long way around.

BFS vs DFS, round by round

The visual sheet above scores them on six points. Here they are in one table:

BFS DFS
How it moves Level by level Deep down one path, then backtrack
Data structure Queue (deque) Stack or recursion
Shortest path, no weights Finds it Finds a path, not the shortest
Memory on wide graphs Holds a whole level, grows with width Holds one path, grows with depth
Every path or combination No stated advantage Natural fit: try, go deeper, undo
Visiting everything O(V + E) O(V + E)

Round one, shortest path. On an unweighted graph or grid, BFS wins every time. Rotting Oranges and Word Ladder are both BFS problems.

Round two, memory. BFS keeps a full level in its queue, so a wide graph makes it heavy. DFS only remembers the current path, so it wins on wide graphs. The catch: a very deep graph can push recursive DFS past Python's default recursion limit of 1,000 calls.

Round three, every path. Backtracking problems like Word Search and Permutations are DFS by nature: try a choice, go deeper, undo it. Course Schedule works with DFS, and also with BFS through Kahn's algorithm.

Round four, visit everything. For Number of Islands or counting connected components, it's a tie. Both are O(V + E).

How to pick in an interview

Read the question for one of two signals:

  • It says shortest, fewest steps or minimum moves? Pick BFS.
  • It needs every path, every combination or an order? Pick DFS.

If it only needs to visit everything, either works, so use whichever you can write fastest without bugs.

The trap to watch for

Using DFS for a shortest-path question. It returns a valid path, the tests with tiny inputs might pass, and then it gives a longer answer on a bigger grid. If the word "shortest" or "minimum" is in the question and the edges have no weights, reach for the queue.

Frequently asked questions

Why does BFS find the shortest path and DFS doesn't?

BFS visits everything 1 step away, then everything 2 steps away, and so on, so the first time it reaches a node is by the fewest edges. DFS commits to one path first, so the first path it finds to the exit can be a long one.

Which uses more memory, BFS or DFS?

On a wide graph, BFS usually does, because its queue holds a whole level at once. DFS holds one path, so its memory grows with depth instead of width. On a very deep graph, recursive DFS can hit Python's default recursion limit of 1,000.

Is BFS or DFS faster?

For visiting every node, neither. Both run in O(V + E) time, where V is the number of nodes and E the number of edges. The choice comes down to what the question asks for.

Should I use BFS or DFS for Course Schedule?

Either works. DFS can detect a cycle while it goes deep, and BFS works through Kahn's algorithm, which repeatedly takes courses with no remaining prerequisites.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free