I'm learning about graph data structure in C and I have represented a graph with the adjacency matrix. So far I created the adjacency matrix which means(in my understanding at least) that I have specified 'between which vertices there will be edges' altough I have not yet created any actual nodes. Now after I have done that I want to actually populate my graph with nodes which contain a data type of my own(say each node in a structure which contains some elements). I have googled a lot but all I found was examples on how to create the adjacency matrix but the explanations would stop there without showing how you actually insert new elements in the graph.
I have written the code to populate the adjacency matrix. I have provided the code below:
#include<stdio.h>
#define V 5
void init(int arr[][V])
{
int i,j;
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
arr[i][j] = 0;
}
void addEdge(int arr[][V],int src, int dest)
{
arr[src][dest] = 1;
}
void printAdjMatrix(int arr[][V])
{
int i, j;
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main()
{
int adjMatrix[V][V];
init(adjMatrix);
addEdge(adjMatrix,0,1);
addEdge(adjMatrix,0,2);
addEdge(adjMatrix,0,3);
addEdge(adjMatrix,1,3);
addEdge(adjMatrix,1,4);
addEdge(adjMatrix,2,3);
addEdge(adjMatrix,3,4);
printAdjMatrix(adjMatrix);
return 0;
}
Now my question is: to populate my graph with new nodes do I have to create another array of size noofnodes x noofnodes and populate it? Is that the correct way to do it or is there any other way? I want to know how the normal and considered correct way to do this is.
Thank you for reading.