Tutorial Playlist

Data Structure Tutorial


Arrays in Data Structures: A Guide With Examples

Lesson - 1

All You Need to Know About Two-Dimensional Arrays

Lesson - 2

All You Need to Know About a Linked List in a Data Structure

Lesson - 3

The Complete Guide to Implement a Singly Linked List

Lesson - 4

The Ultimate Guide to Implement a Doubly Linked List

Lesson - 5

The Fundamentals for Understanding Circular Linked List

Lesson - 6

The Ultimate Guide To Understand The Differences Between Stack And Queue

Lesson - 7

Implementing Stacks in Data Structures

Lesson - 8

Your One-Stop Solution for Stack Implementation Using Array

Lesson - 9

Your One-Stop Solution for Queue Implementation Using Array

Lesson - 10

Your One-Stop Solution to Learn Depth-First Search(DFS) Algorithm From Scratch

Lesson - 11

Your One-Stop Solution for Stack Implementation Using Linked-List

Lesson - 12

The Definitive Guide to Understand Stack vs Heap Memory Allocation

Lesson - 13

All You Need to Know About Linear Search Algorithm

Lesson - 14

All You Need to Know About Breadth-First Search Algorithm

Lesson - 15

A One-Stop Solution for Using Binary Search Trees in Data Structure

Lesson - 16

The Best Tutorial to Understand Trees in Data Structure

Lesson - 17

A Complete Guide to Implement Binary Tree in Data Structure

Lesson - 18

A Holistic Look at Using AVL Trees in Data Structures

Lesson - 19

All You Need to Know About Tree Traversal in Data Structure

Lesson - 20

The Best Guide You’ll Ever Need to Understand B-Tree in Data Structure

Lesson - 21

The Best Guide You'll Ever Need to Understand Spanning Tree in Data Structure

Lesson - 22

The Best and Easiest Way to Understand an Algorithm

Lesson - 23

Your One-Stop Solution to Understand Shell Sort Algorithm

Lesson - 24

Your One-Stop Solution to Quick Sort Algorithm

Lesson - 25

The Most Useful Guide to Learn Selection Sort Algorithm

Lesson - 26

Everything You Need to Know About Radix Sort Algorithm

Lesson - 27

Everything You Need to Know About the Counting Sort Algorithm

Lesson - 28

Everything You Need to Know About the Merge Sort Algorithm

Lesson - 29

Insertion Sort Algorithm: One-Stop Solution That Will Help You Understand Insertion Sort

Lesson - 30

Everything You Need to Know About the Bubble Sort Algorithm

Lesson - 31

The Best Guide You’ll Ever Need to Understand Bucket Sort Algorithm

Lesson - 32

Your One-Stop Solution to Understand Recursive Algorithm in Programming

Lesson - 33

The Definitive Guide to Understanding Greedy Algorithm

Lesson - 34

Your One-Stop Solution to Understand Backtracking Algorithm

Lesson - 35

The Fundamentals of the Bellman-Ford Algorithm

Lesson - 36

Your One-Stop Solution for Graphs in Data Structures

Lesson - 37

The Best Guide to Understand and Implement Solutions for Tower of Hanoi Puzzle

Lesson - 38

A Simplified and Complete Guide to Learn Space and Time Complexity

Lesson - 39

All You Need to Know About the Knapsack Problem : Your Complete Guide

Lesson - 40

The Fibonacci Series: Mathematical and Programming Interpretation

Lesson - 41

The Holistic Look at Longest Common Subsequence Problem

Lesson - 42

The Best Article to Understand What Is Dynamic Programming

Lesson - 43

A Guide to Implement Longest Increasing Subsequence Using Dynamic Programming

Lesson - 44

A Holistic Guide to Learn Stop Solution Using Dynamic Programming

Lesson - 45

One Stop Solution to All the Dynamic Programming Problems

Lesson - 46

Understanding the Fundamentals of Binomial Distribution

Lesson - 47

Here’s All You Need to Know About Minimum Spanning Tree in Data Structures

Lesson - 48

Understanding the Difference Between Array and Linked List

Lesson - 49

The Best Article Out There to Understand the B+ Tree in Data Structure

Lesson - 50

A Comprehensive Look at Queue in Data Structure

Lesson - 51

Your One-Stop Solution to Understand Coin Change Problem

Lesson - 52

The Best Way to Understand the Matrix Chain Multiplication Problem

