-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
437 lines (350 loc) · 7.17 KB
/
Copy pathgraph.cpp
File metadata and controls
437 lines (350 loc) · 7.17 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// graph.cpp
#include "graph.hpp"
#include "common.hpp"
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>
#include <cassert>
#include <random>
#include <queue>
// DEBUG!
#include <iostream>
#include <bitset>
namespace
{
index_t read_graph_size(const std::string& filename)
{
std::ifstream file(filename);
std::string line = "";
index_t n = 0;
index_t m = 0;
while (std::getline(file, line))
{
std::istringstream iss(line);
char ch;
if (iss >> ch)
{
if (ch == 'p')
{
std::string format = "";
if (iss >> format >> n >> m)
{
// Format can be whatever.
}
else
{
const std::string error = "Badly formatted problem line in " + filename;
throw std::exception(error.c_str());
}
}
}
}
return n;
}
}
void graph::add_edge(index_t u, index_t v)
{
assert(u >= 0 &&
v >= 0 &&
u != v &&
u < adj_.size() &&
v < adj_.size());
adj_[u] |= (1ULL << v);
adj_[v] |= (1ULL << u);
edges_.emplace_back(u);
edges_.emplace_back(v);
}
index_t graph::get_degree(index_t u) const
{
return popcount64(adj_[u]);
}
index_t graph::num_vertices() const
{
return adj_.size();
}
index_t graph::num_edges() const
{
return edges_.size() / 2;
}
std::vector<index_t> graph::get_edges() const
{
return edges_;
}
bool is_adjacent(const graph& g, index_t u, index_t v)
{
assert(u >= 0 && v >= 0 && u < g.adj_.size() && v < g.adj_.size());
return bittest64(g.adj_[u], v);
}
void print_graph(const graph& g)
{
std::cout << "Matrix:\n";
for (index_t i = 0; i < g.num_vertices(); ++i)
{
std::cout << std::bitset<4>(g.adj_[i]).to_string() << "\n";
}
std::cout << "\nEdges:\n";
auto e = g.get_edges();
for (int i = 0; i < e.size(); i += 2)
{
std::cout << "(" << e[i] << "," << e[i + 1] << ")\n";
}
}
graph build_clique(index_t n)
{
graph g(n);
for (index_t i = 0; i < n; ++i)
{
for (index_t j = i + 1; j < n; ++j)
{
g.add_edge(i, j);
}
}
return g;
}
graph build_path(index_t n)
{
graph g(n);
for (index_t i = 0; i < (n - 1); ++i)
{
g.add_edge(i, i + 1);
}
return g;
}
graph build_biclique(index_t p, index_t q)
{
graph g(p + q);
for (index_t i = 0; i < p; ++i)
{
for (index_t j = p; j < (p + q); ++j)
{
g.add_edge(i, j);
}
}
return g;
}
graph build_star(index_t l)
{
graph g(l + 1);
for (index_t i = 1; i <= l; ++i)
{
g.add_edge(0, i);
}
return g;
}
graph build_cycle(index_t n)
{
graph g(n);
for (index_t i = 0; i < (n - 1); ++i)
{
g.add_edge(i, i + 1);
}
g.add_edge(0, n - 1);
return g;
}
graph build_wheel(index_t spokes)
{
const index_t n = spokes;
graph g(n + 1);
for (index_t i = 0; i < (n - 1); ++i)
{
g.add_edge(i, i + 1);
}
g.add_edge(0, n - 1);
for (index_t i = 0; i < n; ++i)
{
g.add_edge(n, i);
}
return g;
}
graph build_corona(index_t n)
{
graph g(2 * n);
for (index_t i = 0; i < n; ++i)
{
for (index_t j = i + 1; j < n; ++j)
{
g.add_edge(i, j);
}
}
for (index_t i = 0; i < n; ++i)
{
g.add_edge(i, n + i);
}
return g;
}
graph build_random_graph(index_t n, double p)
{
assert(n >= 2 && n <= 64);
std::random_device rd;
std::mt19937 gen(rd());
gen.seed(123);
std::bernoulli_distribution d(p);
graph g(n);
for (index_t i = 0; i < n; ++i)
{
for (index_t j = i + 1; j < n; ++j)
{
if (d(gen))
{
g.add_edge(i, j);
}
}
}
return g;
}
graph read_dimacs(const std::string& filename)
{
index_t n = 0;
try
{
n = read_graph_size(filename);
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
std::ifstream file(filename);
std::string line = "";
graph g(n);
while (std::getline(file, line))
{
std::istringstream iss(line);
char ch;
if (iss >> ch)
{
if (ch == 'e')
{
index_t u = 0;
index_t v = 0;
if (iss >> u >> v)
{
// In DIMACS, vertices start from 1 (not from 0)
g.add_edge(u - 1, v - 1);
}
else
{
const std::string error = "Badly formatted edge line in " + filename;
throw std::exception(error.c_str());
}
}
}
}
return g;
}
void bfs(const graph& g, std::vector<index_t>& dist, index_t source)
{
assert(g.num_vertices() <= 64);
assert(source < g.num_vertices());
const index_t n = g.num_vertices();
index_t visited = (1ULL << source);
std::queue<index_t> q;
q.push(source);
assert(dist.size() == n && std::count(dist.cbegin(), dist.cend(), 0) == n);
while (!q.empty())
{
index_t v = q.front();
for (index_t i = 0; i < n; ++i)
{
if (bittest64(g.adj_[v], i) && !bittest64(visited, i))
{
dist[i] = 1 + dist[v];
q.push(i);
visited |= (1ULL << i);
}
}
q.pop();
}
}
bool is_connected(const graph& g)
{
const index_t n = g.num_vertices();
index_t visited = 1ULL;
index_t q = 1ULL;
// The vertex 0 is visited and added to the queue.
assert(visited == q);
assert(popcount64(visited) <= g.num_vertices());
while (q != 0)
{
assert(popcount64(visited) <= g.num_vertices());
index_t v = ctz64(q);
for (index_t i = 0; i < n; ++i)
{
if (bittest64(g.adj_[v], i) && !bittest64(visited, i))
{
q |= (1ULL << i);
visited |= (1ULL << i);
}
}
q &= ~(1ULL << v);
}
return popcount64(visited) == g.num_vertices();
}
std::vector<index_t> get_bridges(const graph& g)
{
// Naive algorithm: remove each edge, and check if the graph is connected.
std::vector<index_t> bridges;
auto edges = g.get_edges();
const index_t m = edges.size();
for (index_t i = 0; i < (m - 1); i += 2)
{
auto edges_ref = edges;
auto candidate = std::make_pair(edges[i], edges[i + 1]);
assert(candidate.first == *(edges_ref.begin() + i));
assert(candidate.second == *(edges_ref.begin() + i + 1));
edges_ref.erase(edges_ref.begin() + i, edges_ref.begin() + i + 2);
//std::cout << "Testing edge: " << candidate.first << "," << candidate.second << "\n";
graph h(g.num_vertices());
for (index_t j = 0; j < edges_ref.size(); j += 2)
{
h.add_edge(edges_ref[j], edges_ref[j + 1]);
}
if (!is_connected(h))
{
assert(candidate.first < candidate.second);
bridges.emplace_back(candidate.first);
bridges.emplace_back(candidate.second);
}
}
return bridges;
}
index_t get_diameter(const graph& g)
{
const index_t n = g.num_vertices();
index_t diam = 0;
for (index_t i = 0; i < n; ++i)
{
std::vector<index_t> dist(n);
bfs(g, dist, i);
const index_t current_max = *std::max_element(dist.cbegin(), dist.cend());
if (current_max > diam)
diam = current_max;
}
return diam;
}
std::pair<index_t, index_t> get_diametral_pair(const graph& g)
{
const index_t diam = get_diameter(g);
const index_t n = g.num_vertices();
for (index_t i = 0; i < n; ++i)
{
std::vector<index_t> dist(n);
bfs(g, dist, i);
auto distant_elem = std::max_element(dist.cbegin(), dist.cend());
if (diam == *distant_elem)
{
return std::minmax(i, std::distance(dist.cbegin(), distant_elem));
}
}
assert(false && "no diametral pair found");
return std::make_pair(0, 0);
}
// neato -Tpng foo.dot -o foo.png :-)
void write_dot(const graph& g, const std::vector<index_t>& cols, std::ostream& os)
{
const std::string SPACE = " ";
os << "graph G {\n";
os << SPACE;
os << "node [fixedsize=true shape=circle width=0.325];\n";
assert(false && "finish the impl");
}