What is the difference between BFS and DFS, and when do you use each?

3 stars If you are short on time, start with this one.

In one or two sentences
BFS explores layer by layer from the nearest, using a queue. DFS goes as far as it can along one path and then backtracks, using a stack or recursion.

Think of your own answer first

Say it out loud, or type it below. What you type never leaves this device.

Follow-up questions

These are likely next questions from an interviewer. Think of your answer first, then press the button.

  1. 1 / 3

    Why does BFS guarantee a shortest path?

Sources checked

This is a textbook level concept, so no outside source is listed.