MinLCA algorithms
geometric.hh
Go to the documentation of this file.
1 #ifndef __MINLCA_UTILS_GENERATORS_GEOMETRIC_HH
2 #define __MINLCA_UTILS_GENERATORS_GEOMETRIC_HH
3 
4 #include <lemon/smart_graph.h>
5 #include <lemon/random.h>
6 #include "random-graph.hh"
7 
10 
11 namespace minlca
12 {
13 namespace utils
14 {
15 
20 template<typename BaseGraph = lemon::SmartGraph>
21 class RandomGeometric : public RandomGraph<BaseGraph>
22 {
23 protected:
26 
27 public:
28  TEMPLATE_GRAPH_TYPEDEFS(Graph);
29 
30 protected:
31  typedef typename Graph::template NodeMap<lemon::dim2::Point<double>> Coords;
32  using Base::_r;
33  Coords _coords;
34  int _n;
35  double _rad_sq;
36 
37 public:
40  template<typename Number = int>
41  RandomGeometric(Number seed = 0)
42  : Base(seed), _coords(*this)
43  {
44  }
45 
48  RandomGeometric(lemon::Random &r)
49  : Base(r), _coords(*this)
50  {
51  }
52 
54  Graph &init(
55  int n,
56  double radSq
57  )
58  {
59  _n = n;
60  _rad_sq = radSq;
61 
62  return *this;
63  }
64 
65  virtual void generate()
66  {
67  this->clear();
68  this->reserveNode(_n);
69 
70  for (int i = 0; i < _n; ++i) {
71  Node v = this->addNode();
72  _coords[v] = _r->disc();
73  }
74 
75  for (int i = 0; i < _n; ++i) {
76  Node u = this->nodeFromId(i);
77 
78  for (int j = i + 1; j < _n; ++j) {
79  Node v = this->nodeFromId(j);
80 
81  if ((_coords[v] - _coords[u]).normSquare() < _rad_sq) {
82  this->addEdge(u, v);
83  }
84  }
85  }
86  }
87 
91  lemon::dim2::Point<double> coord(
92  const Node &v
93  ) const
94  {
95  return _coords[v];
96  }
97 
101  const Coords &coords() const
102  {
103  return _coords;
104  }
105 };
106 }
107 }
108 
109 #endif
int _n
Number of vertices.
Definition: geometric.hh:34
lemon::dim2::Point< double > coord(const Node &v) const
Retrieve coordinates for a vertex.
Definition: geometric.hh:91
RandomGeometric(lemon::Random &r)
Constructor with custom random number generator.
Definition: geometric.hh:48
virtual void generate()
Generate graph.
Definition: geometric.hh:65
Class defining random geometric graphs.
Definition: geometric.hh:21
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
Contains base definitions for defining particular random graphs.
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
Graph & init(int n, double radSq)
Initialise the generator.
Definition: geometric.hh:54
RandomGeometric(Number seed=0)
Constructor with seed.
Definition: geometric.hh:41
Coords _coords
Coordinates of the vertices.
Definition: geometric.hh:33
double _rad_sq
Square of the radius.
Definition: geometric.hh:35
const Coords & coords() const
Retrieve all the coordinates.
Definition: geometric.hh:101
Base class for random graph generators.
Definition: random-graph.hh:16