Graph Representation using 2D Arrays in C

This is a C Program to generates graph using 2D Array. 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 source code of the C Program to Represent Graph Using 2D Arrays. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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

Output:

$ gcc GraphUsingTwoDMatrix.c
$ ./a.out
 
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.