Lesson - 53

Your One-Stop Solution to Learn Floyd-Warshall Algorithm for Using Dynamic Programming

Lesson - 54

The Best Tutorial You'll Ever Need for Queue Implementation Using Linked List

Lesson - 55
All You Need to Know About Breadth-First Search Algorithm

The breadth-first search or BFS algorithm is used to search a tree or graph data structure for a node that meets a set of criteria. It begins at the root of the tree or graph and investigates all nodes at the current depth level before moving on to nodes at the next depth level. You can solve many problems in graph theory via the breadth-first search. For example, finding the shortest path between two vertices a and b is determined by the number of edges. In a flow network, the Ford–Fulkerson method is used to calculate the maximum flow and when a binary tree is serialized/deserialized instead of serialized in sorted order, the tree can be reconstructed quickly.

What Is Graph Traversal Algorithms in Data Structure?

Graph traversal is a search technique to find a vertex in a graph. In the search process, graph traversal is also used to determine the order in which it visits vertices. Without producing loops, a graph traversal finds the edges to be employed in the search process. That is, utilizing graph traversal, you can visit all the graph's vertices without going through a looping path.

There are two methods for traversing graphs, which are as follows:

  • Breadth-First Search or BFS Algorithm
  • Depth- First Search or DFS Algorithm

In this tutorial, you will learn the breadth-first search algorithm.

What Is the Breadth-First Search Algorithm?

Breadth-First Search Algorithm or BFS is the most widely utilized method.

BFS is a graph traversal approach in which you start at a source node and layer by layer through the graph, analyzing the nodes directly related to the source node. Then, in BFS traversal, you must move on to the next-level neighbor nodes.

According to the BFS, you must traverse the graph in a breadthwise direction:

  • To begin, move horizontally and visit all the current layer's nodes.
  • Continue to the next layer.


Breadth-First Search uses a queue data structure to store the node and mark it as "visited" until it marks all the neighboring vertices directly related to it. The queue operates on the First In First Out (FIFO) principle, so the node's neighbors will be viewed in the order in which it inserts them in the node, starting with the node that was inserted first.

Following the definition of breadth-first search, you will look at why we need a breadth-first search algorithm.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Why Do You Need Breadth-First Search Algorithm? 

There are several reasons why you should use the BFS Algorithm to traverse graph data structure. The following are some of the essential features that make the BFS algorithm necessary:

  • The BFS algorithm has a simple and reliable architecture.
  • The BFS algorithm helps evaluate nodes in a graph and determines the shortest path to traverse nodes.
  • The BFS algorithm can traverse a graph in the fewest number of iterations possible.
  • The iterations in the BFS algorithm are smooth, and there is no way for this method to get stuck in an infinite loop.
  • In comparison to other algorithms, the BFS algorithm's result has a high level of accuracy.

In this tutorial, next, you will look at the pseudocode for the breadth-first search algorithm.

Pseudocode Of Breadth-First Search Algorithm 

The breadth-first search algorithm's pseudocode is:

Bredth_First_Serach( G, A ) // G ie the graph and A is the source node

          Let q be the queue 

          q.enqueue( A ) // Inserting source node A to the queue

          Mark A node as visited.

          While ( q is not empty )

          B = q.dequeue( ) // Removing that vertex from the queue, which will be visited by its neighbour

Processing all the neighbors of B

For all neighbors of C of B

             If C is not visited, q. enqueue( C ) //Stores C in q to visit its neighbour

Mark C a visited

For a better understanding, you will look at an example of a breadth-first search algorithm later in this tutorial.

Example of Breadth-First Search Algorithm

In a tree-like structure, graph traversal requires the algorithm to visit, check, and update every single un-visited node. The sequence in which graph traversals visit the nodes on the graph categorizes them.

The BFS algorithm starts at the first starting node in a graph and travels it entirely. After traversing the first node successfully, it visits and marks the next non-traversed vertex in the graph.

Step 1: In the graph, every vertex or node is known. First, initialize a queue.


Step 2: In the graph, start from source node A and mark it as visited.


Step 3: Then you can observe B and E, which are unvisited nearby nodes from A. You have two nodes in this example, but here choose B, mark it as visited, and enqueue it alphabetically.


Step 4: Node E is the next unvisited neighboring node from A. You enqueue it after marking it as visited.


