-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminSpanningTree.cpp
More file actions
88 lines (70 loc) · 2.02 KB
/
minSpanningTree.cpp
File metadata and controls
88 lines (70 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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);
}