## BFS algorithmIn this article, we will discuss the BFS algorithm in the data structure. Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node and explores all the neighboring nodes. Then, it selects the nearest node and explores all the unexplored nodes. While using BFS for traversal, any node in the graph can be considered as the root node. There are many ways to traverse the graph, but among them, BFS is the most commonly used approach. It is a recursive algorithm to search all the vertices of a tree or graph data structure. BFS puts every vertex of the graph into two categories - visited and non-visited. It selects a single node in a graph and, after that, visits all the nodes adjacent to the selected node. ## Applications of BFS algorithmThe applications of breadth-first-algorithm are given as follows - - BFS can be used to find the neighboring locations from a given source location.
- In a peer-to-peer network, BFS algorithm can be used as a traversal method to find all the neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc. employ this process to find "seeds" and "peers" in the network.
- BFS can be used in web crawlers to create web page indexes. It is one of the main algorithms that can be used to index web pages. It starts traversing from the source page and follows the links associated with the page. Here, every web page is considered as a node in the graph.
- BFS is used to determine the shortest path and minimum spanning tree.
- BFS is also used in Cheney's technique to duplicate the garbage collection.
- It can be used in ford-Fulkerson method to compute the maximum flow in a flow network.
## AlgorithmThe steps involved in the BFS algorithm to explore a graph are given as follows -
their STATUS = 2 (waiting state) [END OF LOOP]
## Example of BFS algorithmNow, let's understand the working of BFS algorithm by using an example. In the example given below, there is a directed graph having 7 vertices. In the above graph, minimum path 'P' can be found by using the BFS that will start from Node A and end at Node E. The algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that are to be processed, while QUEUE2 holds all the nodes that are processed and deleted from QUEUE1. Now, let's start examining the graph starting from Node A.
## Complexity of BFS algorithmTime complexity of BFS depends upon the data structure used to represent the graph. The time complexity of BFS algorithm is The space complexity of BFS can be expressed as ## Implementation of BFS algorithmNow, let's see the implementation of BFS algorithm in java. In this code, we are using the adjacency list to represent our graph. Implementing the Breadth-First Search algorithm in Java makes it much easier to deal with the adjacency list since we only have to travel through the list of nodes attached to each node once the node is dequeued from the head (or start) of the queue. In this example, the graph that we are using to demonstrate the code is given as follows -
## ConclusionIn this article, we have discussed the Breadth-first search technique along with its example, complexity, and implementation in java programming language. Here, we have also seen the real-life applications of BFS that show it the important data structure algorithm. So, that's all about the article. Hope, it will be helpful and informative to you. Next TopicDFS Algorithm |