# Difference between BFS and DFS

AlgorithmsAnalysis of AlgorithmsData Structure and Algorithms

BFS and DFS are graph traversal algorithms.

## BFS

Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue to remember to get the next vertex to start a search when a dead end occurs in any iteration. ## DFS

Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search when a dead end occurs in any iteration. Following are the important differences between BFS and DFS.

Sr. No.KeyBFSDFS
1DefinitionBFS, stands for Breadth First Search.DFS, stands for Depth First Search.
2Data structureBFS uses Queue to find the shortest path.DFS uses Stack to find the shortest path.
3SourceBFS is better when target is closer to Source.DFS is better when target is far from source.
4Suitablity for decision treeAs BFS considers all neighbour so it is not suitable for decision tree used in puzzle games.DFS is more suitable for decision tree. As with one decision, we need to traverse further to augment the decision. If we reach the conclusion, we won.
5SpeedBFS is slower than DFS.DFS is faster than BFS.
6Time ComplexityTime Complexity of BFS = O(V+E) where V is vertices and E is edges.Time Complexity of DFS is also O(V+E) where V is vertices and E is edges.