Thursday 24 April 2014

C program to convert Incidence matrix to adjacency matrix

The below given program converts incidence matrix elements to adjacency matrix elements.

 #include<stdio.h>  
 #include<conio.h>  
 int main()  
 {  
   int a[10][10],b[10][10],i,j,deg=0,n,p,k,l;  
   printf("enter the number of vertices in the graph\n");  
   scanf("%d",&n);  
   printf("enter the adjacency matrix elements : \n");  
   for(i=0;i<n;i++)  
   {  
           for(j=0;j<n;j++)  
           {  
                   scanf("%d",&a[i][j]);  
           }  
   }  
   // printf("the degree of each vertex is : \n");  
   for(i=0;i<n;i++)  
   {  
           for(j=0;j<n;j++)  
           {  
                   if((i!=j) && a[i][j]==1)  
                   {  
                        deg++;  
                   }  
                   else  
                   if(a[i][j]==1)  
                   {  
                          deg=deg+2;  
                   }  
           }  
           //printf("the degree of V%d = %d\n",i,deg);  
 }  
 p=deg/2;  
 for(k=0;k<n;k++)  
 {  
         for(l=0;l<p;l++)  
         {  
                 b[k][l]=0;  
         }  
 }  
 k=0;  
 l=0;  
 for(i=0;i<n;i++)  
 {  
         for(j=i;j<n;j++)  
         {  
                 if(a[i][j]==1)  
                 {  
                  k=i;  
                  b[k][l]=1;  
                  k=j;  
                  b[k][l]=1;  
                  l++;  
                  }  
         }  
 }  
 printf("the Incidence matrix is : \n\n");  
   for(k=0;k<n;k++)  
 {  
         for(l=0;l<p;l++)  
         {  
                 printf("%d\t",b[k][l]);  
         }  
         printf("\n");  
 }                
 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