Graph Representation Using Adjacency Matrix - 👑 सार्थक मुंड S3 🤴🏻

Graph Representation Using Adjacency Matrix

Graph Representation Using Adjacency Matrix

Graph Representation Using Adjacency Matrix

#include<stdio.h>
//init matrix to 0
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");
     }
}
//print the adjMatrix
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;
}

OUTPUT :
0 1 1 1 0
0 0 0 1 1
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0
Previous article
Next article

Leave Comments

Post a Comment

Articles Ads

Articles Ads 1

Articles Ads 2

Advertisement Ads