-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphadjmatrix.c
48 lines (40 loc) · 1.2 KB
/
graphadjmatrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
int vertices;
void init(int arr[vertices][vertices]){ //Initialise the matrix.
int i,j;
for(i=0;i<vertices;i++){
for(j=0;j<vertices;j++){
arr[i][j]=0;
}
}
}
void addEdge(int arr[vertices][vertices],int i,int j){ //Add edges in the matrix.
arr[i][j]=1;
arr[j][i]=1;
}
int main() {
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int adjacencyMatrix[vertices][vertices];
init(adjacencyMatrix);
printf("Enter the details for adjacency matrix :\n"); // Input the adjacency matrix
printf("Enter the edge as a pair of vertices(eg: 1 4) and enter -1 to exit:");
int sel; //selection
int j,k;
while(sel != -1)
{
scanf("%d%d",&j,&k);
addEdge(adjacencyMatrix,j,k);
printf("Add more edges? 1(YES) or -1(NO)");
scanf("%d",&sel);
printf("Enter the the pair of vertices to add an edge: ");
}
printf("Adjacency Matrix:\n"); // Display the adjacency matrix
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
printf("%d ", adjacencyMatrix[i][j]);
}
printf("\n");
}
return 0;
}