MinLCA algorithms
greedy-random.hh
Go to the documentation of this file.
1 #ifndef __MINLCA_MINLCA_GREEDY_RANDOM_HH
2 #define __MINLCA_MINLCA_GREEDY_RANDOM_HH
3 
4 #include <minlca/greedy.hh>
5 #include <lemon/random.h>
6 
9 
10 namespace minlca
11 {
12 
19 template<typename Graph = lemon::SmartGraph>
21  : public MinLCAGreedy<Graph>
22 {
23 
24 protected:
26  typedef MinLCAGreedy<Graph> Base;
27  using Base::_g;
28  using Base::_c;
29  using Base::_max_k;
30  using Base::_lca;
31  using Base::_k;
32 
33  double _p;
34  lemon::Random _r;
35 
36  TEMPLATE_GRAPH_TYPEDEFS(Graph);
37 
42  template<typename Number = unsigned long long>
44  const Graph &g,
45  int maxK = 0,
46  double p = 0.0,
47  Number seed = 0ULL
48  )
49  : Base(g, maxK), _p(p), _r(seed)
50  {
51  }
52 
53  virtual void paintVertex(const Node &v)
54  {
55  int available = _max_k;
56 
57  std::vector<int> colours(_max_k, 0);
58 
59  for (IncEdgeIt e(_g, v); e != lemon::INVALID; ++e) {
60  Node n = _g.oppositeNode(v, e);
61  int nColour = _c[n];
62 
63  if (nColour != -1) {
64  colours[nColour] = -1;
65 
66  if (nColour - 1 >= 0 and colours[nColour - 1] != -1) {
67  ++colours[nColour - 1];
68  }
69 
70  if (nColour + 1 < _max_k and colours[nColour + 1] != -1) {
71  ++colours[nColour + 1];
72  }
73  }
74  }
75 
76  int vColour = utils::posMax(colours);
77  colours[vColour] = -1;
78 
79  std::vector<int> nonNeg = utils::nonNegative(colours);
80 
81  if (nonNeg.size() > 0 and _r.boolean(_p)) {
82  vColour = nonNeg[_r.integer(0, static_cast<int>(nonNeg.size()))];
83  }
84 
85  this->setColour(v, vColour);
86  }
87 
88 public:
90  void p(
91  double p
92  )
93  {
94  _p = p;
95  }
96 
98  template<typename Number>
99  void seed(
100  Number seed
101  )
102  {
103  _r.seed(seed);
104  }
105 };
106 
108 template<typename Graph>
109 #ifndef USING_NOT_AVAILABLE
112 #else
114  : public MinLCAGreedyBfs<MinLCAGreedyNearestRnd, Graph>
115 {
117 public:
118  template<typename Number = unsigned long long>
119  MinLCAGreedyNearestRndBfs(const Graph &g, int maxK = 0, double p = 0.0,
120  Number seed = 0ULL)
121  : Base(g, maxK)
122  {
123  }
124 };
125 #endif
126 
127 }
128 
129 #endif
std::vector< int > nonNegative(const std::vector< int > &v)
Vector of non-negative indices.
Definition: utils.hh:33
MinLCAGreedyNearestRnd(const Graph &g, int maxK=0, double p=0.0, Number seed=0ULL)
Constructor.
MinLCAGreedyBfs< MinLCAGreedyNearestRnd, Graph > MinLCAGreedyNearestRndBfs
Class defined to ease the use of MinLCAGreedyNearestRnd with Bfs.
void p(double p)
Change the probability of choosing a random colour.
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
Definition: base.hh:60
double _p
Probability of choosing a random colour.
Base class for greedy algorithms.
Definition: greedy.hh:23
const Graph & _g
A const reference to the graph we want to arrange.
Definition: base.hh:36
int _k
Largest index of the colours used.
Definition: base.hh:37
lemon::Random _r
Random number generator.
Colouring _c
The vertex colouring.
Definition: base.hh:40
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
Class for greedy algorithms using a breadth-first search.
Definition: greedy.hh:52
int _max_k
Maximum number of colours allowed.
Definition: base.hh:38
void seed(Number seed)
Change the seed for the internal random number generator.
int posMax(const std::vector< T > &v)
Index of the maximum position.
Definition: utils.hh:25
int maxK()
Maximum number of allowed colours.
Definition: base.hh:205
long long _lca
Cost of the current solution.
Definition: base.hh:39
Randomised greedy nearest colour approach for MinLCA.
virtual void paintVertex(const Node &v)
Assign a colour to a vertex using a greedy approach.