ADJACENT MATRIX
This usually used in representation of graphs.If there are n vertices in graph then there are n rows and n columns in matrix.
Let us take Ith row and Jth column then A[I][J] represents the edge between two nodes I & J.If a[I][J] == 0 then there is no edge between node I and node J & if A[I][J] == 1 then there is edge.
These type of graph representation is used when there are more number of edges.Reason is that if there are e less number of edges then lot of space will wasted in storing the zeroes which is not good thing.
Following is the graph and its adjancy matrix =>
#include<stdio.h>
#include<stdlib.h>
int main()
{
// n is number of edges
int n;
printf("Enter the number of edges => ");
scanf("%d",&n);
// construct adjancy matrix
int adjMatrix[n+1][n+1];
// iterate for status of connected or not
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n;j++){
printf("Are vertices %d & %d connected.1 yes and 0 for no",i,j);
int key;
scanf("%d",&key);
adjMatrix[i][j] = key;
}
}
// traverse for checking the connectivity
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n;j++){
if (adjMatrix[i][j])
{
printf("%d & %d vertices are connected\n",i,j);
}else{
printf("%d & %d vertices are not connected\n",i,j);
}
}
}
return 0;
}
Enter the number of edges => 2
Are vertices 0 & 0 connected.1 yes and 0 for no1
Are vertices 0 & 1 connected.1 yes and 0 for no1
Are vertices 1 & 0 connected.1 yes and 0 for no1
Are vertices 1 & 1 connected.1 yes and 0 for no1
0 & 0 vertices are connected
0 & 1 vertices are connected
1 & 0 vertices are connected
1 & 1 vertices are connected
Comments
Post a Comment