BFS VS DFS
BFS VS DFS
BFS VS DFS
BFS stands for Breadth First DFS stands for Depth First
1. Stands for
Search. Search.
Conceptual BFS builds the tree level by DFS builds the tree sub-tree by
5.
Difference level. sub-tree.
Visiting of
Here, siblings are visited before Here, children are visited before
10. Siblings/
the children. the siblings.
Children
Removal of Nodes that are traversed several The visited nodes are added to
11. Traversed times are deleted from the the stack and then removed when
Nodes queue. there are no more nodes to visit.
14. Memory BFS requires more memory. DFS requires less memory.
BFS is optimal for finding the DFS is not optimal for finding
15. Optimality
shortest path. the shortest path.
When the target is close to the When the target is far from the
19. When to use?
source, BFS performs better. source, DFS is preferable.