Generating Random **Directed Unweighted Graphs**

- Since this is a graph, the test data generation plan doesn’t guarantee that a cycle gets formed or not.
- The number of edges –
**NUMEDGE**is greater than zero and less than**NUM*(NUM-1)/2**, where NUM = Number of Vertices - For each
**RUN**we first print the number of vertices**– NUM**first in a new separate line and the next NUMEDGE lines are of the form (a b) where a is connected to b and the edge is directed from a to b**(a->b)** - Each of the NUMEDGE lines will have distinct edges, for e.g – if
**(1, 2)**is there in one of the NUMEDGE lines then it is guaranteed, that**(1, 2)**will not be there in the remaining NUMEDGE-1 lines as this is a directed graph.

`// A C++ Program to generate test cases for` `// an unweighted directed graph` `#include<bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Define the number of runs for the test data` `// generated` `#define RUN 5` ` ` `// Define the maximum number of vertices of the graph` `#define MAX_VERTICES 20` ` ` `// Define the maximum number of edges` `#define MAX_EDGES 200` ` ` `int` `main()` `{` ` ` `set<pair<` `int` `, ` `int` `>> container;` ` ` `set<pair<` `int` `, ` `int` `>>::iterator it;` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// freopen ("Test_Cases_Directed_Unweighted_Graph.in", "w", stdout);` ` ` ` ` `//For random values every time` ` ` `srand` `(` `time` `(NULL));` ` ` ` ` `int` `NUM; ` `// Number of Vertices` ` ` `int` `NUMEDGE; ` `// Number of Edges` ` ` ` ` `for` `(` `int` `i=1; i<=RUN; i++)` ` ` `{` ` ` `NUM = 1 + ` `rand` `() % MAX_VERTICES;` ` ` ` ` `// Define the maximum number of edges of the graph` ` ` `// Since the most dense graph can have N*(N-1)/2 edges` ` ` `// where N = nnumber of vertices in the graph` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `while` `(NUMEDGE > NUM*(NUM-1)/2)` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES; ` ` ` ` ` `// First print the number of vertices and edges` ` ` `printf` `(` `"%d %d\n"` `, NUM, NUMEDGE);` ` ` ` ` `// Then print the edges of the form (a b)` ` ` `// where 'a' is connected to 'b'` ` ` `for` `(` `int` `j=1; j<=NUMEDGE; j++)` ` ` `{` ` ` `int` `a = 1 + ` `rand` `() % NUM;` ` ` `int` `b = 1 + ` `rand` `() % NUM;` ` ` `pair<` `int` `, ` `int` `> p = make_pair(a, b);` ` ` ` ` `// Search for a random "new" edge everytime` ` ` `// Note - In a tree the edge (a, b) is same ` ` ` `// as the edge (b, a)` ` ` `while` `(container.find(p) != container.end())` ` ` `{` ` ` `a = 1 + ` `rand` `() % NUM;` ` ` `b = 1 + ` `rand` `() % NUM;` ` ` `p = make_pair(a, b);` ` ` `}` ` ` `container.insert(p);` ` ` `}` ` ` ` ` `for` `(it=container.begin(); it!=container.end(); ++it)` ` ` `printf` `(` `"%d %d\n"` `, it->first, it->second);` ` ` ` ` `container.clear();` ` ` `printf` `(` `"\n"` `); ` ` ` ` ` `}` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// fclose(stdout);` ` ` `return` `(0);` `}` |

Generating Random** Directed Weighted Graphs**

- Since this is a graph, the test data generation plan doesn’t guarantee that a cycle gets formed or not.
- The number of edges – NUMEDGE is greater than zero and less than
**NUM*(NUM-1)/2**, where NUM = Number of Vertices - For each
**RUN**we first print the number of vertices – NUM first in a new separate line and the next NUMEDGE lines are of the form (a b wt) where a is connected to b and the edge is directed from a to b**(a->b)**and the edge has a weight of wt - Each of the NUMEDGE lines will have distinct edges, for e.g – if (1, 2) is there in one of the NUMEDGE lines then it is guaranteed, that
**(1, 2)**will not be there in the remaining**NUMEDGE-1**lines as this is a directed graph.

