# BFS (Breadth First Search)

BFS, or Breadth-First Search, is a graph traversal algorithm utilized for exploring or searching a graph or tree. It operates by starting at a given node, often referred to as the root node, and then systematically exploring all the neighboring nodes at the current depth level before moving on to the nodes at the next depth level.

During BFS, the algorithm follows a breadth-first approach, which means it visits all the nodes at a particular level before progressing to the nodes at the next level. This ensures the exploration of nodes closer to the root node before deeper nodes. The BFS algorithm’s origins can be traced back to the field of graph theory, which has contributions from various mathematicians and computer scientists throughout history.

## General Working of BFS

The general working of BFS can be summarized as follows:

1. Begin by initializing a queue data structure and add the starting node to the queue.
2. Mark the starting node as visited to keep track of the visited nodes.
3. Repeat the following steps until the queue becomes empty:
• Remove a node from the front of the queue (dequeue operation).
• Perform some operation on the dequeued node, such as visiting or processing it.
• Add all its unvisited neighbors to the queue (enqueue operation) and mark them as visited.
4. Continue this process until either all nodes have been visited or a specific condition or goal is achieved.

By following these steps, BFS ensures the processing of nodes at the same depth level before moving on to deeper nodes. This allows the algorithm to traverse the graph or tree in a breadth-first manner, exploring nearby nodes before going further

## Example

Consider the following graph for example:

### Table for the graph

Here’s the step-by-step BFS traversal of the given graph represented in a tabular form:

### Explanation

In this table, the column “Nodes Enqueued” represents the nodes added to the queue at each step, while the column “Nodes Dequeued” represents the nodes removed from the front of the queue at each step.

Let’s apply BFS on the given graph using the table above as a reference:

1. We start at node A and enqueue it.
2. In the next step, we dequeue A and enqueue its neighbors B and C.
3. We dequeue B and enqueue its neighbors D and E.
4. After that, we dequeue C and enqueue its neighbors F and G.
5. Next, we dequeue D, followed by dequeueing E.
6. We then dequeue F and enqueue G.
7. Finally, we dequeue G, and as there are no more nodes in the queue, the traversal ends.

## Time and Space Complexity of BFS

The time complexity of BFS (Breadth-First Search) is O(V + E), where V represents the number of vertices (nodes) in the graph or tree, and E represents the number of edges.

In terms of big O notation:

• O(V) represents the time complexity of visiting each vertex once.
• O(E) represents the time complexity of visiting each edge once.

Since BFS guarantees visiting each vertex and each edge once, the overall time complexity is determined by the sum of visiting all vertices and edges.

Additionally, the space complexity of BFS is O(V), as it requires additional space to store the visited nodes and the queue used for traversal. This space complexity arises due to the need to keep track of the visited nodes and maintain the queue during the traversal process.

In conclusion, BFS has a time complexity of O(V + E) and a space complexity of O(V). This makes it an efficient algorithm for exploring or searching graphs or trees, especially in scenarios where the number of edges is proportional to the number of vertices.

### Is BFS better than DFS?

Whether Breadth-First Search (BFS) is better than Depth-First Search (DFS) depends on the specific problem or application at hand. Both BFS and DFS have their strengths and weaknesses.

BFS guarantees that it will find the shortest path between two nodes in an unweighted graph. It explores all nodes at the same depth level before moving on to deeper levels, which can be advantageous in certain scenarios. BFS is particularly useful for finding the shortest path, determining connectivity, or exploring a graph iteratively.

On the other hand, DFS explores a path as deeply as possible before backtracking. It can be more memory-efficient than BFS as it only needs to store information for the current path. DFS is often used for tasks such as detecting cycles, solving maze problems, or traversing through a tree or graph in a recursive manner.

In summary, neither BFS nor DFS is universally better than the other. The choice between them depends on the specific requirements of the problem, the structure of the graph or tree, and the desired outcomes. It’s important to understand the characteristics and limitations of each algorithm to determine which one is more suitable for a particular situation.

## Conclusion

In conclusion, Breadth-First Search (BFS) is a highly effective and widely utilized graph traversal algorithm. By employing a breadth-first exploration strategy, BFS efficiently explores or searches through a graph or tree. This algorithm guarantees that it visits nodes at the current depth level before moving on to deeper levels. BFS, with a time complexity of O(V + E) and a space complexity of O(V), ensures that it is well-suited for scenarios where the number of edges is proportional to the number of vertices. Moreover, BFS finds its application in various domains, including finding the shortest path, checking for connectivity, and traversing graphs. Overall, BFS’s ability to explore nodes in a breadth-first manner, along with its favorable performance characteristics, solidifies its significance in computer science and graph theory.