-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_generator.cpp
149 lines (130 loc) · 3.46 KB
/
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
using namespace std;
set<int> *G;
int *labels;
int main(int argc, char **argv) {
int seed = 42;
if (argc < 3) {
printf("Usage: %s N M output label A B seed\n", argv[0]);
printf("\tN, number of nodes\n");
printf("\tM, number of edges\n");
printf("\toutput, filename (optional, default: stdout)\n");
printf("\tA, size of set A (jaccard)\n");
printf("\tB, size of set B (jaccard)\n");
printf(
"\tlabel, labeled graph in [0, label-1] (optional, 0 if not "
"labeled)\n");
printf("\tseed, (optional)\n");
return 1;
}
bool jaccard = false;
int N = atol(argv[1]);
int M = atol(argv[2]);
int Mc = M;
int fd = -1;
int label = 0;
int sampleS[2] = {0, 0};
int *sample[2];
if (argc >= 4 && strcmp(argv[3],"stdout") != 0) {
fd = open(argv[3], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("Opening file");
return -1;
}
}
if (argc >= 5) label = atoi(argv[4]);
if( argc >= 7 )
{
jaccard = true;
sampleS[0] = atoi(argv[5]);
sampleS[1] = atoi(argv[6]);
}
if (argc >= 8) seed = atoi(argv[7]);
assert(seed >= 0);
assert(N > 0);
assert(M > 0);
assert(M >= N);
assert(sampleS[0] >= 0);
assert(sampleS[0] < N);
assert(sampleS[1] >= 0);
assert(sampleS[1] < N);
assert(label >= 0);
assert(label < 255);
assert((long long)M <= (long long)N * (N - 1) / 2);
srand(seed);
G = new set<int>[N];
labels = new int[N];
for (int i = 0; i < N - 1; i++) {
G[i].insert(i + 1);
G[i + 1].insert(i);
M--;
}
if (label > 0)
for (int i = 0; i < N; i++) labels[i] = rand() % label;
int *toWrite = new int[2 + Mc * 2 + (jaccard ? (2+sampleS[0]+sampleS[1]) : 0 )];
int idx = 0;
do {
int i = rand() % N;
if (G[i].size() == (size_t)N - 1) continue;
while (1) {
int j = rand() % N;
if (j == i || G[i].find(j) != G[i].end()) continue;
G[i].insert(j);
G[j].insert(i);
break;
}
M--;
} while (M);
toWrite[idx++] = N;
toWrite[idx++] = Mc;
for (int i = 0; i < N; i++)
for (auto j : G[i])
if (i < j) {
toWrite[idx++] = i;
toWrite[idx++] = j;
}
if(jaccard)
{
sample[0] = new int[sampleS[0]];
sample[1] = new int[sampleS[1]];
vector<int> V;
for(int i=0; i<N; i++) V.push_back(i);
random_shuffle(V.begin(), V.end());
for(int i=0; i<sampleS[0]; i++)
sample[0][i] = V[i];
for(int i=0; i<sampleS[0]; i++)
sample[1][i] = V[N-1-i];
}
if (fd != -1) {
write(fd, (void *)toWrite, 2 * sizeof(int));
if (label > 0) write(fd, (void *)labels, N * sizeof(int));
write(fd, (void *)&toWrite[2], 2 * Mc * sizeof(int));
if(jaccard)
{
write(fd, (void *) sampleS, 2*sizeof(int));
write(fd, (void *) sample[0], sampleS[0]*sizeof(int));
write(fd, (void *) sample[1], sampleS[1]*sizeof(int));
}
} else {
printf("%d %d\n", toWrite[0], toWrite[1]);
if (label > 0) {
for (int i = 0; i < N; i++) printf("%d ", labels[i]);
printf("\n");
}
for (int i = 1; i < Mc + 1; i++)
printf("%d %d\n", toWrite[2 * i], toWrite[2 * i + 1]);
if( jaccard )
{
printf("%d %d\n", sampleS[0], sampleS[1]);
for(int i=0; i<sampleS[0]; i++) printf("%d ", sample[0][i]);
printf("\n");
for(int i=0; i<sampleS[1]; i++) printf("%d ", sample[1][i]);
printf("\n");
}
}
close(fd);
return 0;
}