diff --git a/cpp/switchchain.cpp b/cpp/switchchain.cpp index c28de798386c5f684874bda92e5ab545330f8104..30e4389eed3e925e9bbe5b4d4dd5b790503d056a 100644 --- a/cpp/switchchain.cpp +++ b/cpp/switchchain.cpp @@ -2,6 +2,7 @@ #include "graph.hpp" #include "powerlaw.hpp" #include "graph_spectrum.hpp" +#include "switchchain.hpp" #include #include #include @@ -10,61 +11,6 @@ #include #include -// 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); -} - -class SwitchChain { - public: - SwitchChain() - : mt(std::random_device{}()), permutationDistribution(0.5) - // 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() { - int e1index, e2index; - int timeout = 0; - // Keep regenerating while conflicting edges - do { - e1index = edgeDistribution(mt); - e2index = edgeDistribution(mt); - if (++timeout % 100 == 0) { - std::cerr << "Warning: sampled " << timeout - << " random edges but they keep conflicting.\n"; - } - } while (edgeConflicts(g.getEdge(e1index), g.getEdge(e2index))); - - // 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 - bool switchType = permutationDistribution(mt); - return g.exchangeEdges(e1index, e2index, switchType); - } - - Graph g; - std::mt19937 mt; - std::uniform_int_distribution<> edgeDistribution; - //std::uniform_int_distribution<> permutationDistribution; - std::bernoulli_distribution permutationDistribution; -}; - void getTriangleDegrees(const Graph& g) { std::vector> trids; const auto& adj = g.getAdj();