BFS using vectors & queue as per the algorithm of CLRS in C Program?

In graph theory, BFS (Breadth-First Search) is a fundamental traversal algorithm described in CLRS (Introduction to Algorithms). It explores vertices level by level using a queue data structure and maintains vertex states using colors.

Syntax

void BFS(int graph[][MAX_VERTICES], int vertices, int source);

Algorithm

The CLRS BFS algorithm uses the following pseudocode −

BFS(G, s) -
begin
   for each vertex u in G.V - {s}, do
      u.color := white
      u.d := infinity
      u.p := NIL
   done
   s.color := gray
   s.d := 0
   s.p := NIL
   Q := NULL
   insert s into Q
   while Q is not null, do
      u = delete from Q
      for each v adjacent to u, do
         if v.color = white
            v.color := gray
            v.d := u.d + 1
            v.p := u
            insert v into Q
         end if
      done
      u.color = black
   done
end

Example: BFS Implementation in C

Here's a complete C implementation of BFS using arrays to simulate vectors and a simple queue −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_VERTICES 100
#define MAX_QUEUE 100

// Colors: 0=white, 1=gray, 2=black
int color[MAX_VERTICES];
int distance[MAX_VERTICES];
int parent[MAX_VERTICES];
int graph[MAX_VERTICES][MAX_VERTICES];
int vertices;

// Simple queue implementation
int queue[MAX_QUEUE];
int front = 0, rear = 0;

void enqueue(int item) {
    queue[rear++] = item;
}

int dequeue() {
    return queue[front++];
}

int isEmpty() {
    return front == rear;
}

void addEdge(int u, int v) {
    graph[u][v] = 1;
    graph[v][u] = 1;  // For undirected graph
}

void BFS(int source) {
    // Initialize all vertices
    for (int i = 0; i < vertices; i++) {
        color[i] = 0;        // white
        distance[i] = -1;    // infinity
        parent[i] = -1;      // NIL
    }
    
    // Initialize source vertex
    color[source] = 1;       // gray
    distance[source] = 0;
    parent[source] = -1;
    
    enqueue(source);
    
    printf("BFS Traversal: ");
    
    while (!isEmpty()) {
        int u = dequeue();
        printf("%d ", u);
        
        // Check all adjacent vertices
        for (int v = 0; v < vertices; v++) {
            if (graph[u][v] == 1 && color[v] == 0) {  // white
                color[v] = 1;              // gray
                distance[v] = distance[u] + 1;
                parent[v] = u;
                enqueue(v);
            }
        }
        
        color[u] = 2;  // black (processed)
    }
    printf("
"); } void printDistances(int source) { printf("Distances from source %d:
", source); for (int i = 0; i < vertices; i++) { printf("Vertex %d: %d
", i, distance[i]); } } int main() { vertices = 7; // Initialize graph for (int i = 0; i < vertices; i++) { for (int j = 0; j < vertices; j++) { graph[i][j] = 0; } } // Add edges addEdge(0, 1); addEdge(0, 2); addEdge(1, 3); addEdge(1, 4); addEdge(2, 5); addEdge(2, 6); BFS(0); printDistances(0); return 0; }
BFS Traversal: 0 1 2 3 4 5 6 
Distances from source 0:
Vertex 0: 0
Vertex 1: 1
Vertex 2: 1
Vertex 3: 2
Vertex 4: 2
Vertex 5: 2
Vertex 6: 2

Key Points

  • Color Coding: White (0) = unvisited, Gray (1) = discovered, Black (2) = processed
  • Time Complexity: O(V + E) where V is vertices and E is edges
  • Space Complexity: O(V) for the queue and auxiliary arrays
  • Applications: Shortest path in unweighted graphs, level-order traversal

Conclusion

BFS systematically explores graph vertices level by level using a queue. The CLRS algorithm ensures all reachable vertices are visited exactly once, making it optimal for shortest path problems in unweighted graphs.

Updated on: 2026-03-15T11:48:36+05:30

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements