Thursday, 24 April 2014

C program to find the number of triangle in the given Graph

 #include<stdio.h>  
 #include<conio.h>  
 int main()  
 {  
  int i,k,j,l,m,a[10][10],c[10][10],b[10][10],d[10][10],sumd=0,tng;  
  printf(" Enter the no of vertices: ");  
  scanf ("%d",&m);  
  printf ("@@@Enter the adjacency of matrix: @@@\n");  
  for (i=1;i<=m;i++)  
  {  
   for(j=1;j<=m;j++)  
   {  
     scanf("%d",&a[i][j]);  
   }  
  }      
  for (i=1;i<=m;i++)  
  {  
   for(j=1;j<=m;j++)  
   {  
     b[i][j]=a[i][j];  
   }  
  }  
  printf("Multiplication of matrix (A*A)\n");      
  for(i=1;i<=m;i++)  
  {   
   for(j=1;j<=m;j++)  
   {  
     c[i][j]=0;  
     for(k=1;k<=m;k++)  
     {  
       c[i][j]=c[i][j]+(a[i][k]*b[k][j]);  
     }  
   }  
  }  
  for(i=1;i<=m;i++)  
  {   
   for(j=1;j<=m;j++)  
   {  
     d[i][j]=0;  
     for(l=1;l<=m;l++)  
     {  
      d[i][j]=d[i][j]+(a[i][l]*c[l][j]);  
     }  
   }  
  }  
  for(i=1;i<=m;i++)  
  {  
    for(j=1;j<=m;j++)  
    {   
     printf("%3d",c[i][j]);  
    }  
    printf("\n");  
  }  
  printf("Multiplication of matrix (A*A*A)");  
  printf("\n");  
  for(i=1;i<=m;i++)  
  {  
    for(j=1;j<=m;j++)  
    {   
     printf("%3d",d[i][j]);  
    }  
    printf("\n");  
  }  
  printf("Displaying and sum of the diagonal element");  
  for(i=1;i<=m;i++)  
  {  
   for(j=1;j<=m;j++)  
   {  
    if(i==j)  
    {  
      printf("\n%d", d[i][j]);  
      sumd=sumd+d[i][j];  
    }  
   }  
   printf("\n");  
  }  
  printf("\nSum of the diagonal of a3 matrix: %d\n",sumd);       
  tng=sumd/6;  
  printf("Number of triangles in a given graph: %d",tng);  
  getch();  
 }  

4 comments:
Write comments
  1. hello sir, can u just tell me how to display the vertices of found triangle in the graph?

    ReplyDelete
  2. hello sir, how to display the vertices of found triangle?

    ReplyDelete
  3. hello sir, how to display the vertices of found triangle?

    ReplyDelete
  4. This is all done with maths calculation but for traceing the found triangle you may suppose to chose different method.

    ReplyDelete

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-2016