
- 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 Directed Acyclic Graph DAC for a Given Number of Edges
In this program we generate a random directed acyclic graph for the given edges ‘e’. The time complexity of this program is O(e*v*e).
Algorithm
Begin function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list. generate a connection between two random numbers, for sample a small case, limit the number of vertex to 20. Check for the cycle in the graph Using CheckAcyclic(), if it returns false discard this edge. Else maintain the edge in the graph. Print all the directed connection of each vertex. If the in+out degree of each vertex is zero then print the vertex as “isolated vertex”. End
Example Code
#include<iostream> #include<stdlib.h> #define N 10 using namespace std; bool Checkcyclic(int ed[][2], int edge, bool check[], int v)//to check for the cycle, on addition of a new edge in the random graph.{ int i; // If the current vertex is visited already, then the graph contains cycle. if(check[v] == true) { return false; } else { check[v] = true; // For each vertex, go for all the vertex connected to it. for(i = edge; i >= 0; i--) { if(ed[i][0] == v) { return Checkcyclic(ed, edge, check, ed[i][1]); } } } // In case, if the path ends then reassign the vertexes visited in that path to false again. check[v] = false; if(i == 0) return true; } void GenerateRandomGraphs(int e) { int i, j, ed[e][2], count; bool c[11]; i = 0; while(i < e) { ed[i][0] = rand()%N+1; ed[i][1] = rand()%N+1; for(j = 1; j <= 10; j++) c[j] = false; if(Checkcyclic(ed, i, c, ed[i][0]) == true) i++; } cout<<"\nThe generated random random graph is: "; for(i = 0; i < N; i++) { count = 0; cout<<"\n\t"<<i+1<<"-> { "; for(j = 0; j < e; j++) { if(ed[j][0] == i+1) { cout<<ed[j][1]<<" "; count++; } else if(ed[j][1] == i+1) { count++; } else if(j == e-1 && count == 0) cout<<"Isolated Vertex!"; } cout<<" }"; } } int main() { int 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: 4 The generated random random graph is: 1-> { } 2-> { 8 } 3-> { 5 } 4-> { Isolated Vertex! } 5-> { 1 } 6-> { Isolated Vertex! } 7-> { Isolated Vertex! } 8-> { } 9-> { Isolated Vertex! } 10-> { 5 }
- Related Articles
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- Directed Acyclic Graph (DAG)
- Longest Path in a Directed Acyclic Graph
- Shortest Path in a Directed Acyclic Graph
- C++ Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
- 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
- Python Program for Detect Cycle in a Directed Graph
- How to generate a random number in C++?
- C++ Program to Find All Forward Edges in a Graph
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Path
- 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

Advertisements