C Codes · General Discussions

BFS – C code

Github Link.

#include
#include 

/*
Funtion Name: bfs
Input Params: Graph input as matrix, number of vertices and source vertex
Return Type:  void
Description:  performs bfs traversal on the given graph
*/
void bfs(int m[10][10], int v, int source)
{
    /// A queue will be used during the traversal
    int queue[20];
    int front = 0;
    int rear = 0;

    /// Temporary variables
    int u;
    int i;

    /// Array to maintain the visited vertices
    int visited[10];

    /// Set all the vertices to not visited
    for (i= 0; i < v; i ++)
        visited[i] = 0;

    /// Initilaize the queue with source and mark it visited
    queue[rear] = source;
    visited = 1;

    printf("The BFS Traversal is... \n");

    /// Until the queue is empty
    while (front <= rear)
    {
        /// Pick the vertex from queue front
        /// This is also when we mark the traversal and print it
        u = queue[front];
        printf("%d\t", u);
        front = front + 1;

        /// All the vertices that are reachable from considered vertex
        /// and not yet visited,
        /// Mark them as visited and Add them to queue
        for(i=0;i<<v;i++)
        {
            if(m[u][i] == 1 && visited[i] == 0)
            {
                visited[i] = 1;
                rear = rear + 1;
                queue[rear] = i;
            }
        }
    }
}

int main()
{
    /// Variable to hold the number of vertices
    int v = 5;

    /// Variable to hold the graph as matrix
    int m[10][10] =
                    {
                        {0, 1, 1, 0, 0},
                        {1, 0, 0, 1, 1},
                        {1, 0, 0, 0, 1},
                        {0, 1, 0, 0, 0},
                        {0, 1, 1, 0, 0}
                    };

    /// Variable to hold source vertex
    int source = 4;

    /// Call the bfs traversal
    bfs(m, v, source);

    return 0;
}

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s