Breadth First Search (BFS) Program in C

In this tutorial we will discuss about Breadth First Search or BFS program in C with algorithm and an example. Before jumping to actual coding lets discuss something about Graph and BFS.

Also Read: Depth First Search (DFS) Traversal of a Graph [Algorithm and Program]

A Graph G = (V, E) is a collection of sets V and E where V is a collection of vertices and E is a collection of edges.

A Graph can be of two types:
1. Directed Graph
2. Undirected Graph

In data structures, there is a popular term known as ‘Traversal’. It is the process of systematically visiting or examining (may be to update the Graph nodes) each node in a tree data structure, exactly once.

There are two most common methods to traverse a Graph:
1. Breadth First Search
2. Depth First Search

In this tutorial, we are going to focus on Breadth First Search technique.

In this technique, we first visit the vertex and then visit all the vertices adjacent to the starting vertex i.e., 0. Next, we pick the adjacent vertices one after another and visit their adjacent vertices and this process goes on and on until we reach the last vertex.


Breadth First Search (BFS) Example

Consider below Graph as an example.


Breadth First Search (BFS) Program in C 2


The vertex 0 is the starting vertex in our case. We start our traversal from the vertex 0. Then we will visit all vertices adjacent to vertex 0 i.e., 1, 4, 3. Here, we can visit these three vertices in any order. Suppose we visit the vertices in order 1,3,4.

The traversal would be: 0 1 3 4

Now, we shall visit all the vertices adjacent to 1, then all the vertices adjacent to 3 and then all the vertices adjacent to 4. So first we shall visit 2 (since it is adjacent to 1), then 6 (since it is adjacent to 3) and 5, 7 (since these are adjacent to 4).

Note: Vertex 4 is adjacent to vertices 1 and 3, but it has already been visited so we’ve ignored it.

The traversal would be: 0 1 3 4 2 6 5 7

Now, we shall visit all the vertices adjacent to 2, 6, 5, and 7 one by one. We can see that vertex 5 is adjacent to vertex 2. Since, it has already been traversed upon before, we have don’t need to traverse through it again and move on to the next vertex. Now, the vertices 4 and 7 are adjacent to the vertex 6. Since, they have been traversed upon before, we will not traverse on them again. Vertex 5 does not have any adjacent vertices. Vertices 5 and 8 are adjacent to vertex 7. Since, vertex 5 has been traversed upon before, we will not traverse it again. However, vertex 8 has not yet been visited. So we will traverse on vertex 8.

The traversal would be: 0 1 3 4 2 6 5 7 8

Now, we need to visit vertices adjacent to vertex 8. However, there is no vertex adjacent to vertex 8 and hence we will have to stop the traversal here.

Note: There’s no unique traversal and it can be different based on the order of the successors.

We shall look at a BFS program in C for directed Graph using a Queue. This program reaches only those vertices that are reachable from the start vertex.


Breadth First Search (BFS) Program in C




Breadth First Search (BFS) Program in C 1
If you find anything incorrect or have any doubts regarding above Breadth First Search (BFS) program in C then comment below.

17 thoughts on “Breadth First Search (BFS) Program in C

  1. Akhilesh Kumar

    this code may not work in case of disjoint graphs

    1. Kaustubh

      this code is for bfs. what you are saying is the code for bf traversal. both are different.

  2. Shithi Maitra

    This was of great help. But I have two questions.
    1. In the input, you have excluded two connections : 1 4 and 3 4 ; while I ran this code including these edged, it rendered the same output. What is actually happening here?
    2. Do you have a Dijkstra code written in the same fashion? If so, plz provide me.
    All the best. 🙂

  3. sandesh

    can i input edges as character ex: like a,b .any changes to make in this program?

  4. srinadh

    why do we need Waiting state? Isnt Initial and Visited are enough? We dont seem to check for Waiting state anywhere.

  5. mahendran

    Neeraj mishra jii the way in which you program astonishes me, you are very much talented

  6. Albert Einstein

    Thanks for the stuff…
    very good code..
    loved it…

  7. Mayur Gudi

    According to me correct answer should be
    0 1 2 5 4 6 3 7 8
    Check it out …

    1. Kiran Poojary

      yes your answer also correct,our goal is to visit every node of the graph.

  8. Suman

    Thanks for the simply illustrated BFS program.
    I am new to data structures. I am beginner and have little knowledge on Linked Lists, Circular LLs, Double LLs.
    I was in the assumption to create a node with many childs say(similar to Linked List)
    struct GraphNode
    int data;
    struct GraphNode *p1;
    struct GraphNode *p2;
    struct GraphNode *p3;
    And I am trying to add Nodes one by one to create a graph, and then traverse through all the nodes of the graph(in BFS way).

    Anyways Thanks again for simple program.


Leave a Reply

Your email address will not be published. Required fields are marked *