Breadth-First Search (BFS)
Definition
Breadth-First Search explores the graph level by level. Starting from a source node, it visits all the adjacent nodes (neighbors) before moving on to nodes at the next level. BFS uses a queue to keep track of nodes yet to be explored.
Contents
Advantages
- Finds the shortest path in an unweighted graph.
- Guaranteed to visit all nodes at a given depth before exploring deeper levels.
- Ideal for finding connected components in graphs.
- Efficient in problems involving level-order traversal of a tree.
- Avoids unnecessary paths by exploring breadth-first.
Disadvantages
- Uses more memory than DFS since it stores all nodes at the current level.
- May be slower for deep graphs with many levels.
Uses
- Finding shortest paths in unweighted graphs or maps.
- Web crawling to visit all pages linked from a website.
- Network broadcasting to traverse all nodes in a network.
- Maze solving to explore all possible paths evenly.
- Level-order traversal of binary or n-ary trees.
Depth-First Search (DFS)
Definition
Depth-First Search explores as far as possible along a branch before backtracking. It uses a stack (or recursion) to keep track of the visited nodes and the nodes to be explored next.
Advantages
- Memory efficient for deep graphs since it stores only the current path.
- Useful for topological sorting of graphs.
- Good for solving backtracking problems like puzzles (e.g., Sudoku).
- Effective in cycle detection in graphs.
- Can be used for finding strongly connected components in a graph.
Disadvantages
- May explore paths unnecessarily in some cases, leading to inefficient search.
- Can go into infinite loops in cyclic graphs without proper visited-node tracking.
Uses
- Maze solving to explore all paths to a goal.
- Cycle detection in directed and undirected graphs.
- Topological sorting for scheduling dependencies.
- Finding strongly connected components in a directed graph.
- Backtracking algorithms for constraint-based problems (e.g., N-Queens problem).
When to Use BFS and DFS
- Use BFS when you need to find the shortest path or explore all nodes at a specific depth (level-order).
- Use DFS when solving puzzles, performing backtracking, or topological sorting in directed acyclic graphs.
Â
Â
BFS | DFS | |
---|---|---|
Stands for | BFS stands for Breadth First Search. | DFS stands for Depth First Search. |
Data Structure | BFS(Breadth First Search) uses Queue data structure for finding the shortest path. | DFS(Depth First Search) uses Stack data structure. |
Definition | BFS is a traversal approach in which we first walk through all nodes on the same level before moving on to the next level. | DFS is also a traversal approach in which the traverse begins at the root node and proceeds through the nodes as far as possible until we reach the node with no unvisited nearby nodes. |
Data Structure | Queue | Stack or recursion |
Traversal Order | Explores neighbors first (level-wise) | Explores deeper nodes first |
Shortest Path | Finds the shortest path in unweighted graphs | Not guaranteed to find the shortest path |
Memory Usage | Higher (stores all nodes at a level) | Lower (only keeps track of the current path) |
Completeness | Complete if the graph is finite | May not terminate if cycles exist without checks |