Breadth-First Search (BFS) in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Level by Level

Breadth-first search starts at a source, visits all neighbors, then their neighbors, and so on, using a queue. In an unweighted graph the first time BFS reaches a vertex is along a shortest path in edges, so storing the distance when enqueuing gives shortest hop counts. With an adjacency matrix BFS runs in O(V^2); with lists, O(V + E).

Remember
dist[src] = 0; enqueue(src);
while (queue not empty) {
    u = dequeue();
    for each neighbor v of u:
        if (dist[v] < 0) { dist[v] = dist[u] + 1; enqueue(v); }
}

Cheat Sheet

Keep these facts handy while you work with Breadth-First Search (BFS) in C in C.

TopicDetail
StructureQueue
Visiteddist[v] >= 0
GivesShortest path in edges
TimeO(V + E) with lists

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Depth-First Search (DFS) in C.

Example 1 — BFS from vertex 0

Edges: 0-1, 0-3, 1-4, 2-3, 2-5.

C
#include <stdio.h>
#define V 6

int main(void) {
    int adj[V][V] = {{0}};
    int edges[][2] = {{0, 1}, {0, 3}, {1, 4}, {2, 3}, {2, 5}};
    for (int i = 0; i < 5; i++) {
        adj[edges[i][0]][edges[i][1]] = 1;
        adj[edges[i][1]][edges[i][0]] = 1;
    }

    int dist[V], q[V], head = 0, tail = 0;
    for (int i = 0; i < V; i++) dist[i] = -1;

    dist[0] = 0;
    q[tail++] = 0;
    printf("BFS order:");
    while (head < tail) {
        int u = q[head++];
        printf(" %d", u);
        for (int v = 0; v < V; v++) {
            if (adj[u][v] && dist[v] < 0) {
                dist[v] = dist[u] + 1;
                q[tail++] = v;
            }
        }
    }

    printf("\ndistances:");
    for (int i = 0; i < V; i++) printf(" %d", dist[i]);
    printf("\n");
    return 0;
}

How It Works

  1. From 0 the neighbors 1 and 3 are queued, in index order.
  2. Vertex 1 adds 4; vertex 3 adds 2; vertex 2 adds 5.
  3. Distances are listed by vertex number: vertex 5 is three edges away.

Edge Cases & Pitfalls

Check these before relying on Breadth-First Search (BFS) in C in real programs.

mark

Marking on dequeue instead of enqueue

This can enqueue a vertex many times. Mark when enqueuing.

size

Queue size

Each vertex enters the queue once, so V entries are enough.

Key Takeaways

  • Queue and visited.
  • Mark when enqueuing.
  • Shortest paths in unweighted graphs.

One line: Breadth-first search starts at a source, visits all neighbors, then their neighbors, and so on, using a queue.

Frequently Asked Questions

A graph traversal that visits vertices in order of increasing distance from the source, using a queue.
Yes, in unweighted graphs it finds paths with the fewest edges.
Continue with "Depth-First Search (DFS) in C" next. Explore a graph recursively and count connected components. The C Graphs hub lists every topic in order.

Next: Depth-First Search (DFS) in C

Explore a graph recursively and count connected components.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful