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); } |
chevron_right
filter_none
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); } |
chevron_right
filter_none
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 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
- 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); } |
chevron_right
filter_none
Generating Random Undirected 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 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); } |
chevron_right
filter_none
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.
Recommended Posts:
- Generating Test Cases (generate() and generate_n() in C++)
- Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
- Test Case Generation | Set 1 (Random Numbers, Arrays and Matrices)
- Test Case Generation | Set 3 (Unweighted and Weighted Trees)
- Test Case Generation | Set 5 (Generating random Sorted Arrays and Palindromes)
- How to prepare for Google Asia Pacific University (APAC) Test ?
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- AKS Primality Test
- Vantieghems Theorem for Primality Test
- Multi Source Shortest Path in Unweighted Graph
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Print a case where the given sorting algorithm fails
- Find two numbers whose divisors are given in a random order
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Count of unique lengths of connected components for an undirected graph using STL
- Nodes with prime degree in an undirected Graph


