Data Structure lab

Program 11

Develop a Program in C for the following operations on Graph(G) of Cities

a. Create a Graph of N cities using Adjacency Matrix.

b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method.



#include <stdio.h>
#include <stdlib.h>
#define MAX 20

int graph[MAX][MAX], visited[MAX], n;

// Create the graph
void createGraph() {
    printf("Enter number of cities: ");
    scanf("%d", &n);
    printf("Enter the adjacency matrix:\n");
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &graph[i][j]);
}

// DFS Traversal
void DFS(int v) {
    visited[v] = 1;
    printf("City %d ", v);
    for (int i = 0; i < n; i++) {
        if (graph[v][i] == 1 && !visited[i])
            DFS(i);
    }
}

// BFS Traversal
void BFS(int start) {
    int queue[MAX], front = 0, rear = 0;
    for (int i = 0; i < n; i++) visited[i] = 0;

    visited[start] = 1;
    queue[rear++] = start;

    while (front < rear) {
        int v = queue[front++];
        printf("City %d ", v);
        for (int i = 0; i < n; i++) {
            if (graph[v][i] == 1 && !visited[i]) {
                queue[rear++] = i;
                visited[i] = 1;
            }
        }
    }
}

int main() {
    int choice, start;
    while (1) {
        printf("\n--- MENU ---\n");
        printf("1. Create Graph\n");
        printf("2. DFS Traversal\n");
        printf("3. BFS Traversal\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                createGraph();
                break;
            case 2:
                printf("Enter starting city (0 to %d): ", n - 1);
                scanf("%d", &start);
                for (int i = 0; i < n; i++) visited[i] = 0;
                printf("DFS Traversal from City %d: ", start);
                DFS(start);
                printf("\n");
                break;
            case 3:
                printf("Enter starting city (0 to %d): ", n - 1);
                scanf("%d", &start);
                printf("BFS Traversal from City %d: ", start);
                BFS(start);
                printf("\n");
                break;
            case 4:
                exit(0);
            default:
                printf("Invalid choice!\n");
        }
    }
}

               


  

Output :-


Menu:
1. Create Graph
2. DFS
3. BFS
4. Exit
Enter choice: 1
Enter number of cities: 4
Enter adjacency matrix (0/1):
0 1 1 0
0 0 1 1
0 0 0 1
0 0 0 0

Menu:
1. Create Graph
2. DFS
3. BFS
4. Exit
Enter choice: 2
Enter starting city (0 to 3): 2
DFS Traversal: City 2 City 3 

Menu:
1. Create Graph
2. DFS
3. BFS
4. Exit
Enter choice: 3
Enter starting city (0 to 3): 4
BFS Traversal: City 4 

Menu:
1. Create Graph
2. DFS
3. BFS
4. Exit
Enter choice: 4
  
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.