What is BFS Algorithm? Breath First Search Algorithm Explained
Updated on Feb 17, 2025 | 9 min read | 4.2k views
Share:
For working professionals
For fresh graduates
More
Updated on Feb 17, 2025 | 9 min read | 4.2k views
Share:
Table of Contents
BFS is a graph traversal technique to explore and analyse graphs. It systematically visits all the neighbouring vertices of a current vertex before moving on to the next level of vertices.
Read on to learn about BFS.
Graph traversal algorithms in data structures are essential techniques for systematically visiting and exploring each node or vertex within a graph. They play a crucial role in understanding the relationships and connectivity present in complex data structures.
Graph traversal algorithms facilitate the examination of graphs by navigating through their nodes in a specific order.
Graph traversal algorithms are widely used in network analysis, routing algorithms, and web crawling, among other fields. They empower efficient analysis and ease the decision-making processes.
The two main approaches for graph traversal are:
Unlike BFS, DFS explores a graph by traversing as far as possible along each branch before backtracking.
BFS algorithm starts from a given source vertex and explores the graph layer by layer, examining all the vertices at the same level before descending further. It uses a queue data structure to maintain the order of exploration, ensuring that vertices are visited in a breadth first manner.
To implement BFS, a queue data structure is used to maintain the exploration order. The algorithm begins by enqueuing the source vertex and marking it as visited. Then, while the queue is not empty, it dequeues a vertex, visits its adjacent unvisited vertices, enqueues them, and marks them as visited.
This process continues until all vertices have been visited or until the desired condition is met. Using this approach, BFS guarantees that vertices in the BFS graph are visited in order of their distance from the source vertex.
There are several reasons why using the BFS algorithm is essential:
Programmers often use breadth first search Python for various applications in graph theory and data structures.
Some important rules to remember when implementing the breadth first search algorithm for graph traversal:
The architecture of the BFS algorithm is broken down below:
Enrol for the Machine Learning Course from the World’s top Universities. Earn Master, Executive PGP, or Advanced Certificate Programs to fast-track your career.
BFS(graph, start_vertex):
queue = create an empty queue
visited = create an empty set or array to track visited vertices
enqueue start_vertex into the queue
add start_vertex to visited
while queue is not empty:
current_vertex = dequeue from the queue
process(current_vertex) // e.g., print or perform operations on the current_vertex
for each neighbour in graph[current_vertex]:
if neighbour is not in visited:
enqueue neighbour into the queue
add neighbour to visited
The BFS algorithm starts by initialising an empty queue and an empty set/array to track visited vertices. It begins the traversal from the start_vertex, which is enqueued into the queue and marked as visited.
The algorithm then enters a while loop that continues as long as its queue remains nonempty. At each iteration, a vertex at the front of its queue (denoted by current_vertex) is removed and processed, either through operations on it or printing it out.
Next, the algorithm explores all the neighbours of the current_vertex. For each unvisited neighbour, it enqueues the neighbour into the queue and marks it as visited.
The process continues until the queue becomes empty, indicating that all reachable vertices from the start_vertex have been visited.
You can also check out our free courses offered by upGrad in Management, Data Science, Machine Learning, Digital Marketing, and Technology. All of these courses have top-notch learning resources, weekly live lectures, industry assignments, and a certificate of course completion – all free of cost!
Let’s consider the following graph to understand a BFS example:
A––B
| |
C––D
We want to perform a breadth first search (BFS) traversal from vertex A to visit all the vertices.
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
The BFS algorithm has now traversed all vertices reachable from the starting vertex A in a breadth-first manner. The order of traversal is A, B, C, and D.
The complexity of the breadth first search algorithm can be analysed in terms of time and space complexity.
BFS has an O(V + E) time complexity, where V represents the number of nodes (vertices) in the graph, and E represents its edges. When dequeuing from its queue or exploring its adjacent vertices, BFS attempts to visit all nodes and edges once. Thus its time complexity increases linearly as more nodes and edges enter or exit it.
Space complexity for BFS grows linearly with the number of vertices in a graph (V), represented as an integer value. This is because even under extreme conditions, the BFS queue can simultaneously contain all vertices in its maximum level traversal. Furthermore, its visited array or set requires O(V) space to store visited vertices visited during traversal; hence BFS grows linearly in space complexity with each increase in graph vertex count.
Here are a few examples of the numerous applications of the BFS algorithm in various domains, showcasing its versatility and usefulness in graph exploration and analysis:
The Breadth First Search (BFS) algorithm is an invaluable tool for exploring and analysing graphs in a breadth-first manner. Its efficiency, accuracy in finding the shortest path, and versatility in various applications make it a fundamental technique in data structures and algorithms.
Aspiring IT professionals or software developers looking to enhance their skills and master graph traversal algorithms like DFS and BFS can check out upGrad’s Advanced Certificate Programme in Machine Learning & NLP from IIITB. Put yourself at the forefront of a technologically evolving landscape with upGrad.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources