---
description: What is BFS Algorithm (Breadth-First Search)? Breadth-first search (BFS) is an algorithm that is used to graph data or searching tree or traversing structures. The full form of BFS is the Breadth-firs
title: Breadth First Search (BFS) Algorithm with EXAMPLE
image: https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Breadth First Search (BFS) is an algorithm that traverses a graph level by level, visiting all neighbors of a node before moving deeper. It uses a FIFO queue and finds the shortest path in unweighted graphs without infinite loops.

* 📊 **Level-Order:** BFS visits every node at the current depth before moving to the next level.
* 📥 **Queue-Based:** A FIFO queue holds visited nodes so neighbors are processed in order.
* 🎯 **Shortest Path:** In unweighted graphs, BFS finds the shortest path in the fewest iterations.
* ✅ **No Loops:** Marking visited nodes prevents BFS from getting stuck in an infinite loop.
* 🌐 **Applications:** BFS powers web crawlers, P2P networks, navigation, and network broadcasting.

[ Read More ](javascript:void%280%29;) 

[![Breadth First Search (BFS) Algorithm with Example](https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png)](https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png)

## What is BFS Algorithm (Breadth-First Search)?

Breadth-first search (BFS) is an algorithm that is used to graph data or search a tree or traversing structures. The full form of BFS is the Breadth-first search.

The algorithm efficiently visits and marks all the key nodes in a graph in an accurate breadthwise fashion. This algorithm selects a single node (initial or source point) in a graph and then visits all the nodes adjacent to the selected node. Remember, BFS accesses these nodes one by one.

Once the algorithm visits and marks the starting node, then it moves towards the nearest unvisited nodes and analyses them. Once visited, all nodes are marked. These iterations continue until all the nodes of the graph have been successfully visited and marked.

## What is Graph traversals?

A graph traversal is a commonly used methodology for locating the vertex position in the graph. It is an advanced search algorithm that can analyze the graph with speed and precision along with marking the sequence of the visited vertices. This process enables you to quickly visit each node in a graph without being locked in an infinite loop.

## The architecture of BFS algorithm

[![Architecture of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs1.png)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs1.png)

1. In the various levels of the data, you can mark any node as the starting or initial node to begin traversing. The BFS will visit the node, mark it as visited, and place it in the queue.
2. Now the BFS will visit the nearest and un-visited nodes and mark them. These values are also added to the queue. The queue works on the [FIFO model](https://www.guru99.com/python-queue-example.html).
3. In a similar manner, the remaining nearest and un-visited nodes on the graph are analyzed, marked, and added to the queue. These items are deleted from the queue as they are received and printed as the result.

## Why do we need BFS Algorithm?

There are numerous reasons to utilize the BFS Algorithm for searching your dataset. Some of the most vital aspects that make this algorithm your first choice are:

* BFS is useful for analyzing the nodes in a graph and constructing the shortest path of traversing through these.
* BFS can traverse through a graph in the smallest number of iterations.
* The architecture of the BFS algorithm is simple and robust.
* The result of the BFS algorithm holds a high level of accuracy in comparison to other algorithms.
* BFS iterations are seamless, and there is no possibility of this algorithm getting caught up in an infinite loop problem.

### RELATED ARTICLES

* [B Tree in Data Structure: Search, Insert, Delete ](https://www.guru99.com/b-tree-example.html "B Tree in Data Structure: Search, Insert, Delete")
* [Binary Search Tree (BST) with Example ](https://www.guru99.com/binary-search-tree-data-structure.html "Binary Search Tree (BST) with Example")
* [Top 18 Algorithm Interview Questions and Answers (2026) ](https://www.guru99.com/algorithm-interview-questions.html "Top 18 Algorithm Interview Questions and Answers (2026)")
* [Heap Data Structure: What is Heap? ](https://www.guru99.com/heap-data-structure.html "Heap Data Structure: What is Heap?")

## How does BFS Algorithm Work?

Graph traversal requires the algorithm to visit, check, and/or update every single un-visited node in a tree-like structure. Graph traversals are categorized by the order in which they visit the nodes on the graph.

BFS algorithm starts the operation from the first or starting node in a graph and traverses it thoroughly. Once it successfully traverses the initial node, then the next non-traversed vertex in the graph is visited and marked.

Hence, you can say that all the nodes adjacent to the current vertex are visited and traversed in the first iteration. A simple queue methodology is utilized to implement the working of a BFS algorithm, and it consists of the following steps:

**Step 1)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs2.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs2.jpg)

Each vertex or node in the graph is known. For instance, you can mark the node as V.

**Step 2)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs3.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs3.jpg)

In case the vertex V is not accessed, then add the vertex V into the BFS Queue.

**Step 3)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs4.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs4.jpg)

Start the BFS search, and after completion, mark vertex V as visited.

**Step 4)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs5.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs5.jpg)

The BFS queue is still not empty, hence remove the vertex V of the graph from the queue.

**Step 5)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs6.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs6.jpg)

Retrieve all the remaining vertices on the graph that are adjacent to the vertex V.

**Step 6)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs7.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs7.jpg)

For each adjacent vertex, let’s say V1, in case it is not visited yet, then add V1 to the BFS queue.

**Step 7)**

[![Working of BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs8.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs8.jpg)

BFS will visit V1, mark it as visited, and delete it from the queue.

## Example BFS Algorithm

**Step 1)**

[![Example BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs9.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs9.jpg)

You have a graph of seven numbers ranging from 0 to 6.

**Step 2)**

[![Example BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs10.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs10.jpg)

0 or zero has been marked as a root node.

**Step 3)**

