diff --git a/cpp/graph_powerlaw.hpp b/cpp/graph_powerlaw.hpp new file mode 100644 index 0000000000000000000000000000000000000000..9d6981eaef64d669f6d06bdf6f604698a532eaa7 --- /dev/null +++ b/cpp/graph_powerlaw.hpp @@ -0,0 +1,35 @@ +#pragma once +#include "graph.hpp" +#include "powerlaw.hpp" +#include +#include + +template +void generatePowerlawGraph(int n, float tau, Graph& g, DegreeSequence& ds, + RNG& rng) { + ds.resize(n); + powerlaw_distribution degDist(tau, 1, n); + + // Generate a graph + // might require multiple tries + for (int i = 1;; ++i) { + std::generate(ds.begin(), ds.end(), + [°Dist, &rng] { return degDist(rng); }); + // First make the sum even + unsigned int sum = std::accumulate(ds.begin(), ds.end(), 0); + if (sum % 2) { + continue; + // Can we do this: ?? + ds.back()++; + } + + if (g.createFromDegreeSequence(ds)) + break; + + // When 10 tries have not worked, output a warning + if (i % 10 == 0) { + std::cerr << "Warning: could not create graph from " + "degree sequence. Trying again...\n"; + } + } +}