C++ Programming Code Examples C++ > Computer Graphics Code Examples Program to Generate a Random UnDirected Graph for a Given Number of Edges Program to Generate a Random UnDirected Graph for a Given Number of Edges - This algorithm generates a undirected random graph for the given edges 'e'. - Basically it implements on a big network. - The time complexity of this algorithm is O(log(n)). - This algorithm takes the input of the number of edges 'e'. - It connects vertexes randomly and generate cyclic, acyclic or disconnected graphs. #include<iostream> #include<stdlib.h> // The maximum number of vertex for the sample random graph. #define NOV 20 using namespace std; // A function to generate random graph. void GenerateRandGraphs(int e) { int i, j, edge[e][2], count; i = 0; // Build a connection between two random vertex. while(i < e) { edge[i][0] = rand()%NOV+1; edge[i][1] = rand()%NOV+1; i++; } // Print the random graph. cout<<"\nThe generated random graph is: "; for(i = 0; i < NOV; i++) { count = 0; cout<<"\n\t"<<i+1<<"-> { "; for(j = 0; j < e; j++) { if(edge[j][0] == i+1) { cout<<edge[j][1]<<" "; count++; } else if(edge[j][1] == i+1) { cout<<edge[j][0]<<" "; count++; } else if(j == e-1 && count == 0) cout<<"Isolated Vertex!"; } cout<<" }"; } } int main() { int n, i ,e; cout<<"Enter the number of edges for the random graphs: "; cin>>e; // A function to generate random undirected graph with e edges. GenerateRandGraphs(e); }