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.
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
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS: \n");
void BFS(int v)
state[v] = waiting;
v = delete_queue( );
state[v] = visited;
for(i=0; i<n; i++)
if(adj[v][i] == 1 && state[i] == initial)
state[i] = waiting;
void insert_queue(int vertex)
if(rear == MAX-1)
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
if(front == -1 || front > rear)
if(front == -1 || front > rear)
delete_item = queue[front];
front = front+1;
printf("Enter number of vertices : ");
max_edge = n*(n-1);
for(count=1; count<=max_edge; count++)
printf("Enter edge %d( -1 -1 to quit ) : ",count);
if((origin == -1) && (destin == -1))
if(origin>=n || destin>=n || origin<0 || destin<0)
adj[origin][destin] = 1;
If you find anything incorrect or have any doubts regarding above Breadth First Search (BFS) program in C then comment below.
20 thoughts on “Breadth First Search (BFS) Program in C”
this code may not work in case of disjoint graphs
this code is for bfs. what you are saying is the code for bf traversal. both are different.
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. 🙂
Can I use this program for undirected graph also
is it possible to use bfs and dfs on undirected graphs?
How to use it for undirected graphs?
can i input edges as character ex: like a,b .any changes to make in this program?
why do we need Waiting state? Isnt Initial and Visited are enough? We dont seem to check for Waiting state anywhere.
Neeraj mishra jii the way in which you program astonishes me, you are very much talented
Thanks for the stuff…
very good code..
According to me correct answer should be
0 1 2 5 4 6 3 7 8
Check it out …
yes your answer also correct,our goal is to visit every node of the graph.
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 *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.
explain me the full code
Kindly explain d output
What would the code look like for c++ ?
then use it for joint graphs
Hello actually i am a beginner , i am in my second year and i am facing problem to create bfs using linklist . I try to write the code in linklist but don’t know why it’s not working plz help.
typedef struct node
struct node *next;
struct node *front=NULL,*rear=NULL,*q;
//heads of linked list
//create adjacency list
//insert an edge (vi,vj) in te adjacency list
//initialised visited to 0
if(front==NULL & rear== NULL)
struct node *temp;
if(front==NULL && rear==NULL)
printf(“LIST IS EMPTY”);
void BFS (int startvertex)
int current = dequeue();
struct node *temp=G[current];
printf(“Enter number of vertices:”);
//initialise G with a null
//read edges and insert them in G
printf("Enter number of edges:");
//insert the node in the linked list number vi
//go to end of the linked list