MinLCA algorithms
binomial.hh
Go to the documentation of this file.
1 #ifndef __MINLCA_UTILS_GENERATORS_BINOMIAL_HH
2 #define __MINLCA_UTILS_GENERATORS_BINOMIAL_HH
3 
4 #include <lemon/smart_graph.h>
5 #include <lemon/random.h>
7 #include <algorithm>
8 
11 
12 namespace minlca
13 {
14 namespace utils
15 {
16 
18 template<typename BaseGraph = lemon::SmartGraph>
19 class BinomialRandom : public RandomGraph<BaseGraph>
20 {
21 protected:
24 
25 public:
26  TEMPLATE_GRAPH_TYPEDEFS(Graph);
27 
28 protected:
29  using Base::_r;
30  int _n;
31  double _p;
32 
33 public:
36  template<typename Number = int>
37  BinomialRandom(Number seed = 0)
38  : Base(seed)
39  {
40  }
41 
44  BinomialRandom(lemon::Random &r)
45  : Base(r)
46  {
47  }
48 
52  Graph &init(
53  int n,
54  double p
55  )
56  {
57  _n = n;
58  _p = std::max(0.0, std::min(p, 1.0));;
59 
60  return *this;
61  }
62 
64  double p() const
65  {
66  return _p;
67  }
68 
69  virtual void generate()
70  {
71  this->clear();
72  this->reserveNode(_n);
73  this->reserveEdge(static_cast<int>(0.5 * _p * _n * (_n - 1)));
74 
75  for (int u = 0; u < _n; ++u) {
76  this->addNode();
77  }
78 
79  for (int u = 0; u < _n; ++u)
80  for (int v = u + 1; v < _n; ++v)
81  if (_r->boolean(_p)) {
82  this->addEdge(this->nodeFromId(u), this->nodeFromId(v));
83  }
84  }
85 };
86 }
87 }
88 
89 #endif
Class defining binomial random graphs.
Definition: binomial.hh:19
double p() const
Retrieve the edge probability.
Definition: binomial.hh:64
BinomialRandom(lemon::Random &r)
Constructor with custom random number generator.
Definition: binomial.hh:44
Graph & init(int n, double p)
Initialise the generator.
Definition: binomial.hh:52
virtual void generate()
Generate graph.
Definition: binomial.hh:69
lemon::Random * _r
Random number generator.
Definition: random-graph.hh:19
void seed(Number seed)
Change the random number generator seed.
Definition: random-graph.hh:56
double _p
Edge probability.
Definition: binomial.hh:31
Contains base definitions for defining particular random graphs.
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
BinomialRandom(Number seed=0)
Constructor with seed.
Definition: binomial.hh:37
int _n
Number of vertices.
Definition: binomial.hh:30
Base class for random graph generators.
Definition: random-graph.hh:16