测试用例生成|集4(随机有向/无向加权和无加权图)

随机生成 有向无权图

null
  • 由于这是一个图表,测试数据生成计划不能保证一个循环是否形成。
  • 边的数量—— 努梅奇 大于零且小于 NUM*(NUM-1)/2 ,其中NUM=顶点数
  • 每人 我们首先打印顶点的数量 –NUM 首先在一条新的独立线中,接下来的NUMEDGE线的形式是(a b),其中a连接到b,边从a指向b (a->b)
  • 每一条NUMEDGE线都有不同的边缘,例如–如果 (1, 2) NUMEDGE系列中有没有一款可以保证 (1, 2) 不会出现在剩余的NUMEDGE-1行中,因为这是一个有向图。

CPP

// 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 =  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", 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", it->first, it->second);
container.clear();
printf ("");
}
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


随机生成 有向加权图

  • 由于这是一个图表,测试数据生成计划不能保证一个循环是否形成。
  • 边数–NUMEDGE大于零且小于 NUM*(NUM-1)/2 ,其中NUM=顶点数
  • 每人 我们首先打印顶点的数量–NUM first在一个新的单独行中,下一个NUMEDGE行的形式是(a b wt),其中a连接到b,边从a指向b (a->b) 边缘的权重为wt
  • 每一条NUMEDGE线都有不同的边,例如,如果其中一条NUMEDGE线中有(1,2),则可以保证: (1, 2) 在剩下的时间里不会出现 NUMEDGE-1 这是一个有向图。

CPP

// 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", 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", it->first, it->second, wt);
}
container.clear();
printf ("");
}
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


随机生成 无向无权图

  • 由于这是一个图表,测试数据生成计划不能保证一个循环是否形成。
  • 边的数量—— 努梅奇 大于零且小于 NUM*(NUM-1)/2 哪里 NUM=垂直的数量 s
  • 每人 我们首先打印顶点的数量—— 全国矿工联盟 首先在一个新的单独的行和下一个NUMEDGE行的形式 (a b) 连接到 B
  • 每一条NUMEDGE线都有不同的边缘,例如–如果 (1, 2) NUMEDGE系列中有没有一款可以保证 (1, 2) (2, 1) 由于这是一个无向图,所以在剩下的NUMEDGE-1行中不会出现这两种情况。

CPP

// 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 =  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", 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", it->first, it->second);
container.clear();
printf ("");
}
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


随机生成 无向加权图

  • 由于这是一个图表,测试数据生成计划不能保证一个循环是否形成。
  • 边的数量—— 努梅奇 大于零且小于 NUM*(NUM-1)/2 哪里 NUM=顶点数
  • 每人 我们首先打印顶点的数量–NUM first在一个新的单独行中,下一个NUMEDGE行的形式如下 (a b wt) 其中a连接到b,且边缘的重量为wt
  • 每一条NUMEDGE线都有不同的边缘,例如–如果 (1, 2) NUMEDGE系列中有没有一款可以保证 (1, 2) (2, 1) 由于这是一个无向图,所以在剩下的NUMEDGE-1行中不会出现这两种情况。

CPP

// 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 =  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", 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", it->first, it->second, wt);
}
container.clear();
printf ("");
}
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


本文由 拉希特·贝尔瓦里亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享