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

📅  最后修改于: 2021-06-26 15:52:35             🧑  作者: Mango

生成随机有向非加权图

  • 由于这是一个图形,因此测试数据生成计划不能保证形成或不形成一个循环。
  • 边数– NUMEDGE大于零且小于NUM *(NUM-1)/ 2 ,其中NUM =顶点数
  • 对于每一个我们RUN首先打印的顶点的数量– NUM首先在新分离的线和下一NUMEDGE行是一个被连接到b与边缘从a到b(A-> B定向形式(AB)的)
  • 每条NUMEDGE行将具有不同的边,例如-–如果(1,2)在NUMEDGE行之一中,则可以保证(1,2)在其余NUMEDGE-1行中不存在,因为这是有向图。
// A C++ Program to generate test cases for
// an unweighted directed graph
#include
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> container;
    set>::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 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);
}

生成随机有向加权图

  • 由于这是一个图形,因此测试数据生成计划不能保证形成或不形成一个循环。
  • 边数– NUMEDGE大于零且小于NUM *(NUM-1)/ 2 ,其中NUM =顶点数
  • 对于每个RUN,我们首先打印顶点数量-NUM首先在新的单独行中打印,而下NUMEDGE行的形式为(ab wt),其中a连接到b,边从a指向b (a-> b)边缘的重量为wt
  • 每条NUMEDGE行将具有不同的边,例如-–如果(1,2)在NUMEDGE行之一中,则可以保证(1,2)在其余NUMEDGE-1行中不存在,因为这是有向图。
// A C++ Program to generate test cases for
// a weighted directed graph
#include
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> container;
    set>::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 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);
}

生成随机无向非加权图

  • 由于这是一个图形,因此测试数据生成计划不能保证形成或不形成一个循环。
  • 边数– NUMEDGE大于零且小于NUM *(NUM-1)/ 2 ,其中NUM =顶点数
  • 对于每一个我们RUN首先打印的顶点的数量- NUM首先在新分离的线和下一NUMEDGE行是一个被连接到B的形式(AB)
  • 每条NUMEDGE线将具有不同的边,例如–如果NUMEDGE线之一中存在(1,2),则可以保证(1,2)(2,1)都不会在其中剩下的NUMEDGE-1行,因为这是无向图。
// A C++ Program to generate test cases for
// an unweighted undirected graph
#include
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> container;
    set>::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 p = make_pair(a, b);
            pair 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);
}

生成随机无向加权图

    • 由于这是一个图形,因此测试数据生成计划不能保证形成或不形成一个循环。
    • 边数– NUMEDGE大于零且小于NUM *(NUM-1)/ 2 ,其中NUM =顶点数
    • 对于每个RUN,我们首先打印顶点数-NUM首先在新的单独行中打印,接下来的NUMEDGE行的形式为(ab wt) ,其中a连接到b,并且边缘的权重为wt
    • 每条NUMEDGE线将具有不同的边,例如–如果NUMEDGE线之一中存在(1,2),则可以保证(1,2)(2,1)都不会在其中剩下的NUMEDGE-1行,因为这是无向图。
// A C++ Program to generate test cases for
// an weighted undirected graph
#include
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> container;
    set>::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 p = make_pair(a, b);
            pair 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);
}

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。