-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple_connected_graph_generator.cpp
60 lines (49 loc) · 1.36 KB
/
simple_connected_graph_generator.cpp
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
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <graph_output_file>\n";
return 0;
}
int n, m;
int x, y;
cin >> n >> m;
if (m < n - 1 || m > n * 1ll * (n - 1) / 2) {
cout << "Invalid parameters\n";
// Can't be a simple connected graph
return 0;
}
freopen(argv[1], "w", stdout);
cout << n << " " << m << endl;
int nodes[n + 1];
pair<int, int> *edges = new pair<int, int>[m + 1];
map<pair<int, int>, bool> edgeExists;
for (int i = 0; i < n; i++) {
nodes[i] = i;
}
srand(time(0));
random_shuffle(nodes, nodes + n);
for (int i = 1; i < n; i++) {
int v = rand() % i;
x = min(nodes[i], nodes[v]);
y = max(nodes[i], nodes[v]);
edges[i - 1] = make_pair(x, y);
edgeExists[make_pair(x, y)] = 1;
}
for (int i = n - 1; i < m; i++) {
do {
x = rand() % n;
y = rand() % n;
if (x > y)
swap(x, y);
} while (x == y || edgeExists[make_pair(x, y)]);
edges[i] = make_pair(x, y);
edgeExists[make_pair(x, y)] = 1;
}
random_shuffle(edges, edges + m);
for (int i = 0; i < m; i++) {
cout << edges[i].first << " " << edges[i].second << endl;
}
}