`// A C++ Program to generate test cases for` `// a weighted directed graph` `#include<bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Define the number of runs for the test data` `// generated` `#define RUN 5` ` ` `// Define the maximum number of vertices of the graph` `#define MAX_VERTICES 20` ` ` `// Define the maximum number of edges` `#define MAX_EDGES 200` ` ` `// Define the maximum weight of edges` `#define MAXWEIGHT 200` ` ` `int` `main()` `{` ` ` `set<pair<` `int` `, ` `int` `>> container;` ` ` `set<pair<` `int` `, ` `int` `>>::iterator it;` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// freopen("Test_Cases_Directed_Weighted_Graph.in",` ` ` `// "w", stdout);` ` ` ` ` `// For random values every time` ` ` `srand` `(` `time` `(NULL));` ` ` ` ` `int` `NUM; ` `// Number of Vertices` ` ` `int` `NUMEDGE; ` `// Number of Edges` ` ` ` ` `for` `(` `int` `i=1; i<=RUN; i++)` ` ` `{` ` ` `NUM = 1 + ` `rand` `() % MAX_VERTICES;` ` ` ` ` `// Define the maximum number of edges of the graph` ` ` `// Since the most dense graph can have N*(N-1)/2 edges` ` ` `// where N = n number of vertices in the graph` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `while` `(NUMEDGE > NUM*(NUM-1)/2)` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `// First print the number of vertices and edges` ` ` `printf` `(` `"%d %d\n"` `, NUM, NUMEDGE);` ` ` ` ` `// Then print the edges of the form (a b)` ` ` `// where 'a' is connected to 'b'` ` ` `for` `(` `int` `j=1; j<=NUMEDGE; j++)` ` ` `{` ` ` `int` `a = 1 + ` `rand` `() % NUM;` ` ` `int` `b = 1 + ` `rand` `() % NUM;` ` ` `pair<` `int` `, ` `int` `> p = make_pair(a, b);` ` ` ` ` `// Search for a random "new" edge every time` ` ` `// Note - In a tree the edge (a, b) is same` ` ` `// as the edge (b, a)` ` ` `while` `(container.find(p) != container.end())` ` ` `{` ` ` `a = 1 + ` `rand` `() % NUM;` ` ` `b = 1 + ` `rand` `() % NUM;` ` ` `p = make_pair(a, b);` ` ` `}` ` ` `container.insert(p);` ` ` `}` ` ` ` ` `for` `(it=container.begin(); it!=container.end(); ++it)` ` ` `{` ` ` `int` `wt = 1 + ` `rand` `() % MAXWEIGHT;` ` ` `printf` `(` `"%d %d %d\n"` `, it->first, it->second, wt);` ` ` `}` ` ` ` ` `container.clear();` ` ` `printf` `(` `"\n"` `);` ` ` ` ` `}` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// fclose(stdout);` ` ` `return` `(0);` `}` |

Generating Random **Undirected Unweighted Graphs**

- Since this is a graph, the test data generation plan doesn’t guarantee that a cycle gets formed or not.
- The number of edges –
**NUMEDGE**is greater than zero and less than**NUM*(NUM-1)/2**, where**NUM = Number of Vertice**s - For each
**RUN**we first print the number of vertices –**NUM**first in a new separate line and the next NUMEDGE lines are of the form**(a b)**where a is connected to**b** - Each of the NUMEDGE lines will have distinct edges, for e.g – if
**(1, 2)**is there in one of the NUMEDGE lines then it is guaranteed, that**(1, 2)**and**(2, 1)**both will not be there in the remaining NUMEDGE-1 lines as this is an undirected graph.

`// A C++ Program to generate test cases for` `// an unweighted undirected graph` `#include<bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Define the number of runs for the test data` `// generated` `#define RUN 5` ` ` `// Define the maximum number of vertices of the graph` `#define MAX_VERTICES 20` ` ` `// Define the maximum number of edges` `#define MAX_EDGES 200` ` ` `int` `main()` `{` ` ` `set<pair<` `int` `, ` `int` `>> container;` ` ` `set<pair<` `int` `, ` `int` `>>::iterator it;` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// freopen("Test_Cases_Undirected_Unweighted_Graph.in",` ` ` `// "w", stdout);` ` ` ` ` `// For random values every time` ` ` `srand` `(` `time` `(NULL));` ` ` ` ` `int` `NUM; ` `// Number of Vertices` ` ` `int` `NUMEDGE; ` `// Number of Edges` ` ` ` ` `for` `(` `int` `i=1; i<=RUN; i++)` ` ` `{` ` ` `NUM = 1 + ` `rand` `() % MAX_VERTICES;` ` ` ` ` `// Define the maximum number of edges of the graph` ` ` `// Since the most dense graph can have N*(N-1)/2 edges` ` ` `// where N = nnumber of vertices in the graph` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `while` `(NUMEDGE > NUM*(NUM-1)/2)` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `// First print the number of vertices and edges` ` ` `printf` `(` `"%d %d\n"` `, NUM, NUMEDGE);` ` ` ` ` `// Then print the edges of the form (a b)` ` ` `// where 'a' is connected to 'b'` ` ` `for` `(` `int` `j=1; j<=NUMEDGE; j++)` ` ` `{` ` ` `int` `a = ` `rand` `() % NUM;` ` ` `int` `b = ` `rand` `() % NUM;` ` ` `pair<` `int` `, ` `int` `> p = make_pair(a, b);` ` ` `pair<` `int` `, ` `int` `> reverse_p = make_pair(b, a);` ` ` ` ` `// Search for a random "new" edge everytime` ` ` `// Note - In a tree the edge (a, b) is same` ` ` `// as the edge (b, a)` ` ` `while` `(container.find(p) != container.end() ||` ` ` `container.find(reverse_p) != container.end())` ` ` `{` ` ` `a = ` `rand` `() % NUM;` ` ` `b = ` `rand` `() % NUM;` ` ` `p = make_pair(a, b);` ` ` `reverse_p = make_pair(b, a);` ` ` `}` ` ` `container.insert(p);` ` ` `}` ` ` ` ` `for` `(it=container.begin(); it!=container.end(); ++it)` ` ` `printf` `(` `"%d %d\n"` `, it->first, it->second);` ` ` ` ` `container.clear();` ` ` `printf` `(` `"\n"` `);` ` ` ` ` `}` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// fclose(stdout);` ` ` `return` `(0);` `}` |

