# Graph Traversal Algorithms (BFS, DFS)

In competitive programming, graph traversal algorithms play a crucial role in solving various problems efficiently. Two commonly used algorithms for graph traversal are Breadth-First Search (BFS) and Depth-First Search (DFS). These algorithms enable us to visit all the nodes or vertices of a graph in a systematic manner.

## Breadth-First Search (BFS)

BFS is a graph traversal algorithm that explores all the vertices of a graph in a breadthward motion. It starts at a given source node and visits all the neighboring nodes before moving on to the next level.

### Working of BFS Algorithm

1. Initialize a queue and add the source node to it.
2. Create a visited array to keep track of visited nodes, initially marking all nodes as unvisited.
3. While the queue is not empty, do the following:
• Dequeue a node from the queue.
• Mark the dequeued node as visited.
• Visit the node (perform required operations).
• Enqueue all the unvisited neighbors of the dequeued node to the queue.
4. Repeat steps 3 until the queue becomes empty.

### Applications of BFS Algorithm

BFS algorithm is widely used in various applications, including:

• Shortest path algorithms like Dijkstra's algorithm and Bellman-Ford algorithm.
• Connectivity analysis in a graph.
• Puzzle solving algorithms like the 15-puzzle or Rubik's Cube.

## Depth-First Search (DFS)

DFS is a graph traversal algorithm that explores all the vertices of a graph in a depthward motion. It starts at a given source node and continuously explores as far as possible before backtracking.

### Working of DFS Algorithm

1. Initialize a stack and add the source node to it.
2. Create a visited array to keep track of visited nodes, initially marking all nodes as unvisited.
3. While the stack is not empty, do the following:
• Pop a node from the stack.
• Mark the popped node as visited.
• Visit the node (perform required operations).
• Push all the unvisited neighbors of the popped node to the stack.
4. Repeat steps 3 until the stack becomes empty.

### Applications of DFS Algorithm

DFS algorithm has various applications, including:

• Finding connected components in a graph.
• Topological sorting.
• Detecting cycles in a graph.
• Solving maze-related problems.

## Pros and Cons of BFS and DFS

Both BFS and DFS have their own advantages and disadvantages.

### BFS Pros

• Guarantee the shortest path for unweighted graphs.
• Useful in finding all reachable nodes from a given source.
• Ideal for finding the minimum cost in unweighted graphs.

### BFS Cons

• Requires additional memory to keep track of visited nodes.
• Inefficient for large, dense graphs.
• Does not work well for problems that require backtracking.

### DFS Pros

• Simpler to implement.
• Suitable for solving problems with finite solutions or finding just one solution.
• Efficient for solving problems that involve backtracking.

### DFS Cons

• Does not guarantee the shortest path.
• Can get stuck in an infinite loop if not implemented properly.
• Inefficient for finding all reachable nodes.

## Conclusion

Graph traversal algorithms, such as BFS and DFS, are essential tools in solving competitive programming problems involving graphs. By understanding how these algorithms work and their respective advantages and disadvantages, programmers can effectively analyze and solve graph-related problems in a more efficient manner. So, mastering these algorithms is a must for any competitive programmer.

noob to master © copyleft