Graph Theory - Breadth-First Search



Breadth-First Search (BFS)

Breadth-First Search (BFS) is a graph traversal algorithm used to systematically explore nodes and edges in a graph. It starts at a selected node (often called the 'root') and explores all neighboring nodes at the current depth level before moving on to nodes at the next depth level.

It is particularly useful in scenarios such as finding the shortest path in an unweighted graph, determining the shortest distance between nodes, and exploring the reachability of nodes in a graph.

BFS uses a queue data structure to maintain the order of exploration. Unlike Depth-First Search (DFS), which dives deep into a path before backtracking, BFS explores all nodes level by level.

BFS Algorithm

The BFS algorithm follows these basic steps −

  • Initialize a queue and mark the starting node as visited.
  • While the queue is not empty, dequeue a node from the queue.
  • Visit all unvisited neighboring nodes of the current node, mark them as visited, and enqueue them.
  • Repeat this process until all reachable nodes are visited.

Let us look at an example to better understand BFS. Consider the following graph with nodes labeled A, B, C, D, and E −

BFS Graph

We will perform BFS starting from node A. The traversal proceeds as follows −

  • Start at node A. Mark it as visited and enqueue it.
  • Dequeue A, visit its neighbors (B, C), mark them as visited, and enqueue them.
  • Dequeue B, visit its neighbor (D), mark it as visited, and enqueue it.
  • Dequeue C, visit its neighbor (E), mark it as visited, and enqueue it.
  • Dequeue D and E. No further unvisited neighbors are found.

Thus, the BFS traversal order is A B C D E.

BFS on an Undirected Graph

In an undirected graph, BFS explores all nodes level by level starting from the chosen root node. It uses a queue to track nodes to visit next, ensuring all neighbors are visited before moving to the next level. This helps in finding the shortest path and checking connectivity between nodes.

Let us take a concrete example of BFS traversal on an undirected graph −

Undirected BFS

The graph is as follows −

  • A is connected to B and C.
  • B is connected to A, D, and E.
  • C is connected to A and F.
  • D is connected to B.
  • E is connected to B.
  • F is connected to C.

Starting BFS from node A:

  • Visit A, then its neighbors B and C.
  • Queue: B, C.
  • Visit B, then its neighbors D and E.
  • Queue: C, D, E.
  • Visit C, then its neighbor F.
  • Queue: D, E, F.
  • Visit D and E. No further unvisited neighbors.
  • Visit F. No further unvisited neighbors.

Final BFS traversal order: A B C D E F.

BFS on a Directed Graph

In directed graphs, BFS works similarly but respects the direction of edges. In directed graphs, BFS explores only the neighbors that are directly reachable from a node, following the directions of the edges.

Consider the following directed graph −

Directed BFS

In this graph, we perform BFS starting from node A −

  • Start at node A, visit its neighbors B and C.
  • Queue: B, C.
  • Visit B, then its neighbor D.
  • Queue: C, D.
  • Visit C, then its neighbor E.
  • Queue: D, E.
  • Visit D and E. No further unvisited neighbors.

Final BFS traversal order: A B C D E.

Applications of BFS

BFS is used in various real-world applications, such as −

  • Shortest Path in Unweighted Graphs: BFS is often used to find the shortest path between two nodes in an unweighted graph. Since BFS explores level by level, the first time it reaches a node, it does so via the shortest path.
  • Connected Components: BFS can help identify connected components in an undirected graph. By performing BFS from an unvisited node and marking all reachable nodes, you can determine the entire connected component.
  • Level Order Traversal in Trees: BFS can be used to traverse trees in level order, ensuring that all nodes at each level are visited before moving to the next level.
  • Network Broadcasting: In networking, BFS can be used to model the process of broadcasting messages from a central node to all other nodes in a network.

BFS in Finding the Shortest Path

One of the most important applications of BFS is finding the shortest path in an unweighted graph. Since BFS explores all nodes at a given depth before moving on to the next level, the first time it reaches a node, it does so via the shortest path.

For example, given an unweighted graph and two nodes, A and E, BFS can be used to determine the shortest path from A to E. The algorithm proceeds level by level, ensuring that the first time it reaches node E, it does so via the minimum number of edges.

Here is an example of finding the shortest path from node A to node E in an unweighted graph −

BFS Shortest Path

The shortest path from A to E is A B E, with two edges traversed. BFS ensures that this is the shortest path because it explores the graph level by level, visiting nodes closest to the starting point first.

Complexity of BFS

The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges in the graph. This is because BFS processes each vertex and edge exactly once.

The space complexity is O(V), as BFS requires storage for the queue and a visited list, both of which require space proportional to the number of vertices in the graph.

BFS performs efficiently on sparse and dense graphs, making it suitable for various graph-related problems.

BFS with Edge Weights

While BFS is not suitable for graphs with weighted edges when looking for the shortest path, it can still be used to find the shortest path in graphs where all edges have equal weight. In such cases, BFS guarantees finding the shortest path because it explores nodes level by level.

For weighted graphs, algorithms like Dijkstra's algorithm are more appropriate as they account for varying edge weights.

Advertisements