C Graph Representation

C Graph Representation

In C, a graph can be represented using an adjacency matrix or an adjacency list. Here, we will use an adjacency matrix representation to store the graph structure.

Graph Using Adjacency Matrix

#include 

#define V 5  // Number of vertices

void printGraph(int graph[V][V]) {
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            printf("%d ", graph[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int graph[V][V] = { {0, 1, 0, 1, 0},
                        {1, 0, 1, 1, 0},
                        {0, 1, 0, 0, 1},
                        {1, 1, 0, 0, 1},
                        {0, 0, 1, 1, 0} };

    printf("Adjacency Matrix of the Graph:\n");
    printGraph(graph);

    return 0;
}
    

This program demonstrates how to represent a graph using an adjacency matrix in C. It prints the matrix of a simple graph with 5 vertices.

html>