Generating Random **Undirected Weighted Graphs**

- The number of edges –
**NUMEDGE**is greater than zero and less than**NUM*(NUM-1)/2**, where**NUM = Number of Vertices** - For each
**RUN**we first print the number of vertices – NUM first in a new separate line and the next NUMEDGE lines are of the form**(a b wt)**where a is connected to b and the edge has a weight of wt - Each of the NUMEDGE lines will have distinct edges, for e.g – if
**(1, 2)**is there in one of the NUMEDGE lines then it is guaranteed, that**(1, 2)**and**(2, 1)**both will not be there in the remaining NUMEDGE-1 lines as this is an undirected graph.

`// A C++ Program to generate test cases for` `// an weighted undirected graph` `#include<bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Define the number of runs for the test data` `// generated` `#define RUN 5` ` ` `// Define the maximum number of vertices of the graph` `#define MAX_VERTICES 20` ` ` `// Define the maximum number of edges` `#define MAX_EDGES 200` ` ` `// Define the maximum weight of edges` `#define MAXWEIGHT 200` ` ` `int` `main()` `{` ` ` `set<pair<` `int` `, ` `int` `>> container;` ` ` `set<pair<` `int` `, ` `int` `>>::iterator it;` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// freopen("Test_Cases_Undirected_Weighted_Graph.in",` ` ` `// "w", stdout);` ` ` ` ` `//For random values every time` ` ` `srand` `(` `time` `(NULL));` ` ` ` ` `int` `NUM; ` `// Number of Vertices` ` ` `int` `NUMEDGE; ` `// Number of Edges` ` ` ` ` `for` `(` `int` `i=1; i<=RUN; i++)` ` ` `{` ` ` `NUM = 1 + ` `rand` `() % MAX_VERTICES;` ` ` ` ` `// Define the maximum number of edges of the graph` ` ` `// Since the most dense graph can have N*(N-1)/2 edges` ` ` `// where N = nnumber of vertices in the graph` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `while` `(NUMEDGE > NUM*(NUM-1)/2)` ` ` `NUMEDGE = 1 + ` `rand` `() % MAX_EDGES;` ` ` ` ` `// First print the number of vertices and edges` ` ` `printf` `(` `"%d %d\n"` `, NUM, NUMEDGE);` ` ` ` ` `// Then print the edges of the form (a b)` ` ` `// where 'a' is connected to 'b'` ` ` `for` `(` `int` `j=1; j<=NUMEDGE; j++)` ` ` `{` ` ` `int` `a = ` `rand` `() % NUM;` ` ` `int` `b = ` `rand` `() % NUM;` ` ` `pair<` `int` `, ` `int` `> p = make_pair(a, b);` ` ` `pair<` `int` `, ` `int` `> reverse_p = make_pair(b, a);` ` ` ` ` `// Search for a random "new" edge everytime` ` ` `// Note - In a tree the edge (a, b) is same` ` ` `// as the edge (b, a)` ` ` `while` `(container.find(p) != container.end() ||` ` ` `container.find(reverse_p) != container.end())` ` ` `{` ` ` `a = ` `rand` `() % NUM;` ` ` `b = ` `rand` `() % NUM;` ` ` `p = make_pair(a, b);` ` ` `reverse_p = make_pair(b, a);` ` ` `}` ` ` `container.insert(p);` ` ` `}` ` ` ` ` `for` `(it=container.begin(); it!=container.end(); ++it)` ` ` `{` ` ` `int` `wt = 1 + ` `rand` `() % MAXWEIGHT;` ` ` `printf` `(` `"%d %d %d\n"` `, it->first, it->second, wt);` ` ` `}` ` ` ` ` `container.clear();` ` ` `printf` `(` `"\n"` `);` ` ` ` ` `}` ` ` ` ` `// Uncomment the below line to store` ` ` `// the test data in a file` ` ` `// fclose(stdout);` ` ` `return` `(0);` `}` |

This article is contributed by **Rachit Belwariar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**