Files
@ d06d14d184a0
Branch filter:
Location: AENC/switchchain/cpp/switchchain.cpp
d06d14d184a0
8.6 KiB
text/x-c++src
Add "CircularEmbedding" graph representation
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 | #include <algorithm>
#include <fstream>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>
class Edge {
public:
unsigned int u, v;
bool operator==(const Edge &e) const { return u == e.u && v == e.v; }
};
// Its assumed that u,v are distinct.
// Check if all four vertices are distinct
bool edgeConflicts(const Edge &e1, const Edge &e2) {
return (e1.u == e2.u || e1.u == e2.v || e1.v == e2.u || e1.v == e2.v);
}
std::ostream &operator<<(std::ostream &s, const Edge &e) {
s << '{' << e.u << ',' << e.v << '}';
return s;
}
class DiDegree {
public:
unsigned int in;
unsigned int out;
};
typedef std::vector<unsigned int> DegreeSequence;
typedef std::vector<DiDegree> DiDegreeSequence;
class Graph {
public:
Graph() {}
Graph(unsigned int n) { adj.resize(n); }
~Graph() {}
void resize(unsigned int n) {
if (n < adj.size()) {
edges.clear();
}
adj.resize(n);
}
unsigned int edgeCount() const { return edges.size(); }
Edge &getEdge(unsigned int i) { return edges[i]; }
const Edge &getEdge(unsigned int i) const { return edges[i]; }
bool createFromDegreeSequence(const DegreeSequence &d) {
// Havel-Hakimi algorithm
unsigned int n = d.size();
// degree, vertex index
std::vector<std::pair<unsigned int, unsigned int>> degrees(n);
for (unsigned int i = 0; i < n; ++i) {
degrees[i].first = d[i];
degrees[i].second = i;
}
edges.clear();
adj.resize(n);
while (!degrees.empty()) {
std::sort(degrees.begin(), degrees.end());
// Highest degree is at back of the vector
// Take it out
unsigned int degree = degrees.back().first;
unsigned int u = degrees.back().second;
degrees.pop_back();
if (degree > degrees.size()) {
edges.clear();
adj.clear();
return false;
}
// Now loop over the last 'degree' entries of degrees
auto rit = degrees.rbegin();
for (unsigned int i = 0; i < degree; ++i) {
if (rit->first == 0 || !addEdge({u, rit->second})) {
edges.clear();
adj.clear();
return false;
}
rit->first--;
++rit;
}
}
return true;
}
DegreeSequence getDegreeSequence() const {
DegreeSequence d(adj.size());
std::transform(adj.begin(), adj.end(), d.begin(),
[](const auto &u) { return u.size(); });
return d;
}
// Assumes valid vertex indices
bool hasEdge(const Edge &e) const {
for (unsigned int v : adj[e.u]) {
if (v == e.v)
return true;
}
return false;
}
bool addEdge(const Edge &e) {
if (e.u >= adj.size() || e.v >= adj.size())
return false;
if (hasEdge(e))
return false;
edges.push_back(e);
adj[e.u].push_back(e.v);
adj[e.v].push_back(e.u);
return true;
}
// There are two possible edge exchanges
// switchType indicates which one is desired
// Returns false if the switch is not possible
bool exchangeEdges(const Edge &e1, const Edge &e2, bool switchType) {
// The new edges configuration is one of these two
// A) e1.u - e2.u and e1.v - e2.v
// B) e1.u - e2.v and e1.v - e2.u
// First check if the move is possible
if (switchType) {
if (hasEdge({e1.u, e2.u}) || hasEdge({e1.v, e2.v}))
return false; // conflicting edges
} else {
if (hasEdge({e1.u, e2.v}) || hasEdge({e1.v, e2.u}))
return false; // conflicting edges
}
// Find the edges in the adjacency lists
unsigned int i1, j1, i2, j2;
for (i1 = 0; i1 < adj[e1.u].size(); ++i1) {
if (adj[e1.u][i1] == e1.v)
break;
}
for (j1 = 0; j1 < adj[e1.v].size(); ++j1) {
if (adj[e1.v][j1] == e1.u)
break;
}
for (i2 = 0; i2 < adj[e2.u].size(); ++i2) {
if (adj[e2.u][i2] == e2.v)
break;
}
for (j2 = 0; j2 < adj[e2.v].size(); ++j2) {
if (adj[e2.v][j2] == e2.u)
break;
}
// Remove the old edges
bool removedOne = false;
for (auto iter = edges.begin(); iter != edges.end();) {
if (*iter == e1) {
iter = edges.erase(iter);
if (removedOne)
break;
removedOne = true;
} else if (*iter == e2) {
iter = edges.erase(iter);
if (removedOne)
break;
removedOne = true;
} else {
++iter;
}
}
// Add the new edges
if (switchType) {
adj[e1.u][i1] = e2.u;
adj[e1.v][j1] = e2.v;
adj[e2.u][i2] = e1.u;
adj[e2.v][j2] = e1.v;
edges.push_back({e1.u, e2.u});
edges.push_back({e1.v, e2.v});
} else {
adj[e1.u][i1] = e2.v;
adj[e1.v][j1] = e2.u;
adj[e2.u][i2] = e1.v;
adj[e2.v][j2] = e1.u;
edges.push_back({e1.u, e2.v});
edges.push_back({e1.v, e2.u});
}
return true;
}
private:
// Graph is saved in two formats for speed
// The two should be kept consistent at all times
std::vector<std::vector<unsigned int>> adj;
std::vector<Edge> edges;
};
// Mathematica style export
std::ostream &operator<<(std::ostream &s, const Graph &g) {
if (g.edgeCount() == 0) {
s << '{' << '}';
return s;
}
s << '{' << g.getEdge(0).u << '<' << '-' << '>' << g.getEdge(0).v;
for (unsigned int i = 1; i < g.edgeCount(); ++i) {
const Edge &e = g.getEdge(i);
s << ',' << e.u << '<' << '-' << '>' << e.v;
}
s << '}';
return s;
}
class SwitchChain {
public:
SwitchChain() : mt(std::random_device{}()), permutationDistribution(0, 2) {
// random_device uses hardware entropy if available
// std::random_device rd;
// mt.seed(rd());
}
~SwitchChain() {}
bool initialize(const Graph &gstart) {
if (gstart.edgeCount() == 0)
return false;
g = gstart;
edgeDistribution.param(
std::uniform_int_distribution<>::param_type(0, g.edgeCount() - 1));
return true;
}
bool doMove() {
Edge e1 = g.getEdge(edgeDistribution(mt));
Edge e2 = g.getEdge(edgeDistribution(mt));
// Keep regenerating while conflicting edges
int timeout = 0;
while (edgeConflicts(e1, e2)) {
e1 = g.getEdge(edgeDistribution(mt));
e2 = g.getEdge(edgeDistribution(mt));
++timeout;
if (timeout % 100 == 0) {
std::cerr << "Warning: sampled " << timeout
<< " random edges but they keep conflicting.\n";
}
}
// Consider one of the three possible permutations
// 1) e1.u - e1.v and e2.u - e2.v (original)
// 2) e1.u - e2.u and e1.v - e2.v
// 3) e1.u - e2.v and e1.v - e2.u
// Note that it might be that these new edges already exist
// in which case we also reject the move
// This is checked in exchangeEdges
int perm = permutationDistribution(mt);
if (perm == 0) // Original permutation
return false;
return g.exchangeEdges(e1, e2, perm == 1);
}
Graph g;
std::mt19937 mt;
std::uniform_int_distribution<> edgeDistribution;
std::uniform_int_distribution<> permutationDistribution;
};
int main() {
Graph g;
if (!g.createFromDegreeSequence({1, 2, 2, 2, 3, 3, 3})) {
std::cerr << "Could not create graph from degree sequence.\n";
return 1;
}
SwitchChain chain;
if (!chain.initialize(g)) {
std::cerr << "Could not initialize Markov chain.\n";
return 1;
}
std::ofstream outfile("graphdata.m");
outfile << '{' << g;
std::cout << "Starting switch Markov chain" << std::endl;
int movesDone = 0;
int movesTotal = 100000;
for (int i = 0; i < movesTotal; ++i) {
if (chain.doMove())
++movesDone;
if (i % (movesTotal / 100) == (movesTotal / 100 - 1))
outfile << ',' << chain.g;
}
outfile << '}';
std::cout << movesDone << '/' << movesTotal << " moves succeeded."
<< std::endl;
return 0;
}
|