Thursday 24 April 2014

Wap to calculate the degree of each vertex and hence the total degree of a Graph using Incident Matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
  
    int a[10][10],i,j,deg,m,n,totaldegree=0,deg1=0,connect=0,chck=0;
    printf("@@@enter the number of vertices and edges in the graph@@@\n");
    scanf("%d%d",&m,&n);

    printf("enter the incidence matrix elements : \n");
    for(i=0;i<m;i++)
    {
       for(j=0;j<n;j++)
                    {
                       scanf("%d",&a[i][j]);
                    }
    }
    printf("the degree of each vertex is : \n");
    for(j=0;j<n;j++)
    {              deg1=0;             
                    for(i=0;i<m;i++)
                    {
                                    if(a[i][j]==1)
                                    {         deg1++;                                   
                                              totaldegree++;
                                    }                                  
                    }
                    if(deg1==1)
                    {
                              printf("%d  self loop present \n",deg1);
                              totaldegree++;
                              for(i=0;i<m;i++)
                              {
                                              if(a[i][j]==1)
                                              {
                                                            a[i][j]=2;
                                              }
                              }
                             
                    }
 
                   
}

printf("the total degree is %d \n",totaldegree);
 for(i=0;i<m;i++)
    {  deg=0; 
       connect=0;
         for(j=0;j<n;j++)
          {
                                    if(a[i][j]>0)
                                    {
                                    deg=a[i][j]+deg;
                                    } 
                                    else
                                    {
                                      connect++; 
                                    }       
           }
                   
      printf("degree of V%d = %d\n",i,deg);
    }
    if(chck>0)
    {
              if(connect==n)
              {
              printf("it is disconnected graph!\n");
              }
}
else
{
    printf("connected graph");
}
getch();

}


No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2022