Javatpoint Logo
Javatpoint Logo

BFS Algorithm in Java

What is BFS?

Breadth-First Search (BFS) is based on traversing nodes by adding the neighbors of each node to the traversal queue starting from the root node. The BFS for a graph is similar to that of a tree, with the exception that graphs may have cycles. In contrast to depth-first search, all neighbor nodes at a given depth are investigated before proceeding to the next level.

BFS Algorithm

The following are the steps involved in employing breadth-first search to explore a graph:

  1. Take the data for the graph's adjacency matrix or adjacency list.
  2. Create a queue and fill it with items.
  3. Activate the root node (meaning that get the root node at the beginning of the queue).
  4. Dequeue the queue's head (or initial element), then enqueue all of the queue's nearby nodes from left to right. Simply dequeue the head and resume the operation if a node has no nearby nodes that need to be investigated. (Note: If a neighbor emerges that has previously been investigated or is in the queue, don't enqueue it; instead, skip it.)
  5. Continue in this manner until the queue is empty.

BFS Applications

Because of the algorithm's flexibility, Breadth-First Search is quite useful in the real world. These are some of them:

  1. In a peer-to-peer network, peer nodes are discovered. Most torrent clients, such as BitTorrent, uTorrent, and qBittorent, employ this process to find "seeds" and "peers" in the network.
  2. The index is built using graph traversal techniques in web crawling. The procedure starts with the source page as the root node and works its way down to all secondary pages that are linked to the source page (and this process continues). Because of the reduced depth of the recursion tree, Breadth-First Search has an inherent advantage here.
  3. The use of GPS navigation systems using the GPS, conduct a breadth-first search to locate nearby sites.
  4. Cheney's technique, which employs the concept of breadth-first search, is used to collect garbage.

Example BFS Traversal

To get started, let's look at a simple example. We'll start with 0 as the root node and work our way down the graph.

BFS Algorithm in Java

Step 1: Enqueue(0)



Step 2: Dequeue(0), Enqueue(1), Enqueue(3), Enqueue(4)


1 3 4

Step 3: Dequeue(1), Enqueue(2)


3 4 2

Step 4: Dequeue(3), Enqueue(5). We won't add 1 to the queue again because 0 has already been explored.


4 2 5

Step 5: Dequeue(4)


2 5

Step 6: Dequeue(2)



Step 7: Dequeue(5)


The queue is empty now so we'll stop the process.

Breadth-First Search Java Program

There are several approaches of dealing with the code. We'll mostly discuss the steps involved in implementing a breadth first search in Java. An adjacency list or an adjacency matrix can be used to store graphs; either method is acceptable. The adjacency list will be used to represent our graph in our code. When implementing the Breadth-First Search algorithm in Java, it is 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.

The graph used to demonstrate the code will be identical to the one used in the previous example.


Breadth First Traversal for the graph is:
0 1 3 4 2 5

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA