-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
295 lines (240 loc) · 8.91 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <igraph.h>
#include <stdlib.h>
#include <getopt.h>
#include <vector>
#include <unordered_map>
#include <stack>
#include "main.h"
#include "util.h"
int num_vertices = -1, num_colors = -1, degree = -1;
igraph_t graph;
int num_colorings = 1;
int num_steps = -1;
double stopping_threshold = NAN;
std::vector<double> vector;
std::vector<double> new_vector;
std::vector<uint64_t> bitfields;
#ifdef MEMOIZE
std::vector<std::vector<int> > neighbors;
#endif
std::unordered_map<uint64_t, int> indices;
// #define GET_NTH_COLOR(x,n) (unsigned int) ((x >> color_bits*n) & ((1 << color_bits) - 1))
// #define COLOR_MASK(n) (uint64_t) ((1 << color_bits*(n+1)) - (1 << color_bits*n))
// #define SET_NTH_COLOR(x,n,c) (x & ~COLOR_MASK(n)) | (c << color_bits*n)
// DFS to find all colorings
void find_colorings(uint64_t initial_coloring) {
std::stack<int> colorings;
colorings.push(0);
indices[initial_coloring] = 0;
bitfields.push_back(initial_coloring);
igraph_vector_t neighbors_vec;
igraph_vector_init(&neighbors_vec, degree);
while (!colorings.empty()) {
int i = colorings.top();
colorings.pop();
uint64_t x = bitfields[i];
for (int v = 0; v < num_vertices; v++) {
for (int c = 0; c < num_colors; c++) {
if (c == GET_NTH_COLOR(x, v)) continue;
if (check_valid_coloring(&neighbors_vec, x, v, c)) {
uint64_t y = SET_NTH_COLOR(x, v, c);
if (!indices.count(y)) {
indices[y] = num_colorings++;
printf("%d\n", num_colorings);
bitfields.push_back(y);
colorings.push(indices[y]);
}
}
}
}
}
#ifdef MEMOIZE
for (int i = 0; i < num_colorings; i++) {
std::vector<int> neighbors_of_i;
uint64_t x = bitfields[i];
for (int v = 0; v < num_vertices; v++) {
for (int c = 0; c < num_colors; c++) {
if (c == GET_NTH_COLOR(x, v)) continue;
if (check_valid_coloring(&neighbors_vec, x, v, c))
neighbors_of_i.push_back(indices[SET_NTH_COLOR(x, v, c)]);
}
}
neighbors.push_back(neighbors_of_i);
}
#endif
igraph_vector_destroy(&neighbors_vec);
}
// take one "step" on the random walk, or, more precisely, multiply "vector" by the
// random-walk matrix of the Markov chain and place the result in "new_vector"
void matrix_vector_mult() {
// iterate through all possible colorings
for (int i = 0; i < num_colorings; i++) {
uint64_t x = bitfields[i];
int self_loops = 0;
#ifdef MEMOIZE
for (int j : neighbors[i]) {
new_vector[j] += (1.0/(num_colors * num_vertices)) * vector[i];
}
self_loops = num_colors * num_vertices - neighbors[i].size();
#else
igraph_vector_t neighbors_vec;
igraph_vector_init(&neighbors_vec, degree);
for (int v = 0; v < num_vertices; v++) {
for (int c = 0; c < num_colors; c++) {
if (GET_NTH_COLOR(x, v) != c && check_valid_coloring(&neighbors_vec, x, v, c)) {
uint64_t y = SET_NTH_COLOR(x, v, c);
new_vector[indices[y]] += (1.0/(num_colors * num_vertices)) * vector[i];
} else self_loops++;
}
}
igraph_vector_destroy(&neighbors_vec);
#endif
// add properly weighted self-loop
new_vector[i] += (((double) self_loops) / (num_colors * num_vertices)) * vector[i];
}
}
void tv_dist_iterate() {
vector.clear();
new_vector.clear();
vector.push_back(1.0);
for (int i = 1; i < num_colorings; i++) vector.push_back(0.0);
for (int i = 0; i < num_colorings; i++) new_vector.push_back(0.0);
// initialize File IO
FILE *fp;
char fileName[40];
sprintf(fileName, "data/TV-V%dK%dD%d.csv", num_vertices, num_colors, degree);
printf("Written into %s\n", fileName);
fp = fopen(fileName, "w+");
if (fp == NULL) {
fprintf(stderr, "Couldn't open %s\n", fileName);
exit(1);
}
print_parameters(fp);
fprintf(fp, "STEP, TV-dist\n");
// main loop to "advance vector by one step"
for (int t = 0; t < num_steps || num_steps == -1; t++) {
printf("Running step %d.\n", t);
clear_vector(&new_vector);
matrix_vector_mult();
vector.swap(new_vector);
double tv_dist = calculate_tv_dist(vector);
fprintf(fp, "%d, %f\n", t, tv_dist);
printf("%f\n", tv_dist);
if (stopping_threshold != NAN && tv_dist <= stopping_threshold) {
break;
}
}
fclose(fp);
}
void nu_2_iterate() {
vector.clear();
new_vector.clear();
for (int i = 0; i < num_colorings; i++) vector.push_back(-1.0 + (rand() % 2) * 2); // fill with random hypercube
for (int i = 0; i < num_colorings; i++) new_vector.push_back(0.0);
// initialize File IO
FILE *fp;
char fileName[40];
sprintf(fileName, "data/NU2-V%dK%dD%d.csv", num_vertices, num_colors, degree);
printf("Written into %s\n", fileName);
fp = fopen(fileName, "w+");
if (fp == NULL) {
fprintf(stderr, "Couldn't open %s\n", fileName);
exit(1);
}
// Print parameters
print_parameters(fp);
fprintf(fp, "STEP, NU2-est\n");
double prev_nu2 = 1;
// main loop to "advance vector by one step"
for (int t = 0; t < num_steps || num_steps == -1; t++) {
printf("Running step %d.\n", t);
clear_vector(&new_vector);
matrix_vector_mult();
vector.swap(new_vector);
// subtract out stationary distribution for stability purposes
double product = 0;
for (int i = 0; i < num_colorings; i++) product += vector[i] * 1.0/num_colorings;
for (int i = 0; i < num_colorings; i++) vector[i] -= product * 1.0/num_colorings;
// calculate norm of vector
double l2_norm = 0;
for (int i = 0; i < num_colorings; i++) l2_norm += vector[i]*vector[i];
l2_norm = sqrt(l2_norm);
printf("prev: %f, now: %f\n", prev_nu2, 1 - l2_norm);
if (t > 1 && prev_nu2 - (1 - l2_norm) <= 0.000001) {
break;
}
fprintf(fp, "%d, %f\n", t, 1 - l2_norm);
printf("%d, %f\n", t, 1 - l2_norm);
prev_nu2 = 1 - l2_norm;
// renormalize
for (int i = 0; i < num_colorings; i++) vector[i] /= l2_norm;
}
fclose(fp);
}
int main(int argc, char *argv[]) {
// https://stackoverflow.com/questions/1052746/getopt-does-not-parse-optional-arguments-to-parameters/32575314
int getopt_ret, option_index;
static struct option long_options[] = {
{"num_vertices", required_argument, 0, 'n'},
{"num_colors", required_argument, 0, 'k'},
{"degree", required_argument, 0, 'd'},
{"num_steps", required_argument, 0, 't'},
{"stopping_threshold", required_argument, 0, 'e'},
{"seed", required_argument, 0, 's'},
{0, 0, 0, 0}
};
int seed = -1;
while (true) {
getopt_ret = getopt_long(argc, argv, "n:k:k:t:e:", long_options, &option_index);
if (getopt_ret == -1) break;
switch(getopt_ret)
{
case 0: break;
case 'n':
num_vertices = atoi(optarg);
break;
case 'k':
num_colors = atoi(optarg);
break;
case 'd':
degree = atoi(optarg);
break;
case 't':
num_steps = atoi(optarg);
break;
case 'e':
stopping_threshold = atof(optarg);
break;
case 's':
seed = atoi(optarg);
break;
}
}
if (num_vertices == -1 || num_colors == -1 || num_colors == -1
|| (num_steps == -1 && stopping_threshold == NAN)) {
printf("usage: sample_colorings [--num_vertices=6] [--num_colors=5] [--degree=3] [--num_steps=1000] [--stopping_threshold 0.001] [--seed=42]\n");
printf("must pass: --num_vertices, --num_colors, --degree, and at least one of {--num_steps, --stopping_threshold}\n");
return -1;
}
// choose a random undirected graph on num_vertices vertices, where each edge is included w.p. 1/3
if (seed != -1) {
igraph_rng_seed(igraph_rng_default(), seed);
}
igraph_k_regular_game(&graph, num_vertices, degree,
IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);
igraph_bool_t connected;
igraph_is_connected(&graph, &connected, IGRAPH_STRONG);
assert(connected);
printf("Searching for colorings.\n");
find_colorings(find_initial_coloring());
printf("num_colorings: %d\n", num_colorings);
printf("Finished initialization!\n===========\n\n");
tv_dist_iterate();
nu_2_iterate();
igraph_destroy(&graph);
return 0;
}