Artificial Intelligence
Compare and contrast DFS and BFS.
BFS and DFS Algorithms
Feature | BFS | DFS |
---|---|---|
Full Form | Breadth First Search | Depth First Search |
Starting Point | Starts from the root node and expands all nodes at the current level before moving to the next level. | Starts from the root node and follows each path to its greatest depth node before moving to the next path. |
Data Structure Used | Queue | Stack |
Speed | Comparatively slower than DFS | Comparatively faster than BFS |
Memory Usage | Requires more memory compared to DFS | Requires less memory compared to BFS |
Concept | FIFO (First In First Out) | LIFO (Last In First Out) |
Traversal Order | According to tree level | According to tree depth |
Backtracking | No need for backtracking | Backtracking is required |
Infinite Loop Trap | Cannot be trapped in infinite loops | Can be trapped in infinite loops |
Shallowest Path Solution | Provides the shallowest path solution | Does not guarantee the shallowest path solution |
Best for Target Location | Closer to the source | Farther from the source |
Node Visitation Order | Siblings are visited before children | Children are visited before siblings |