[![Example BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs11.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs11.jpg)

0 is visited, marked, and inserted into the queue data structure.

**Step 4)**

[![Example BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs12.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs12.jpg)

The remaining 0-adjacent and unvisited nodes are visited, marked, and inserted into the queue.

**Step 5)**

[![Example BFS Algorithm](https://www.guru99.com/images/1/020820_0543_BreadthFirs13.jpg)](https://www.guru99.com/images/1/020820%5F0543%5FBreadthFirs13.jpg)

Traversing iterations are repeated until all nodes are visited.

## Rules of BFS Algorithm

Here are important rules for using the BFS algorithm:

* A queue (FIFO – First in First Out) [data structure](https://www.guru99.com/graphs-in-data-structures.html) is used by BFS.
* You mark any node in the graph as the root and start traversing the data from it.
* BFS traverses all the nodes in the graph and keeps dropping them as completed.
* BFS visits an adjacent unvisited node, marks it as done, and inserts it into a queue.
* It removes the previous vertex from the queue in case no adjacent vertex is found.
* The BFS algorithm iterates until all the vertices in the graph are successfully traversed and marked as completed.
* There are no loops caused by BFS during the traversing of data from any node.

## Applications of BFS Algorithm

Let’s take a look at some of the real-life applications where a BFS algorithm implementation can be highly effective.

* **Un-weighted Graphs:** The BFS algorithm can easily create the shortest path and a minimum spanning tree to visit all the vertices of the graph in the shortest time possible with high accuracy.
* **P2P Networks:** BFS can be implemented to locate all the nearest or neighboring nodes in a peer-to-peer network. This will find the required data faster.
* **Web Crawlers:** Search engines or web crawlers can easily build multiple levels of indexes by employing BFS. BFS implementation starts from the source, which is the web page, and then it visits all the links from that source.
* **Navigation Systems:** BFS can help find all the neighboring locations from the main or source location.
* **Network Broadcasting:** A broadcasted packet is guided by the BFS algorithm to find and reach all the nodes it has the address for.

## FAQs

🤖 How is BFS used in AI and pathfinding?

In AI, BFS explores game states, puzzle configurations, and maps to find the shortest solution when every move has equal cost. It guarantees the fewest steps, though it can use a lot of memory on large graphs.

🧠 Can AI generate BFS code from a description?

Yes. AI assistants can write BFS in Python, Java, or C++ using a queue and a visited set from a plain description. Test it on sample graphs, since edge cases like disconnected nodes are easy to miss.

❓ What is the difference between BFS and DFS?

BFS explores a graph level by level using a queue and finds the shortest path in unweighted graphs. DFS explores as deep as possible along each branch using a stack or recursion before backtracking.

⏱️ What is the time complexity of BFS?

BFS runs in O(V + E) time, where V is the number of vertices and E is the number of edges, because each vertex and edge is examined once. Its space complexity is O(V) for the queue and visited set.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter](https://www.guru99.com/images/footer-email-avatar-imges-1.png) Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png","url":"https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png","width":"700","height":"250","caption":"Breadth First Search (BFS) Algorithm","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/design-algorithm","name":"Algorithm"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html","name":"Breadth First Search (BFS) Algorithm with EXAMPLE"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html#webpage","url":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html","name":"Breadth First Search (BFS) Algorithm with EXAMPLE","dateModified":"2026-07-03T17:12:48+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/sarah","name":"Sarah Chen","description":"I'm Sarah Chen, a Senior Algorithm Engineer, dedicated to guiding you through the intricacies of algorithm engineering with clear, concise advice.","url":"https://www.guru99.com/author/sarah","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/sarah-chen-author.png","url":"https://www.guru99.com/images/sarah-chen-author.png","caption":"Sarah Chen","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Algorithm","headline":"Breadth First Search (BFS) Algorithm with EXAMPLE","description":"What is BFS Algorithm (Breadth-First Search)? Breadth-first search (BFS) is an algorithm that is used to graph data or searching tree or traversing structures. The full form of BFS is the Breadth-firs","keywords":"bigdata, programming, database, server","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/sarah","name":"Sarah Chen"},"dateModified":"2026-07-03T17:12:48+05:30","image":{"@id":"https://www.guru99.com/images/breadth-first-search-bfs-algorithm.png"},"copyrightYear":"2026","name":"Breadth First Search (BFS) Algorithm with EXAMPLE","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How is BFS used in AI and pathfinding?","acceptedAnswer":{"@type":"Answer","text":"In AI, BFS explores game states, puzzle configurations, and maps to find the shortest solution when every move has equal cost. It guarantees the fewest steps, though it can use a lot of memory on large graphs."}},{"@type":"Question","name":"Can AI generate BFS code from a description?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI assistants can write BFS in Python, Java, or C++ using a queue and a visited set from a plain description. Test it on sample graphs, since edge cases like disconnected nodes are easy to miss."}},{"@type":"Question","name":"What is the difference between BFS and DFS?","acceptedAnswer":{"@type":"Answer","text":"BFS explores a graph level by level using a queue and finds the shortest path in unweighted graphs. DFS explores as deep as possible along each branch using a stack or recursion before backtracking."}},{"@type":"Question","name":"What is the time complexity of BFS?","acceptedAnswer":{"@type":"Answer","text":"BFS runs in O(V + E) time, where V is the number of vertices and E is the number of edges, because each vertex and edge is examined once. Its space complexity is O(V) for the queue and visited set."}}]}],"@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html#schema-1131239","isPartOf":{"@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/breadth-first-search-bfs-graph-example.html#webpage"}}]}
```
