
- 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 Construct a Random Graph by the Method of Random Edge Selection
In this program a random graph is generated for random vertices and edges. The time complexity of this program is O(v*e). Where v is the number of vertices and e is the number of edges.
Algorithm
Begin Develop a function GenRandomGraphs(), with ‘e’ as the number of edges and ‘v’ as the number of vertexes, in the argument list. Assign random values to the number of vertex and edges of the graph, Using rand() function. Print the connections of each vertex, irrespective of the direction. Print “Isolated vertex” for the vertex having no degree. End
Example
#include<iostream> #include<stdlib.h> using namespace std; void GenRandomGraphs(int NOEdge, int NOVertex) { int i, j, edge[NOEdge][2], count; i = 0; //Assign random values to the number of vertex and edges of the graph, Using rand(). while(i < NOEdge) { edge[i][0] = rand()%NOVertex+1; edge[i][1] = rand()%NOVertex+1; //Print the connections of each vertex, irrespective of the direction. if(edge[i][0] == edge[i][1]) continue; else { for(j = 0; j < i; j++) { if((edge[i][0] == edge[j][0] && edge[i][1] == edge[j][1]) || (edge[i][0] == edge[j][1] && edge[i][1] == edge[j][0]))i--; } } i++; } cout<<"\nThe generated random graph is: "; for(i = 0; i < NOVertex; i++) { count = 0; cout<<"\n\t"<<i+1<<"-> { "; for(j = 0; j < NOEdge; 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== NOEdge-1&& count == 0)cout<<"Isolated Vertex!"; //Print “Isolated vertex” for the vertex having no degree. } cout<<" }"; } } int main() { int i, e, n; cout<<"Random graph generation: "; n= 7 + rand()%6; cout<<"\nThe graph has "<<n<<" vertices"; e = rand()%((n*(n-1))/2); cout<<"\nand has "<<e<<" edges."; GenRandomGraphs(e, n); }
Output
Random graph generation: The graph has 8 vertices and has 18 edges. The generated random graph is: 1-> { 5 4 2 } 2-> { 4 8 6 3 1 5 } 3-> { 5 4 7 2 } 4-> { 2 3 7 1 8 5 } 5-> { 3 1 7 4 2 8 } 6-> { 2 8 7 } 7-> { 4 3 5 6 } 8-> { 2 6 4 5 }
- Related Articles
- C++ Program to Create a Random Graph Using Random Edge Generation
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- C++ Program to Find the Edge Connectivity of a Graph
- C++ program to Calculate the Edge Cover of a Graph
- C++ Program to Perform Edge Coloring of a Graph
- C++ Program to Generate a Random Subset by Coin Flipping
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- C++ Program to Generate Random Numbers Using Middle Square Method
- C++ Program to Perform Edge Coloring to the Line Graph of an Input Graph
- C++ Program to Generate Random Numbers Using Multiply with Carry Method
- Java program to generate random numbers
- Java Program to get random letters
- C++ program to generate random number
- C++ program to generate random alphabets
- Java Program to Create random strings

Advertisements