Adjacency Matrix Code
Adjacency matrix code using C is a way to represent a graph as a 2D array. The array is of size V x V, where V is the number of vertices in the graph. If there is an edge from vertex i to vertex j, then the entry adj[i][j] is set to 1. Otherwise, the entry is set to 0.
Adjacency matrix code is a simple and efficient way to represent graphs. It is easy to implement and use. However, it can be inefficient for large graphs, as it requires O(V^2) space.
For Such more codes click here and for video tutorial click here.
//Adjacency Matrix
#include
#include
void create(int[50][50], int);
void dispAdjMat(int[50][50], int);
void main() {
int i, j, m, n;
int adjmat[50][50];
printf("Enter the total number node : ");
scanf("%d", & m);
create(adjmat, m);
printf("Adjacency Matrix is : \n");
dispAdjMat(adjmat, m);
}
void create(int adj[50][50], int g) {
int i, j;
char ch;
for (i = 0; i < g; i++) {
printf("\n===========================================\n");
for (j = 0; j < g; j++) {
printf("\n%d is connected with %d ? (y/n) : ", i + 1, j + 1);
scanf(" %c", & ch);
if (ch == 'y' || ch == 'Y') {
adj[i][j] = 1;
//printf("%d %d %d",i,j,adj[i][j]);
} else {
adj[i][j] = 0;
// printf("%d %d %d",i,j,adj[i][j]);
}
}
}
}
void dispAdjMat(int adj[][50], int g) {
int i, j;
printf("Adjacency Matrix of the entered graph : \n");
for (i = 0; i < g; i++)
printf("\tV%d", i + 1);
printf("\n");
for (i = 0; i < g; i++) {
printf("V%d", i + 1);
for (j = 0; j < g; j++)
printf("\t%d", adj[i][j]);
printf("\n");
}
}