13

Sep# Breadth First Search vs Depth First Search

## BFS Vs. DFS: An Overview

Breadth First Search and Depth First Search are the two techniques of **Graph Traversal**. The breadth-first search (BFS) algorithm explores all the vertices of a graph in a breadthwise order. Whereas in the case of depth-first search, the algorithm works by starting from an arbitrary node of the graph and exploring as far as possible before backtracking.

We have seen their detailed work in the tutorial, **Breadth First Traversal and Depth First Traversal**. If you haven't seen that tutorial, go back and refer to it. In this **DSA tutorial**, we will take a look at the differentiating factors between them. For further insights, consider enrolling in the **Data Structures and Algorithms Certification Course**.

## What is Breadth First Search?

Breadth-First Search (BFS) is a graph traversal algorithm that explores all the vertices of a **graph** in a breadthwise order. It means it starts at a given vertex or node and visits all the vertices at the same level before moving to the next level. It is a **recursive algorithm** for searching all the vertices of a **graph** or **tree data structure**. It uses a **queue data structure** to keep track of the vertices to visit.

#### Breadth-First Search (BFS) Example

## What is Depth First Search?

The depth-first search algorithm works by starting from an arbitrary node of the graph and exploring as far as possible before backtracking i.e. moving to an adjacent node until there is no unvisited adjacent node. After backtracking, it repeats the same process for all the remaining vertices which have not been visited till now. It is a recursive algorithm for searching all the vertices of a graph or tree data structure. It uses a **stack data structure** to keep track of the vertices to visit.

#### Depth-First Search (DFS) Example

## Differences between BFS and DFS

Parameters | BFS | DFS |

Full form | BFS is Breadth First Search. | DFS is Depth First Search. |

Traversal Order | It first explores the graph through all nodes on the same level before moving on to the next level. | It explores the graph from the root node and proceeds through the nodes as far as possible until we reach the node with no unvisited nearby nodes. |

Data Structure | BFS uses a Queue to find the shortest path. | DFS uses a Stack to find the shortest path. |

Principle | It works on the concept of FIFO (First In First Out). | It works on the concept of LIFO (Last In First Out). |

Time Complexity | It is O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges. | It is also O(V + E) when Adjacency List is used and O(V^2) when Adjacency Matrix is used, where V stands for vertices and E stands for edges |

Suitable for | BFS is more suitable for searching vertices closer to the given source. | DFS is more suitable when there are solutions away from the source. |

Speed | BFS is slower than DFS. | DFS is faster than BFS. |

Memory | BFS requires more memory space. | DFS requires less memory space. |

Suitability for decision tree | BFS considers all neighbors first and is therefore not suitable for decision-making trees used in games or puzzles. | DFS is more suitable for decision trees. As with one decision, we need to traverse further to augment the decision. If we conclude, we won. |

Backtracking | In BFS there is no concept of backtracking. | DFS algorithm is a recursive algorithm that uses the idea of backtracking. |

Trapping in loops | In BFS, there is no problem of trapping into infinite loops. | In DFS, we may be trapped in infinite loops. |

Visiting of Siblings/ Children | Here, siblings are visited before the children. | Here, children are visited before their siblings. |

Applications | BFS is used in various applications such as bipartite graphs, shortest paths, etc. | DFS is used in various applications such as acyclic graphs and topological order etc. |

Conceptual Difference | BFS builds the tree level by level. | DFS builds the tree sub-tree by sub-tree. |

Optimality | BFS is optimal for finding the shortest path. | DFS is not optimal for finding the shortest path. |

**Read More: DSA Interview Questions and Answers**

## Considerations for Choosing Between BFS and DFS

**Problem Requirements:**Figure out whether the problem involves finding the shortest path, detecting cycles, or searching for a feasible solution.**Graph Characteristics:**BFS is generally more suitable for sparse graphs or graphs with many short paths, while DFS is often used for dense graphs or graphs with deep paths.**Space Complexity:**BFS requires more memory to maintain the queue for storing nodes at each level, while DFS uses less memory as it traverses deeply before backtracking.**Time Complexity:**BFS has a higher time complexity than DFS, especially for large graphs, due to its breadth-first nature and the need to explore all neighboring nodes at each level.**Optimality:**BFS guarantees the shortest path in unweighted graphs while DFS does not guarantee optimality.**Search Space:**BFS explores all possible nodes at each level before moving to the next level, making it suitable for problems where the solution lies close to the starting node. DFS explores deeply into the search space and is suitable for problems with deep paths or where backtracking is necessary.**Implementation Complexity:**BFS is relatively straightforward to implement using a queue data structure, while DFS may require recursion or maintaining a stack explicitly.**Cycle Detection:**DFS is commonly used for cycle detection in graphs due to its ability to backtrack and explore deeply into the graph.

##### Summary

In this tutorial, we saw the differences between BFS and DFS. Understanding these two types of graph traversal methods is essential to understanding the graph data structure. To test your practical understanding of these two techniques, enroll in our **Best Data Structures And Algorithms Course**.