BFS (Breadth-First Search)

Breadth-First Search (BFS) is a graph traversal algorithm that explores all the vertices of a graph level by level, starting from a given source vertex. BFS is often used in unweighted graphs to find the shortest path between two nodes, solve puzzles like mazes, and perform other graph-based analyses.

BFS Algorithm Overview

BFS uses a queue data structure to track  vertices to be explored. Starting from the source node, it explores all of its neighbors, enqueues them, and continues with the next vertex in the queue. The process ensures that the nodes closest to the source are explored first.

Key Features of BFS:

Queue-based: BFS explores nodes in the order they are discovered using a FIFO (First In, First Out) queue.

Level-wise Exploration: Nodes are explored in increasing order of their distance from the source node.

Shortest Path: In unweighted graphs, BFS guarantees the shortest path between the source and any reachable node.


Time Complexity:

The time complexity of BFS is O(V + E), where V is the number of vertices and E is the number of edges. This is because, in the worst case, BFS visits every vertex and explores every edge.

Space Complexity:

BFS requires O(V) space, as it must store all vertices in the queue in the worst-case scenario (e.g., if the graph is a complete binary tree).

Example of BFS Code (Python):

from collections import deque

def bfs(graph, start):
    visited = set()  # Set to track visited nodes
    queue = deque([start])  # Queue for BFS
   
    visited.add(start)
    while queue:
        node = queue.popleft()
        print(node, end=” “)  # Process the node
        for neighbor in graph[node]:
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append(neighbor)

# Sample Graph (adjacency list)
graph = {
    ‘A’: [‘B’, ‘C’],
    ‘B’: [‘A’, ‘D’, ‘E’],
    ‘C’: [‘A’, ‘F’],
    ‘D’: [‘B’],
    ‘E’: [‘B’, ‘F’],
    ‘F’: [‘C’, ‘E’]
}

bfs(graph, ‘A’)

BFS Use Cases:

Shortest Path in Unweighted Graphs: BFS is ideal for finding the shortest path when edge weights are equal or non-existent.

Level-wise Exploration: BFS is often used in scenarios where level-wise traversal of data is required, such as in networking, scheduling, and more.

Connected Components: BFS can help identify all the connected components in a graph.


BFS vs DFS:

Unlike Depth-First Search (DFS), which uses a stack or recursion for a deep exploration of the graph, BFS explores the graph more broadly before going deeper. This makes BFS ideal for problems like finding the shortest path and determining the number of levels in a tree-like structure.

Conclusion:

BFS is a fundamental algorithm that plays a crucial role in graph theory and problem-solving, offering an efficient way to explore nodes in a graph. Its utility spans across real-world applications such as networking, artificial intelligence, and more complex systems.

The article above is rendered by integrating outputs of 1 HUMAN AGENT & 3 AI AGENTS, an amalgamation of HGI and AI to serve technology education globally.

(Article By : Himanshu N)