Step 5: A now has no unvisited nodes in its immediate vicinity. As a result, you dequeue and locate A.


Step 6: Node C is an unvisited neighboring node from B. You enqueue it after marking it as visited.


Step 7: Node D is an unvisited neighboring node from C. You enqueue it after marking it as visited.


Step 8: If all of D's adjacent nodes have already been visited, remove D from the queue.


Step 9: Similarly, all nodes near E, B, and C nodes have already been visited; therefore, you must remove them from the queue.


Step 10: Because the queue is now empty, the bfs traversal has ended.

In the next section of this tutorial, you will look at the breadth-first search algorithm's complexity.

Complexity Of Breadth-First Search Algorithm

The time complexity of the breadth-first search algorithm : The time complexity of the breadth-first search algorithm can be stated as O(|V|+|E|) because, in the worst case, it will explore every vertex and edge. The number of vertices in the graph is |V|, while the edges are |E|.

The space complexity of the breadth-first search algorithm : You can define the space complexity as O(|V|), where |V| is the number of vertices in the graph, and different data structures are needed to determine which vertices have already been added to the queue. This is also the space necessary for the graph, which varies depending on the graph representation used by the algorithm's implementation.

You will see some bfs algorithm applications now that you’ve grasped the complexity of the breadth-first search method.

Application Of Breadth-First Search Algorithm 

The breadth-first search algorithm has the following applications:

  • For Unweighted Graphs, You Must Use the Shortest Path and Minimum Spacing Tree.

The shortest path in an unweighted graph is the one with the fewest edges. You always reach a vertex from a given source using the fewest amount of edges when utilizing breadth-first. In unweighted graphs, any spanning tree is the Minimum Spanning Tree, and you can identify a spanning tree using either depth or breadth-first traversal.

  • Peer to Peer Network

Breadth-First Search is used to discover all neighbor nodes in peer-to-peer networks like BitTorrent.

  • Crawlers in Search Engine

Crawlers create indexes based on breadth-first. The goal is to start at the original page and follow all of the links there, then repeat. Crawlers can also employ Depth First Traversal. However, the benefit of breadth-first traversal is that the depth or layers of the created tree can be limited.

  • Social Networking Websites

You can use a breadth-first search to identify persons within a certain distance 'd' from a person in social networks up to 'd's levels.

  • GPS Navigation System

To find all nearby locations, utilize the breadth-first search method.

  • Broadcasting Network 

A broadcast packet in a network uses breadth-first search to reach all nodes.

  • Garbage Collection 

Cheney's technique uses the breadth-first search for duplicating garbage collection. Because of the better locality of reference, breadth-first search is favored over the Depth First Search algorithm.

  • Cycle Detection in Graph

Cycle detection in undirected graphs can be done using either Breadth-First Search or Depth First Search. BFS can also be used to find cycles in a directed graph.

  • Identifying Routes

To see if there is a path between two vertices, you can use either Breadth-First or Depth First Traversal.

  • Finding All Nodes Within One Connected Component

To locate all nodes reachable from a particular node, you can use either Breadth-First or Depth First Traversal.

Finally, in this tutorial, you will look at the code for the breadth-first search (bfs) algorithm.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Code Implementation of Breadth-First Search Algorithm 

Breadth-First Search Algorithm Code Implementation:




int twodimarray[10][10],queue[10],visited[10],n,i,j,front=0,rear=-1;

void breadthfirstsearch(int vertex) // breadth first search function


for (i=1;i<=n;i++)

if(twodimarray[vertex][i] && !visited[i])








int main() {

int x;

printf("\n Enter the number of vertices:");


for (i=1;i<=n;i++) {




printf("\n Enter graph value in form of matrix:\n");

for (i=1;i<=n;i++)

for (j=1;j<=n;j++)


printf("\n Enter the source node:");



printf("\n The nodes which are reachable are:\n");

for (i=1;i<=n;i++)




printf("\n Breadth first search is not possible");




Next Steps

The depth-first search method is your next topic, and you will go through the details of the dfs algorithm.

Simplilearn's Software development course will prove to be precisely suitable for you if you're looking for a more comprehensive study that goes beyond Data Structure and covers the most in-demand programming languages and abilities needed today. Now is the time to start exploring.

Do you have any questions about this tutorial on the breadth-first search algorithm? If you have any, please leave them in the comments section at the bottom of this page. Our experts will get back to you as soon as possible!

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.