Javatpoint Logo
Javatpoint Logo

BFS Code in C++

What is BFS?

Breadth-First Search (BFS) is an algorithm for traversing or searching a graph. It starts at a given vertex and explores all the neighboring vertices before moving on to the next level of vertices. BFS is useful for finding the shortest path between two vertices in a graph or for finding the connected components of a graph. It is also used for topological sorting, a linear ordering of the vertices in a directed acyclic graph (DAG) such that for every edge UV from vertex u to vertex v, u comes before v in the ordering.

In this tutorial, we will learn how to implement BFS in C++. We will start by understanding the concept of BFS and its time complexity. Then, we will see how to represent a graph in C++ using an adjacency list and how to traverse the graph using the BFS algorithm.

Concept of BFS

BFS is an algorithm for traversing or searching a graph. It starts at a given vertex and explores all the neighboring vertices before moving on to the next level of vertices. The algorithm maintains a queue of vertices to be visited, starting with the source vertex. At each step, it removes a vertex from the queue and adds its unvisited neighbors to the queue. This process continues until no more vertices are in the queue to be visited.

The BFS algorithm has a time complexity of O(V+E), where V is the number of vertices and E is the number of edges in the graph. This makes it more suitable for dense graphs, where the number of edges is close to the number of vertices.

Representing a Graph in C++

There are several ways to represent a graph in C++. One common representation is to use an adjacency list, which is a list of all the vertices that are adjacent to a given vertex. For example, consider the following graph with 5 vertices:

BFS Code in C++

We can represent this graph using an adjacency list as follows:

Here, adj_list[i] is a vector containing the neighbors of vertex i.

Implementing BFS in C++

Output:

BFS Code in C++

Explanation:

In this code, we first define the bfs() function, which takes an adjacency list adj_list, a starting vertex start, and an optional target vertex target. The bfs() function initializes a boolean array visited to track the visited vertices and a vector order to store the BFS traversal order. It also creates a queue q to hold the vertices to be visited.

The function then adds the starting vertex to the queue and marks it as visited. It then enters a loop that continues until the queue is empty. Each iteration of the loop removes the next vertex from the queue and adds it to the traversal order. It then adds all its unvisited neighbors to the queue and marks them as visited.

Finally, if a target vertex is specified and it is not found, the function returns an empty vector. Otherwise, it returns the traversal order.

In the main() function, we create an adjacency list for a graph with 5 vertices. We then perform a BFS search starting from vertex 0 and store the traversal order in the order vector. Finally, we print the traversal order to the console.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA