🌳
B.TECH I YEAR β€’ II SEMESTER β€’ R25

Data Structures - Lab Manual

Data Structures Laboratory β€’ C Programming Solution

πŸ“˜ R25 Regulation πŸŽ“ I Year πŸ“š II Semester πŸ’» C Programming πŸ§ͺ Lab Program
Selected Program

C program to implement the Breadth First Search a graph traversal methods

← All Lab Programs

C program to implement the Breadth First Search a graph traversal methods

BFS
#include<stdio.h>

// creating queue data structure using arrays
int queue[10];

// defining pointers of the queue to perform pop and push
int front=0,back=0;

// defining push operation on the queue
void push(int var)
{
    queue[back] = var;
    back++;
}

// defining pop operation on queue
void pop()
{
    queue[front] = 0;
    front++;
}

// creating a visited array to keep the track of visited nodes
int visited[7] = {0};

int main()
{
    int v,n,i,j;
    // adjacenty matrix representing graph
    int graph[10][10];
    printf("Enter the number of vertices: ");
    scanf("%d", &n);
    printf("Enter graph data in matrix form:    \n");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            scanf("%d", &graph[i][j]);

    // adding a starting node in the list 
    printf("Enter the starting vertex: ");
    scanf("%d", &v);
    push(v);
    while(front != back)
    {
        int current = queue[front];
        
        // printing current element 
        printf("%d ", current);
        
        // popping the front element from the queue
        pop();
 
        for(int i=0;i < 6;i++)
        {
            // adding non-visited connected nodes of the current node to the queue 
            if((graph[current-1][i] == 1) && (visited[i] == 0))
            {
                visited[i] = 1; // marking visisted
                push(i+1);
            }
        }
    }
    return 0;
}

OUTPUT
Enter the number of vertices: 6
Enter graph data in matrix form:    
0 1 1 0 0 0
1 0 1 0 0 0
1 1 0 1 1 0
0 0 1 0 0 0
0 0 1 0 0 1
0 0 0 0 1 0
Enter the starting vertex: 2
2 1 3 2 4 5 6 

πŸ’» Run This C Program

C program to implement the Breadth First Search a graph traversal methods
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
← Back to Data Structures Lab Programs πŸ“š All Lab Manuals