#include <stdio.h>
#define MAX 10
/* a function to build an adjacency matrix of the graph*/
void buildadjm(int adj[][MAX], int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("Enter 1 if there is an edge from %d to %d, otherwise enter 0 \n",
i,j);
scanf("%d",&adj[i][j]);
}
}
/* a function to compute outdegree of a node*/
int outdegree(int adj[][MAX],int x,int n)
{
int i, count =0;
for(i=0;i<n;i++)
if( adj[x][i] ==1) count++;
return(count);
}
/* a function to compute indegree of a node*/
int indegree(int adj[][MAX],int x,int n)
{
int i, count =0;
for(i=0;i<n;i++)
if( adj[i][x] ==1) count++;
return(count);
}
void main()
{
int adj[MAX][MAX],node,n,i;
printf("Enter the number of nodes in graph maximum = %d\n",MAX);
scanf("%d",&n);
buildadjm(adj,n);
for(i=0;i<n;i++)
{
printf("The indegree of the node %d is %d\n",i,indegree(adj,i,n));
printf("The outdegree of the node %d is %d\n",
i,outdegree(adj,i,n));
}
}
Explanation
- This program uses the adjacency matrix representation of a directed graph to compute the indegree and outdegree of each node of the graph.
- It first builds an adjacency matrix of the graph by calling a buildadjm function, then goes in a loop to compute the indegree and outdegree of each node by calling the indegree and outdegree functions, respectively.
- The indegree function counts the number of 1's in a column of an adjacency matrix using the node number whose indegree is to be computed as a column index.
- The outdegree function counts the number of 1's in a row of an adjacency matrix by using the node number whose outdegree is to be computed as a row index.
- Input: 1. The number of nodes in a graph
2. Information about edges, in the form of values, to be stored in adjacency matrix 1, if there is an edge from node i to node j; 0 otherwise. - Output: The indegree and outdegree of each node.
-
No comments:
Post a Comment