Breadth-First Search (BFS): A Beginner's Guide.

Breadth-First Search (BFS): A Beginner's Guide.

What is BFS?

Breadth-First Search (BFS) is a graph traversal algorithm used to explore nodes and edges of a graph. It starts at a given source node and explores all its neighbors before moving on to the neighbors of those neighbors. BFS guarantees finding the shortest path in an unweighted graph, making it a powerful tool in graph theory.

Key Characteristics of BFS:

  1. Level-Order Traversal: Nodes are visited level by level.

  2. Queue-Based: BFS uses a queue to manage nodes to be visited.

  3. Shortest Path: BFS finds the shortest path in terms of edges in an unweighted graph.

  4. Breadth First: All neighbors are visited before moving deeper.

How Does BFS Work?

  1. Start from the source node and mark it as visited.

  2. Add the source node to a queue.

  3. While the queue is not empty:

  • Remove the front node from the queue.

  • Visit all its unvisited neighbors, mark them as visited, and add them to the queue.

Applications of BFS

  • Finding Shortest Paths: In unweighted graphs (e.g., road networks).

  • Web Crawling: Exploring hyperlinks level by level.

  • Social Networks: Finding friends of friends (degrees of separation).

  • Connected Components: Detecting connected parts in a graph.

  • Maze Solving: Exploring paths in a grid or matrix.

BFS Visualization

Imagine a graph as a series of connected nodes. BFS explores all nodes at the current "depth" before moving to the next depth level. Think of ripples spreading from a pebble dropped into water.

C Program for BFS

Here’s a simple implementation of BFS in C:

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

// Queue implementation

int queue[MAX], front = -1, rear = -1;

void enqueue(int node) {

if (rear == MAX - 1) {

printf("Queue Overflow\n");

return;

}

if (front == -1) front = 0;

queue[++rear] = node;

}

int dequeue() {

if (front == -1 || front > rear) {

printf("Queue Underflow\n");

return -1;

}

return queue[front++];

}

int isEmpty() {

return front == -1 || front > rear;

}

// BFS Function

void bfs(int graph[MAX][MAX], int start, int n) {

int visited[MAX] = {0};

enqueue(start);

visited[start] = 1;

printf("BFS Traversal: ");

while (!isEmpty()) {

int current = dequeue();

printf("%d ", current);

// Visit all adjacent nodes

for (int i = 0; i < n; i++) {

if (graph[current][i] == 1 && !visited[i]) {

enqueue(i);

visited[i] = 1;

}

}

}

printf("\n");

}

int main() {

int n, start;

int graph[MAX][MAX];

printf("Enter the number of nodes: ");

scanf("%d", &n);

printf("Enter the adjacency matrix:\n");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

scanf("%d", &graph[i][j]);

}

}

printf("Enter the starting node: ");

scanf("%d", &start);

bfs(graph, start, n);

return 0;

}

How It Works:

  1. Adjacency Matrix: The graph is represented as an adjacency matrix.

  2. Queue: A queue is used to track nodes to be visited.

  3. Visited Array: Keeps track of visited nodes to avoid reprocessing.

  4. Input: Users input the adjacency matrix and the starting node.

Why BFS is Important?

BFS is a cornerstone of graph algorithms due to its simplicity and versatility. Whether you're exploring social networks, solving puzzles, or analyzing connectivity, BFS is a go-to tool.