C Program to Create a Random Graph Using Random Edge Generation

This C program generates a random graph using random edge generation. Generate random number of edges between the vertices and print the graph.

Here is the source code of the C program to generate a random graph. 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. #include <time.h>
  4.  
  5. #define MAX_VERTICES 30
  6. #define MAX_EDGES 10
  7.  
  8. typedef unsigned char vertex;
  9.  
  10. int main(){
  11.  
  12.     /*number of nodes in a graph*/
  13.     srand ( time(NULL) );
  14.     int numberOfVertices = rand() % MAX_VERTICES;
  15.  
  16.     /*number of maximum edges a vertex can have*/
  17.     srand ( time(NULL) );
  18.     int maxNumberOfEdges = rand() % MAX_EDGES;
  19.     /*graphs is 2 dimensional array of pointers*/
  20.     if( numberOfVertices == 0)
  21.         numberOfVertices++;
  22.     vertex ***graph;
  23.     printf("Total Vertices = %d, Max # of Edges = %d\n",numberOfVertices, maxNumberOfEdges);
  24.  
  25.     /*generate a dynamic array of random size*/
  26.     if ((graph = (vertex ***) malloc(sizeof(vertex **) * numberOfVertices)) == NULL){
  27.         printf("Could not allocate memory for graph\n");
  28.         exit(1);
  29.     }
  30.  
  31.     /*generate space for edges*/
  32.     int vertexCounter = 0;
  33.     /*generate space for vertices*/
  34.     int edgeCounter = 0;
  35.  
  36.     for (vertexCounter = 0; vertexCounter < numberOfVertices; vertexCounter++){
  37.         if ((graph[vertexCounter] = (vertex **) malloc(sizeof(vertex *) * maxNumberOfEdges)) == NULL){
  38.             printf("Could not allocate memory for edges\n");
  39.             exit(1);
  40.         }
  41.         for (edgeCounter = 0; edgeCounter < maxNumberOfEdges; edgeCounter++){
  42.             if ((graph[vertexCounter][edgeCounter] = (vertex *) malloc(sizeof(vertex))) == NULL){
  43.                 printf("Could not allocate memory for vertex\n");
  44.                 exit(1);
  45.             }
  46.         }
  47.     }
  48.  
  49.     /*start linking the graph. All vetrices need not have same number of links*/
  50.     vertexCounter = 0;edgeCounter = 0;
  51.     for (vertexCounter = 0; vertexCounter < numberOfVertices; vertexCounter++){
  52.         printf("%d:\t",vertexCounter);
  53.         for (edgeCounter=0; edgeCounter < maxNumberOfEdges; edgeCounter++){
  54.             if (rand()%2 == 1){ /*link the vertices*/
  55.                 int linkedVertex = rand() % numberOfVertices;
  56.                 graph[vertexCounter][edgeCounter] = graph[linkedVertex];
  57.                 printf("%d, ", linkedVertex);
  58.             }
  59.             else{ /*make the link NULL*/
  60.                 graph[vertexCounter][edgeCounter] = NULL;
  61.             }
  62.         }
  63.         printf("\n");
  64.     }
  65.     return 1;
  66. }

$ gcc random_graph.c -o random_graph
$ ./random_graph
 
Total Vertices = 18, Max # of Edges = 8
0:	12, 9, 6, 
1:	6, 1, 
2:	7, 4, 1, 9, 3, 5, 
3:	8, 13, 1, 12, 13, 6, 
4:	5, 11, 
5:	6, 6, 6, 5, 11, 
6:	6, 5, 16, 10, 1, 13, 
7:	14, 13, 13, 12, 
8:	6, 12, 4, 
9:	6, 17, 4, 10, 
10:	6, 6, 11, 10, 
11:	2, 16, 
12:	3, 15, 7, 
13:	6, 15, 3, 9, 15, 
14:	4, 10, 
15:	5, 4, 3, 
16:	17, 11, 
17:	0, 7,

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.