Open In App

Illustrate the difference in peak memory consumption between DFS and BFS

To understand this let’s take a binary tree

Binary tree

If we conduct BFS in this tree:



Level-0 

Level-1

Level-2

Level-3

But in the case of DFS in this tree, you’ll never have more than 4 nodes in memory

Depth-first search

The difference in peak memory consumption between DFS and BFS:

So, we can conclude that the maximum space used by BFS or DFS is based on the structure of the tree. There can be cases when DFS takes less space than BFS and the opposite can also happen.



Article Tags :