MinLCA algorithms
greedy.hh
Go to the documentation of this file.
1 #ifndef __MINLCA_MINLCA_GREEDY_HH
2 #define __MINLCA_MINLCA_GREEDY_HH
3 
4 #include <minlca/base.hh>
5 #include <list>
6 #include <vector>
7 #include <lemon/connectivity.h>
8 #include <lemon/bfs.h>
9 #include <utils/utils.hh>
10 #include <iostream>
11 #include <limits>
12 #include <cstdlib>
13 
16 
17 namespace minlca
18 {
19 
22 template<typename Graph>
23 class MinLCAGreedy : public MinLCA<Graph>
24 {
25 public:
26  TEMPLATE_GRAPH_TYPEDEFS(Graph);
27 
28 protected:
29  typedef MinLCA<Graph> Base;
30  using Base::_g;
31  using Base::_c;
32  using Base::_max_k;
33  using Base::_lca;
34  using Base::_k;
35 
38  MinLCAGreedy(const Graph &g, int maxK = 0) : Base(g, maxK)
39  {
40  }
41 
43  virtual void paintVertex(
44  const Node &v
45  ) = 0;
46 };
47 
48 
51 template<template<typename> class Greedy, typename Graph = lemon::SmartGraph>
53  : public Greedy<Graph>,
54  private lemon::BfsVisitor<Graph>
55 {
56 public:
57  TEMPLATE_GRAPH_TYPEDEFS(Graph);
58 
59 protected:
60  typedef Greedy<Graph> BaseGreedy;
62 
63  using BaseGreedy::_g;
64 
65  std::list<Node> _sources;
66  lemon::BfsVisit<Graph, Visitor> *_bfs;
67 
68  friend class lemon::BfsVisit<Graph, Visitor>;
69 
70 public:
74  MinLCAGreedyBfs(const Graph &g, int maxK = 0)
75  : BaseGreedy(g, maxK), _bfs(NULL)
76  {
77  _bfs = new lemon::BfsVisit<Graph, Visitor>(_g, *this);
78  }
79 
81  virtual ~MinLCAGreedyBfs()
82  {
83  delete _bfs;
84  }
85 
86  virtual void init()
87  {
88  BaseGreedy::init();
89  _sources.clear();
91  _bfs->init();
92  }
93 
102  const std::list<Node> &sources
103  )
104  {
105  _sources = sources;
106  }
107 
115  const Node &v
116  )
117  {
118  _sources.clear();
119  _sources.push_back(v);
120  }
121 
122  virtual MinLCAStatus run()
123  {
124  for (Node source : _sources) {
125  _bfs->addSource(source);
126  }
127 
128  _bfs->start();
129 
130  if (this->status() != SOLUTION_NOT_FOUND) {
131  this->setSolutionFound();
132  }
133 
134  return this->status();
135  }
136 
137 private:
142  {
143  IntNodeMap cc(_g);
144  int numCc = lemon::connectedComponents(_g, cc);
145  std::vector<bool> withNode(numCc, false);
146 
147  for (NodeIt v(_g); v != lemon::INVALID; ++v) {
148  if (not withNode[cc[v]]) {
149  _sources.push_back(v);
150  withNode[cc[v]] = true;
151  }
152  }
153  }
154 
163  void process(const Node &v)
164  {
165  this->paintVertex(v);
166  }
167 };
168 
174 template<typename Graph = lemon::SmartGraph>
176  : public MinLCAGreedy<Graph>
177 {
178  TEMPLATE_GRAPH_TYPEDEFS(Graph);
179 
180 protected:
182  using Base::_g;
183  using Base::_c;
184  using Base::_max_k;
185  using Base::_lca;
186  using Base::_k;
187 
192  MinLCAGreedyNearest(const Graph &g, int maxK = 0) : Base(g, maxK)
193  {
194  }
195 
196  virtual void paintVertex(const Node &v)
197  {
198  std::vector<int> colours(_max_k, 0);
199 
200  for (IncEdgeIt e(_g, v); e != lemon::INVALID; ++e) {
201  Node n = _g.oppositeNode(v, e);
202  int nColour = _c[n];
203 
204  if (nColour != -1) {
205  colours[nColour] = -1;
206 
207  if (nColour - 1 >= 0 and colours[nColour - 1] != -1) {
208  ++colours[nColour - 1];
209  }
210 
211  if (nColour + 1 < _max_k and colours[nColour + 1] != -1) {
212  ++colours[nColour + 1];
213  }
214  }
215  }
216 
217  int vColour = utils::posMax(colours);
218 
219  if (colours[vColour] == -1) {
220  vColour = -1;
221  }
222 
223  this->setColour(v, vColour);
224  }
225 };
226 
232 template<typename Graph = lemon::SmartGraph>
234  : public MinLCAGreedy<Graph>
235 {
236  TEMPLATE_GRAPH_TYPEDEFS(Graph);
237 protected:
239  typedef long long ll;
240  using Base::_g;
241  using Base::_c;
242  using Base::_max_k;
243  using Base::_lca;
244  using Base::_k;
245 
250  MinLCAGreedyLeastCost(const Graph &g, int maxK = 0) : Base(g, maxK)
251  {
252  }
253 
254  virtual void paintVertex(const Node &v)
255  {
256  std::vector<int> colourCount(_max_k, 0);
257 
258  for (IncEdgeIt e(_g, v); e != lemon::INVALID; ++e) {
259  Node n = _g.oppositeNode(v, e);
260  int nColour = _c[n];
261 
262  if (nColour != -1) {
263  ++colourCount[nColour];
264  }
265  }
266 
267  int vColour = -1;
268  ll vColourCost = std::numeric_limits<ll>::max();
269 
270  for (int c = 0; c < _max_k; ++c)
271  if (colourCount[c] == 0) {
272  ll currentCost = 0;
273 
274  for (int nColour = 0; nColour != _max_k; ++nColour) {
275  currentCost += ll(std::abs(nColour - c)) * ll(colourCount[nColour]);
276  }
277 
278  if (currentCost < vColourCost) {
279  vColourCost = currentCost;
280  vColour = c;
281  }
282  }
283 
284  this->setColour(v, vColour);
285  }
286 };
287 
289 template<typename Graph>
290 #ifndef USING_NOT_AVAILABLE
292 #else
294  : public MinLCAGreedyBfs<MinLCAGreedyNearest, Graph>
295 {
297 public:
298  MinLCAGreedyNearestBfs(const Graph &g, int maxK = 0) : Base(g, maxK)
299  {
300  }
301 };
302 #endif
303 
305 template<typename Graph>
306 #ifndef USING_NOT_AVAILABLE
308 #else
310  : public MinLCAGreedyBfs<MinLCAGreedyLeastCost, Graph>
311 {
313 public:
314  MinLCAGreedyLeastCostBfs(const Graph &g, int maxK = 0) : Base(g, maxK)
315  {
316  }
317 };
318 #endif
319 }
320 
321 #endif
MinLCAGreedy< Graph > Base
The greedy base class.
Definition: greedy.hh:181
MinLCAGreedyBfs< Greedy, Graph > Visitor
This class.
Definition: greedy.hh:61
MinLCAGreedyLeastCost(const Graph &g, int maxK=0)
Constructor.
Definition: greedy.hh:250
Greedy< Graph > BaseGreedy
The greedy algorithm.
Definition: greedy.hh:60
std::list< Node > _sources
List of source nodes for BFS.
Definition: greedy.hh:65
lemon::BfsVisit< Graph, Visitor > * _bfs
Pointer to the visitor class.
Definition: greedy.hh:66
MinLCAGreedyBfs< MinLCAGreedyLeastCost, Graph > MinLCAGreedyLeastCostBfs
Class defined to ease the use of MinLCAGreedyLeastCost with Bfs.
Definition: greedy.hh:307
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
Definition: base.hh:60
virtual void paintVertex(const Node &v)
Assign a colour to a vertex using a greedy approach.
Definition: greedy.hh:196
void setSourceNodes(const std::list< Node > &sources)
Set source vertices.
Definition: greedy.hh:101
virtual void paintVertex(const Node &v)
Assign a colour to a vertex using a greedy approach.
Definition: greedy.hh:254
MinLCAStatus
Possible status for MinLCA solution.
Definition: base.hh:20
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
MinLCAGreedyBfs< MinLCAGreedyNearest, Graph > MinLCAGreedyNearestBfs
Class defined to ease the use of MinLCAGreedyNearest with Bfs.
Definition: greedy.hh:291
int _k
Largest index of the colours used.
Definition: base.hh:37
Greedy nearest colour approach for MinLCA.
Definition: greedy.hh:233
Colouring _c
The vertex colouring.
Definition: base.hh:40
void setSourceNode(const Node &v)
Set source node.
Definition: greedy.hh:114
void process(const Node &v)
Process vertex.
Definition: greedy.hh:163
virtual void paintVertex(const Node &v)=0
Assign a colour to a vertex using a greedy approach.
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
Class for greedy algorithms using a breadth-first search.
Definition: greedy.hh:52
Defines the base class for MinLCA algorithms.
MinLCA< Graph > Base
The base class, for shorter reference.
Definition: greedy.hh:29
int _max_k
Maximum number of colours allowed.
Definition: base.hh:38
Base class for MinLCA algorithms.
Definition: base.hh:29
MinLCAGreedy< Graph > Base
The greedy base class.
Definition: greedy.hh:238
MinLCAGreedyNearest(const Graph &g, int maxK=0)
Constructor.
Definition: greedy.hh:192
MinLCAGreedy(const Graph &g, int maxK=0)
Constructor for derived classes to use.
Definition: greedy.hh:38
virtual ~MinLCAGreedyBfs()
Destructor.
Definition: greedy.hh:81
Solution has not been found after running.
Definition: base.hh:23
int posMax(const std::vector< T > &v)
Index of the maximum position.
Definition: utils.hh:25
MinLCAGreedyBfs(const Graph &g, int maxK=0)
Constructor for the algorithm class.
Definition: greedy.hh:74
int maxK()
Maximum number of allowed colours.
Definition: base.hh:205
Utils not related to graphs.
long long _lca
Cost of the current solution.
Definition: base.hh:39
void connectedComponentsSourceNodes()
Set sources to one vertex of each connected component.
Definition: greedy.hh:141
Greedy nearest colour approach for MinLCA.
Definition: greedy.hh:175