
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
This is a C++ program in which we generate a undirected random graph for the given edges ‘e’. This algorithm basically implements on a big network and time complexity of this algorithm is O(log(n)).
Algorithm
Begin Function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list. Initialize i = 0 while(i < e) edge[i][0] = rand()%N+1 edge[i][1] = rand()%N+1 Increment I; For i = 0 to N-1 Initialize count = 0 For j = 0 to e-1 if(edge[j][0] == i+1) Print edge[j][1] Increase count else if(edge[j][1] == i+1) Print edge[j][0] Increase count else if(j == e-1 && count == 0) Print Isolated Vertex End
Example
#include<iostream> #include<stdlib.h> #define N 10 using namespace std; void GenerateRandomGraphs(int e) { int i, j, edge[e][2], count; i = 0; // generate a connection between two random numbers, for //sample a small case, limit the number of vertex to 10. while(i < e) { edge[i][0] = rand()%N+1; edge[i][1] = rand()%N+1; i++; } //Print all the connection of each vertex, irrespective of the //direction. cout<<"\nThe generated random graph is: "; for(i = 0; i < N; 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++; } //Print “Isolated vertex” for the vertex having zero degree. 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; GenerateRandomGraphs(e); }
Output
Enter the number of edges for the random graphs: 10 The generated random graph is: 1-> { 10 7 } 2-> { 10 } 3-> { 7 8 7 } 4-> { 7 6 7 } 5-> { Isolated Vertex! } 6-> { 8 4 } 7-> { 4 3 4 1 3 } 8-> { 6 3 } 9-> { Isolated Vertex! } 10-> { 2 1 }
- Related Articles
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- Count number of edges in an undirected graph in C++
- C++ Program to find out the number of bridge edges in a given graph
- C++ Program to Generate a Graph for a Given Fixed Degree Sequence
- C++ program to generate random number
- How to generate a random number in C++?
- C++ Program to Find All Forward Edges in a Graph
- Java Program to generate random elements from a given array
- C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters
- Java Program to generate a random number from an array
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Path
- C++ Program to Create a Random Graph Using Random Edge Generation
- C++ Program to Check the Connectivity of Undirected Graph Using BFS
- C++ Program to Check the Connectivity of Undirected Graph Using DFS

Advertisements