Commit 3c0bfdb4 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

generate_sbm(): Slight performance improvement

parent a9a2ba7f
Pipeline #397 failed with stage
in 344 minutes and 23 seconds
......@@ -81,34 +81,33 @@ void gen_sbm(Graph& g, VProp b, IVec& rs, IVec& ss, FVec probs, VDProp in_deg,
size_t s = ss[i];
auto p = probs[i];
if (p == 0)
continue;
if (!is_dir && r == s)
p /= 2;
if (p > 0 && (r >= v_out_sampler.size() || v_out_sampler[r].empty() ||
s >= v_in_sampler.size() || v_in_sampler[s].empty()))
throw GraphException("Inconsistent SBM parameters: nonzero edge probabilities given for empty groups");
auto& r_sampler = v_out_sampler[r];
auto& s_sampler = v_in_sampler[s];
size_t ers;
size_t mrs;
if (micro_ers)
{
ers = p;
mrs = p;
}
else
{
std::poisson_distribution<> poi(p);
ers = poi(rng);
mrs = poi(rng);
}
for (size_t j = 0; j < ers; ++j)
size_t ers = (&r_sampler != &s_sampler) ? mrs : 2 * mrs;
if (!r_sampler.has_n(ers) || !s_sampler.has_n(ers))
throw GraphException("Inconsistent SBM parameters: node degrees do not agree with matrix of edge counts between groups");
for (size_t j = 0; j < mrs; ++j)
{
if (r_sampler.empty())
throw GraphException("Inconsistent SBM parameters: node degrees do not agree with matrix of edge counts between groups");
size_t u = r_sampler.sample(rng);
if (s_sampler.empty())
throw GraphException("Inconsistent SBM parameters: node degrees do not agree with matrix of edge counts between groups");
size_t v = s_sampler.sample(rng);
add_edge(u, v, g);
}
......
......@@ -94,8 +94,9 @@ public:
}
size_t size() const { return _items.size(); }
double prob_sum() const { return _S; }
bool empty() const { return _S == 0; }
bool has_n(size_t n) const { return (n == 0 || !empty()); }
double prob_sum() const { return _S; }
const Value& operator[](size_t i) const
{
......@@ -127,6 +128,7 @@ private:
vector<size_t> _alias;
uniform_int_distribution<size_t> _sample;
double _S;
size_t _size;
};
// uniform sampling from containers
......
......@@ -67,10 +67,9 @@ public:
}
}
bool empty()
{
return _urn.empty();
}
bool empty() const { return _urn.empty(); }
bool size() const { return _urn.size(); }
bool has_n(size_t n) const { return (n == 0 || size() >= (replacement ? 1 : n)); }
private:
vector<Value> _urn;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment