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. | Key | BFS | DFS |
---|---|---|---|
1 | Definition | BFS, stands for Breadth First Search. | DFS, stands for Depth First Search. |
2 | Data structure | BFS uses Queue to find the shortest path. | DFS uses Stack to find the shortest path. |
3 | Source | BFS is better when target is closer to Source. | DFS is better when target is far from source. |
4 | Suitablity for decision tree | As 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. |
5 | Speed | BFS is slower than DFS. | DFS is faster than BFS. |
6 | Time Complexity | Time 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. |