-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_gen.cpp
More file actions
73 lines (67 loc) · 2.14 KB
/
graph_gen.cpp
File metadata and controls
73 lines (67 loc) · 2.14 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
#include <bits/stdc++.h>
using namespace std;
minstd_rand rng(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T> T randint(T a, T b) { return uniform_int_distribution<T>(a, b)(rng);}
vector<vector<int>> generate_graph(int V, int E) {
vector<vector<int>> G(V);
vector<vector<bool>> vst(V, vector<bool>(V, 0));
// Generate random spanning tree with Prufer sequence
vector<int> prufer_seq(V-2, 0);
set <int> L;
for (int i = 0; i < V-2; i++) {
prufer_seq[i] = randint(0, V-1);
}
vector<int> degree(V, 1);
for (int x : prufer_seq) degree[x]++;
for (int i = 0; i < V; i++) {
if (degree[i] == 1) L.insert(i);
}
for (int i = 0; i < V-2; i++) {
int u = *L.begin(), p = prufer_seq[i];
degree[u]--;
degree[p]--;
G[u].push_back(p); G[p].push_back(u);
vst[u][p] = vst[p][u] = 1;
L.erase(u);
if (degree[p] == 1) L.insert(p);
}
int a = *L.begin(), b = *L.rbegin();
vst[a][b] = vst[b][a] = 1;
G[a].push_back(b); G[b].push_back(a);
// Add (E - V + 1) more edges, randomly
vector <pair<int, int>> edge_list;
for (int i = 0; i < V; i++)
for (int j = i+1; j < V; j++)
if (!vst[i][j])
edge_list.push_back({i, j});
random_shuffle(edge_list.begin(), edge_list.end());
for (int i = 0; i < E - V + 1; i++) {
int a, b; tie(a, b) = edge_list[i];
G[a].push_back(b); G[b].push_back(a);
}
return G;
}
void print_graph(vector<vector<int>> graph) {
vector <pair<int, int>> edge_list;
for (int i = 0; i < graph.size(); i++)
for (int j : graph[i])
if (i < j)
edge_list.push_back({i, j});
printf("%d %d\n", (int)graph.size(), (int)edge_list.size());
for (auto p : edge_list) {
printf("%d %d\n", p.first, p.second);
}
}
int main(int argc, char* argv[]) {
int V, E;
if (argc >= 2) {
V = atoi(argv[1]);
E = atoi(argv[2]);
E = max(E, V - 1);
}
else {
V = 10, E = 20;
}
vector<vector<int>> graph = generate_graph(V, E);
print_graph(graph);
}