Graph Representation using Adjacency Matrix in C

This C program generates graph using Adjacency Matrix Method.

A graph G,consists of two sets V and E. V is a finite non-empty set of vertices.E is a set of pairs of vertices,these pairs are called as edges V(G) and E(G) will represent the sets of vertices and edges of graph G.
Undirected graph – It is a graph with V vertices and E edges where E edges are undirected. In undirected graph, each edge which is present between the vertices Vi and Vj,is represented by using a pair of round vertices (Vi,Vj).
Directed graph – It is a graph with V vertices and E edges where E edges are directed.In directed graph,if Vi and Vj nodes having an edge.than it is represented by a pair of triangular brackets Vi,Vj.

Here is the source code of the C program to create a graph using adjacency matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. //... A Program to represent a Graph by using an Adjacency Matrix method
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. void main()
  5. {
  6.    int option;
  7.    do
  8.    {    	
  9.         printf("\n A Program to represent a Graph by using an ");
  10. 	printf("Adjacency Matrix method \n ");
  11. 	printf("\n 1. Directed Graph ");
  12. 	printf("\n 2. Un-Directed Graph ");
  13. 	printf("\n 3. Exit ");
  14. 	printf("\n\n Select a proper option : ");
  15. 	scanf("%d", &option);
  16. 	switch(option)
  17. 	{
  18.             case 1 : dir_graph();
  19.                      break;
  20.             case 2 : undir_graph();
  21.                      break;
  22.             case 3 : exit(0);
  23. 	} // switch
  24.     }while(1);
  25. }
  26.  
  27. int dir_graph()
  28. {
  29.     int adj_mat[50][50];
  30.     int n;
  31.     int in_deg, out_deg, i, j;
  32.     printf("\n How Many Vertices ? : ");
  33.     scanf("%d", &n);
  34.     read_graph(adj_mat, n);
  35.     printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
  36.     for (i = 1; i <= n ; i++ )
  37.     {
  38.         in_deg = out_deg = 0;
  39. 	for ( j = 1 ; j <= n ; j++ )
  40. 	{
  41.             if ( adj_mat[j][i] == 1 )
  42.                 in_deg++;
  43. 	} 
  44.         for ( j = 1 ; j <= n ; j++ )
  45.             if (adj_mat[i][j] == 1 )
  46.                 out_deg++;
  47.             printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
  48.     }
  49.     return;
  50. }
  51.  
  52. int undir_graph()
  53. {
  54.     int adj_mat[50][50];
  55.     int deg, i, j, n;
  56.     printf("\n How Many Vertices ? : ");
  57.     scanf("%d", &n);
  58.     read_graph(adj_mat, n);
  59.     printf("\n Vertex \t Degree ");
  60.     for ( i = 1 ; i <= n ; i++ )
  61.     {
  62.         deg = 0;
  63.         for ( j = 1 ; j <= n ; j++ )
  64.             if ( adj_mat[i][j] == 1)
  65.                 deg++;
  66.         printf("\n\n %5d \t\t %d\n\n", i, deg);
  67.     } 
  68.     return;
  69. } 
  70.  
  71. int read_graph ( int adj_mat[50][50], int n )
  72. {
  73.     int i, j;
  74.     char reply;
  75.     for ( i = 1 ; i <= n ; i++ )
  76.     {
  77.         for ( j = 1 ; j <= n ; j++ )
  78.         {
  79.             if ( i == j )
  80.             {
  81.                 adj_mat[i][j] = 0;
  82. 		continue;
  83.             } 
  84.             printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j);
  85.             scanf("%c", &reply);
  86.             if ( reply == 'y' || reply == 'Y' )
  87.                 adj_mat[i][j] = 1;
  88.             else
  89.                 adj_mat[i][j] = 0;
  90. 	}
  91.     } 
  92.     return;
  93. }

$ gcc graph.c -o graph
$ ./graph
 A Program to represent a Graph by using an Adjacency Matrix method 
 
 1. Directed Graph 
 2. Un-Directed Graph 
 3. Exit 
 
 Select a proper option : 
 How Many Vertices ? : 
 Vertices 1 & 2 are Adjacent ? (Y/N) : N
 Vertices 1 & 3 are Adjacent ? (Y/N) : Y
 Vertices 1 & 4 are Adjacent ? (Y/N) : Y
 Vertices 2 & 1 are Adjacent ? (Y/N) : Y
 Vertices 2 & 3 are Adjacent ? (Y/N) : Y
 Vertices 2 & 4 are Adjacent ? (Y/N) : N
 Vertices 3 & 1 are Adjacent ? (Y/N) : Y
 Vertices 3 & 2 are Adjacent ? (Y/N) : Y
 Vertices 3 & 4 are Adjacent ? (Y/N) : Y
 Vertices 4 & 1 are Adjacent ? (Y/N) : Y
 Vertices 4 & 2 are Adjacent ? (Y/N) : N
 Vertices 4 & 3 are Adjacent ? (Y/N) : Y
 Vertex 	 In_Degree 	 Out_Degree 	 Total_Degree 
 
     1			2		0		2
 
 
 
     2			1		2		3
 
 
 
     3			0		1		1
 
 
 
     4			1		1		2
 
 
 A Program to represent a Graph by using an Adjacency Matrix method 
 
 1. Directed Graph 
 2. Un-Directed Graph 
 3. Exit

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.