BFS(Breadth First Search) is a graph traversal technique where a node and its neighbors are visited first and then the neighbors of neighbors. In simple terms it traverses level wise from the source. First it traverses level 1 nodes (direct neighbors of source node) and then level 2 nodes (neighbors of neighbors of source node) and so on.
Now, suppose if we have to know at which level all the nodes are at (from source node). Then BFS can be used to determine the level of each node.
Input : Output : Node Level 0 0 1 1 2 1 3 2 4 2 5 2 6 2 7 3 Explanation :
Nodes Level 0 --> 0 1 --> 1 2 --> 1 3 --> 2 4 --> 2 5 --> 2 6 --> 2 7 --> 3
- Minimum cost path from source node to destination node via an intermediate node
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- XOR of all the nodes in the sub-tree of the given node
- Right sibling of each node in a tree given as array of edges
- Find the node whose sum with X has minimum set bits
- Find the node whose sum with X has maximum set bits
- Find all reachable nodes from every node present in a given set
- Find the node whose absolute difference with X gives minimum value
- Print the degree of every node from the given Prufer sequence
- Find the node whose absolute difference with X gives maximum value
- Number of ways to select a node from each connected component
- Maximum number of nodes which can be reached from each node in a graph.
- Number of loops of size k starting from a specific node
- Print the node with the maximum degree in the prufer sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.