This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
78 lines (57 loc) · 1.78 KB
/
test.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
#include "test.h"
#include <vector>
#include <set>
#include "bmlrp.h"
#include "debug.h"
#include "graph.h"
#include "misc.h"
using std::cerr;
using std::endl;
Addr Prefix(const Addr addr, const int len) {
return (addr >> (sizeof(addr) * 8 - len));
}
void DFS0(int node, const Graph& graph, const std::vector<Addr>& addrs,
const int nextLevel, std::vector<bool>* used) {
used->at(node) = true;
std::vector<int> succ = graph.GetDirectSuccessors(node);
for (uint i = 0; i < succ.size(); ++i) {
int to = succ[i];
myassert(to != node);
if (!used->at(to)) {
myassert(Prefix(addrs[node], nextLevel) == Prefix(addrs[to], nextLevel));
DFS0(to, graph, addrs, nextLevel, used);
}
}
}
void TestNextLevel(const Graph& graph, const std::vector<Addr>& addrs,
const int nextLevel) {
myassert(graph.Symmetric());
std::set<Addr> usedPrefixes;
std::vector<bool> usedNodes(graph.n, false);
for (int i = 0; i < graph.n; ++i) {
if (!usedNodes[i]) {
Addr prefix = Prefix(addrs[i], nextLevel);
myassert(usedPrefixes.find(prefix) == usedPrefixes.cend());
usedPrefixes.insert(prefix);
DFS0(i, graph, addrs, nextLevel, &usedNodes);
}
}
}
int DFS1(int node, const Graph& graph, std::vector<bool>* used) {
used->at(node) = true;
int res = 1;
std::vector<int> succ = graph.GetDirectSuccessors(node);
for (uint i = 0; i < succ.size(); ++i) {
int to = succ[i];
if (!used->at(to)) {
res += DFS1(to, graph, used);
}
}
return res;
}
bool IsGraphConnected(const Graph& graph) {
cerr << "Checking that graph is connected" << endl;
set_stack_size((1<<20) + 200 * graph.n); // 1MB + 200 bytes per node
std::vector<bool> used(graph.n, false);
return (DFS1(0, graph, &used) == graph.n);
}