Graph-Incident Matrix, In-degree,Out-degree


#include <stdio.h>
int inc[50][50],nv,ne;
void read()
{ int i,j,e;
    printf("Enter the Number of vertices\n");
    scanf("%d",&nv);
    printf("Enter the Number of edges\n");
    scanf("%d",&ne);
    // storing element from [1][1]
    //assuming the vertices and edges are numbered 1 2..n and we will enter only the index
    //use 1 for outgoing edges -1 for incoming edges 0 for no relation
    for(int i=1;i<=nv;i++)
    {
    printf("Enter the details of edges incident to v%d \n",i);
    for(j=1;j<=ne;j++)
    {
    scanf("%d",&e);
    inc[i][j]=e;
    }
    }
}//end of read

void disp()
{
printf("Incident Matrix..\n");
for(int i=1;i<=nv;i++)
{
    for(int j=1;j<=ne;j++)
    printf("%5d",inc[i][j]);
    printf("\n");
}
}
void indegree()
{ int vi,i,id=0;
printf("Enter the vertex index to compute indegree...\n");
scanf("%d",&vi);
for(i=1;i<=ne;i++)
    if(inc[vi][i]==-1) id++;
printf("Indegree of vertex v%d=%d\n",vi,id);
}

void outdegree()
{ int vi,i,od=0;
printf("Enter the vertex index to compute outdegree..\n");
scanf("%d",&vi);
for(i=1;i<=ne;i++)
    if(inc[vi][i]==1) od++;
printf("Outdegree of vertex v%d=%d\n",vi,od);
}
int main()
{
read();
disp();
indegree();
outdegree();
return 0;
}

Comments

Popular posts from this blog

Data Structure Lab CSL 201 Manual KTU - Dr Binu V P-9847390760

Lab Assignments

Polynomial Addition using Arrays