about // doc

DFS and BFS

Created: Mar 2016

Last updated: Mar 2016

  • What is the difference between DFS and BFS?

Generally, DFS is better when searching node deep in the branch, while BFS is better when the node is shallow, which is self-explanatory from the names.

Implementation-wise, DFS and BFS is essentially the same except that the former uses a stack while the latter uses a queue.

This kind of discussion is readily available online, such as this one.

The implementation of both in Python is as follows. Simple but powerful idea.

comments powered by Disqus