Breadth First Search (BFS)

Here, We will discuss about Breadth First Search (BFS), their algorithms and also explain advantages, disadvantages, time complexity and their applications.

Graph Traversals

Graph traversal means visiting every vertex and edge exactly once in a well-defined order.

During a traversal, it is important that you track which vertices have been visited. The most common way of tracking vertices is to mark them

There are two such algorithm for traversing the graphs are:

  1. Breadth First Search (BFS)
  2. Depth First Search (DFS)

What is Breadth First Search(BFS)?

Breadth First Search (BFS) is a traversing algorithm for exploring a tree or graph. It works similar to level-order traversal of the trees. BFS works level by level.

BFS is better choice if the solution closer to the top of the tree.

This algorithm is a generalization of the breadth-first traversal algorithm for binary trees. It uses a queue.

Breadth First Search(BFS) Algorithm

BFS start traversing from a selected node(source or starting node) and traverse the graph layer-wise thus exploring the neighbour nodes (nodes which are directly connected to source node).

It continues this process until all levels of the graph are completed. Queue data structure is using for storing the vertices of a level.

To traverse the graph breadthwise as follow:

  1. First move horizontally and visit all the nodes of the current layer.
  2. Move to the next layer
breadth first search - graph traversal
  1. BFS will find the shortest path between the starting point and any other reachable node.
  1. A BFS on a binary tree generally requires more memory than a DFS.

Time Complexity of Breadth First Search (BFS) is O(V+E) where V is the number of nodes and E is the number of edges.

If we use adjacency list for representing graphs, then Time complexity is O(V2) for adjacency matrix representation.

Applications of Breadth First Search(BFS)

  1. Finding all connected components in a graph
  2. Finding all nodes within one connected component
  3. Find the shortest path between two nodes
  4. Testing a graph for bipartiteness.

Want to Contribute:-

If you like “To The Innovation” and want to contribute, you can mail your articles to 📧 See your articles on the main page and help other coders.😎

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top