Breadth-first search (BFS) algorithm is often used for traversing/searching a tree/graph data structure. It starts at the root (in the case of a tree) or some arbitrary node (in the case of a graph) and explores all its neighbors, followed by the next-level neighbors, and so on.
Consider the following tree which marks the order in which the nodes will be discovered in BFS:
In this post, we have listed out some of the commonly asked interview questions that can be solved using the BFS algorithm:
- Breadth-First Search (BFS)
- Transitive closure of a graph
- Check if a graph is strongly connected or not
- Find root vertex of a graph
- Efficiently print all nodes between two given levels in a binary tree
- Chess Knight Problem | Find the shortest path from source to destination
- Shortest path in a maze – Lee Algorithm
- Find the shortest safe route in a field with sensors present
- Flood Fill Algorithm
- Count number of islands
- Find the shortest path from source to destination in a matrix that satisfies given constraints
- Calculate the height of a binary tree
- Delete a binary tree
- Level order traversal of a binary tree
- Spiral order traversal of a binary tree
- Reverse level order traversal of a binary tree
- Print all nodes of a perfect binary tree in a specific order
- Print left view of a binary tree
- Find the next node at the same level as the given node in a binary tree
- Check if a binary tree is a complete binary tree or not
- Print diagonal traversal of a binary tree
- Print corner nodes of every level in a binary tree
- Invert Binary Tree
- Find minimum passes required to convert all negative values in a matrix
- Convert a binary tree into a doubly-linked list in spiral order
- Check if a binary tree is a min-heap or not
- Invert alternate levels of a perfect binary tree
- Snake and Ladder Problem
- Find the shortest distance of every cell from a landmine inside a maze
- Check if an undirected graph contains a cycle or not
- Find maximum cost path in a graph from a given source to a given destination
- Total paths in a digraph from a given source to a destination having exactly
medges - Least cost path in a digraph from a given source to a destination having exactly
medges - Traverse a given directory using BFS and DFS in Java
- Perform vertical traversal of a binary tree
- Compute the maximum number of nodes at any level in a binary tree
- Print right view of a binary tree
- Find the minimum depth of a binary tree
- Depth-First Search (DFS) vs Breadth-First Search (BFS)
- Bipartite Graph
- Compute the least cost path in a weighted digraph using BFS
- Find the path between given vertices in a directed graph
- Construct a directed graph from an undirected graph that satisfies given constraints
- Print nodes of a binary tree in vertical order
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)