Let GV E be an unweighted undirected graph Its corresponding

Let G(V, E) be an unweighted undirected graph. Its corresponding adjacency matrix is A, where a= 1 if (i, j) is an edge and zero otherwise. The diagonals are all marked as 1. We multiply A by itself n times, n

Solution

#include <stdio.h>
#include <stdlib.h>
void main()
{
int option;
do
{    
printf(\"\ A Program to represent a Graph by using an \");
   printf(\"Adjacency Matrix method \ \");
   printf(\"\ 1. Directed Graph \");
   printf(\"\ 2. Un-Directed Graph \");
   printf(\"\ 3. Exit \");
   printf(\"\ \ Select a proper option : \");
   scanf(\"%d\", &option);
   switch(option)
   {
case 1 : dir_graph();
break;
case 2 : undir_graph();
break;
case 3 : exit(0);
   } // switch
}while(1);
}

int dir_graph()
{
int adj_mat[50][50];
int n;
int in_deg, out_deg, i, j;
printf(\"\ How Many Vertices ? : \");
scanf(\"%d\", &n);
read_graph(adj_mat, n);
printf(\"\ Vertex \\t In_Degree \\t Out_Degree \\t Total_Degree \");
for (i = 1; i <= n ; i++ )
{
in_deg = out_deg = 0;
   for ( j = 1 ; j <= n ; j++ )
   {
if ( adj_mat[j][i] == 1 )
in_deg++;
   }
for ( j = 1 ; j <= n ; j++ )
if (adj_mat[i][j] == 1 )
out_deg++;
printf(\"\ \ %5d\\t\\t\\t%d\\t\\t%d\\t\\t%d\ \ \",i,in_deg,out_deg,in_deg+out_deg);
}
return;
}

int undir_graph()
{
int adj_mat[50][50];
int deg, i, j, n;
printf(\"\ How Many Vertices ? : \");
scanf(\"%d\", &n);
read_graph(adj_mat, n);
printf(\"\ Vertex \\t Degree \");
for ( i = 1 ; i <= n ; i++ )
{
deg = 0;
for ( j = 1 ; j <= n ; j++ )
if ( adj_mat[i][j] == 1)
deg++;
printf(\"\ \ %5d \\t\\t %d\ \ \", i, deg);
}
return;
}

int read_graph ( int adj_mat[50][50], int n )
{
int i, j;
char reply;
for ( i = 1 ; i <= n ; i++ )
{
for ( j = 1 ; j <= n ; j++ )
{
if ( i == j )
{
adj_mat[i][j] = 0;
       continue;
}
printf(\"\ Vertices %d & %d are Adjacent ? (Y/N) :\",i,j);
scanf(\"%c\", &reply);
if ( reply == \'y\' || reply == \'Y\' )
adj_mat[i][j] = 1;
else
adj_mat[i][j] = 0;
   }
}
return;
}

 Let G(V, E) be an unweighted undirected graph. Its corresponding adjacency matrix is A, where a= 1 if (i, j) is an edge and zero otherwise. The diagonals are a
 Let G(V, E) be an unweighted undirected graph. Its corresponding adjacency matrix is A, where a= 1 if (i, j) is an edge and zero otherwise. The diagonals are a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site