48

I always mix up whether I use a stack or a queue for DFS or BFS. Can someone please provide some intuition about how to remember which algorithm uses which data structure?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
captcadaver
  • 1,093
  • 2
  • 9
  • 7

16 Answers16

162

Queue can be generally thought as horizontal in structure i.e, breadth/width can be attributed to it - BFS, whereas

Stack is visualized as a vertical structure and hence has depth - DFS.

Sai Kumar Battinoju
  • 1,729
  • 2
  • 10
  • 4
43

Draw a small graph on a piece of paper and think about the order in which nodes are processed in each implementation. How does the order in which you encounter the nodes and the order in which you process the nodes differ between the searches?

One of them uses a stack (depth-first) and the other uses a queue (breadth-first) (for non-recursive implementations, at least).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
40

I remember it by keeping Barbecue in my mind. Barbecue starts with a 'B' and ends with a sound like 'q' hence BFS -> Queue and the remaining ones DFS -> stack.

kawadhiya21
  • 2,458
  • 21
  • 34
  • 1
    Good observation :) – RBT Apr 19 '17 at 01:17
  • 1
    Google likes this answer it seems. If you search for this question google extracts this answer out. So here is an upvote for that. – tryurbest Jul 13 '18 at 15:50
  • 2
    Hahaha OMG I didn't have the problem of OP when I stumbled upon this question, but after reading this answer, no way I forget that :D – M-J Nov 25 '18 at 10:44
27

BFS explores/processes the closest vertices first and then moves outwards away from the source. Given this, you want to use a data structure that when queried gives you the oldest element, based on the order they were inserted. A queue is what you need in this case since it is first-in-first-out(FIFO). Whereas a DFS explores as far as possible along each branch first and then bracktracks. For this, a stack works better since it is LIFO(last-in-first-out)

Lizz In
  • 271
  • 1
  • 3
  • 3
11

Take it in Alphabetical order...

.... B(BFS).....C......D (DFS)....

.... Q(Queue)...R......S (Stack)...

10

BFS uses always queue, Dfs uses Stack data structure. As the earlier explanation tell about DFS is using backtracking. Remember backtracking can proceed only by Stack.

ashutosh
  • 405
  • 6
  • 10
9

BFS --> B --> Barbecue --> Queue

DFS --> S --> Stack

Yans Ruan
  • 321
  • 3
  • 4
4

Don't remember anything.

Assuming the data structure used for the search is X:

Breadth First = Nodes entered X earlier, have to be generated on the tree first: X is a queue.

Depth First = Nodes entered X later, must be generated on the tree first: X is a stack.

In brief: Stack is Last-In-First-Out, which is DFS. Queue is First-In-First-Out, which is BFS.

M-J
  • 1,028
  • 11
  • 15
3

Bfs;Breadth=>queue

Dfs;Depth=>stack

Refer to their structure

DD_
  • 7,230
  • 11
  • 38
  • 59
Subrato
  • 31
  • 1
2

The depth-first search uses a Stack to remember where it should go when it reaches a dead end.

DFSS

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
1
  1. Stack (Last In First Out, LIFO). For DFS, we retrieve it from root to the farthest node as much as possible, this is the same idea as LIFO.

  2. Queue (First In First Out, FIFO). For BFS, we retrieve it one level by one leve, after we visit upper level of the node, we visit bottom level of node, this is the same idea as FIFO.

FullStackDeveloper
  • 910
  • 1
  • 16
  • 40
1

An easier way to remember, especially for young students, is to use similar acronym:

BFS => Boy FriendS in queue (for popular ladies apparently).

DFS is otherwise (stack).

HZhang
  • 175
  • 12
1

if you visually rotate 'q' symbol (as in queue) 180 degrees you will get a 'b' (as in bfs).

Otherwise this is stack and dfs.

user3067181
  • 11
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 21 '22 at 18:37
0

I would like to share this answer: https://stackoverflow.com/a/20429574/3221630

Taking BFS and replacing a the queue with a stack, reproduces the same visiting order of DFS, it uses more space than the actual DFS algorithm.

Community
  • 1
  • 1
arboreal84
  • 2,086
  • 18
  • 21
0

You can remember by making an acronym

BQDS

Beautiful Queen has Done Sins.

In Hindi, हुरानी क्यु र्द हा

Yogesh Sanchihar
  • 1,080
  • 18
  • 25
0

Here is a simple analogy to remember. In a BFS, you are going one level at a time but in DFS you are going as deep as possible to the left before visiting others. Basically stacking up a big pile of stuff, then analyze them one by one, so if this is STACK, then the other one is queue.

Remember as "piling up", "stacking up", big as possible. (DFS).

vikkee
  • 361
  • 1
  • 2
  • 12