VOOZH about

URL: https://www.geeksforgeeks.org/dsa/breadth-first-search-or-bfs-for-a-graph/

⇱ Breadth First Search or BFS for a Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Breadth First Search or BFS for a Graph

Last Updated : 16 Jan, 2026

Breadth First Search (BFS) is a graph traversal algorithm that starts from a source node and explores the graph level by level. First, it visits all nodes directly adjacent to the source. Then, it moves on to visit the adjacent nodes of those nodes, and this process continues until all reachable nodes are visited.

  • BFS is different from DFS in a way that closest vertices are visited before others. We mainly traverse vertices level by level.
  • Popular graph algorithms like Dijkstra's shortest path, Kahn's Algorithm, and Prim's algorithm are based on BFS.
  • BFS itself can be used to detect cycle in a directed and undirected graph, find shortest path in an unweighted graph and many more problems.

Examples:

Input: adj[][] = [[1, 2], [0, 2], [0, 1, 3, 4], [2], [2]]

👁 frame_3148

Output: [0, 1, 2, 3, 4]
Explanation: Starting from 0, the BFS traversal proceeds as follows:
Visit 0 - Print: 0
Visit 1 (neighbor of 0) - Print: 1
Visit 2 (next neighbor of 0) - Print: 2
Visit 3 (first neighbor of 2 that hasn't been visited yet) - Print: 3
Visit 4 (next neighbor of 2) - Print: 4

Input: adj[][] = [[2, 3], [2], [0, 1], [0], [5], [4]]

👁 420046859

Output: [0, 2, 3, 1, 4, 5]
Explanation: Starting from 0, the BFS traversal proceeds as follows:
Visit 0 - Print: 0
Visit 2 (first neighbor of 0) - Print: 2
Visit 3 (next neighbor of 0) - Print: 3
Visit 1 (neighbor of 2 that hasn't been visited yet) - Print: 1

Start another BFS traversal with source as 4:

Visit 4 - Print: 4
Visit 5 (neighbor of 4) - Print: 5

BFS from a Given Source in an Undirected Graph:

The algorithm starts from a given source vertex and explores all vertices reachable from that source, visiting nodes in increasing order of their distance from the source, level by level using a queue. Since graphs may contain cycles, a vertex could be visited multiple times. To prevent revisiting a vertex, a visited array is used.

Let us understand the working of Breadth First Search with the help of the following Illustration:


Output
0 1 2 3 4 

Time Complexity: O(V + E), BFS explores all the vertices and edges in the graph. It visits every vertex and edge only once.
Auxiliary Space: O(V), Using a queue to keep track of the vertices that need to be visited.

BFS of a Disconnected Undirected Graph:

In a disconnected graph, some vertices may not be reachable from a single source. To ensure all vertices are visited in BFS traversal, we iterate through each vertex, and if any vertex is unvisited, we perform a BFS starting from that vertex being the source. This way, BFS explores every connected component of the graph.


Output
0 2 3 1 4 5 

Time Complexity: O(V + E), The for loop ensures BFS starts from every unvisited vertex to cover all components, but the visited array ensures each vertex and edge is processed only once, keeping the total time complexity to be linear.

Auxiliary Space: O(V), using a queue to keep track of the vertices that need to be visited.

Applications of BFS in Graphs

BFS has various applications in graph theory and computer science, including:

  • Shortest Path Finding
  • Cycle Detection
  • Connected Components
  • Network Routing

To read more about applications of BFS, read this article.

Comment
Article Tags:
Article Tags: