MinLCA algorithms
base.hh
Go to the documentation of this file.
1 #ifndef __MINLCA_MINLCA_BASE_HH
2 #define __MINLCA_MINLCA_BASE_HH
3 
4 #include <lemon/core.h>
5 #include <lemon/smart_graph.h>
6 #include <utils/graph_utils.hh>
7 #include <lemon/color.h>
8 
11 
15 namespace minlca
16 {
17 
24 };
25 
28 template<typename Graph = lemon::SmartGraph>
29 class MinLCA
30 {
31 public:
32  TEMPLATE_GRAPH_TYPEDEFS(Graph);
33  typedef IntNodeMap Colouring;
34 
35 protected:
36  const Graph &_g;
37  int _k;
38  int _max_k;
39  long long _lca;
40  Colouring _c;
42 
45  inline void setSolutionNotFound()
46  {
47  _s = SOLUTION_NOT_FOUND;
48  }
49 
52  inline void setSolutionFound()
53  {
54  _s = SOLUTION_FOUND;
55  }
56 
60  virtual inline void setColour(
61  const Node &v,
62  int vColour
63  )
64  {
65  int oldColour = _c[v];
66  _c[v] = vColour;
67  _k = std::max(_k, vColour);
68 
69  if (vColour == -1) {
70  this->setSolutionNotFound();
71  }
72  else {
73  for (IncEdgeIt e(_g, v); e != lemon::INVALID; ++e) {
74  Node n = _g.oppositeNode(v, e);
75  int nColour = _c[n];
76 
77  if (nColour != -1) {
78  if (oldColour != -1) {
79  _lca -= abs(nColour - oldColour);
80  }
81 
82  _lca += abs(nColour - vColour);
83  }
84  }
85  }
86  }
87 
93  const Graph &g,
94  int maxK = 0
95  )
96  : _g(g),
97  _k(-1),
98  _max_k(maxK > 0 ? maxK : 1 + utils::maxDegree(_g)),
99  _lca(0),
100  _c(g, -1),
101  _s(UNDEFINED)
102  {
103  }
104 
105 public:
112  virtual void init()
113  {
114  _k = -1;
115  _lca = 0;
116 
117  for (NodeIt v(_g); v != lemon::INVALID; ++v) {
118  _c[v] = -1;
119  }
120 
121  _s = UNDEFINED;
122  }
123 
127  {
128  return _s;
129  }
130 
138  virtual MinLCAStatus run()
139  {
140  _s = SOLUTION_NOT_FOUND;
141  return status();
142  }
143 
147  const Colouring &colouring() const
148  {
149  return _c;
150  }
151 
156  const Node &v
157  ) const
158  {
159  return _c[v];
160  }
161 
166  const Edge &e
167  ) const
168  {
169  return abs(_c[_g.u(e)] - _c[_g.v(e)]);
170  }
171 
175  inline long long lcaValue() const
176  {
177  return _lca;
178  }
179 
183  inline int totalColours() const
184  {
185  return _k + 1;
186  }
187 
191  inline long long recalculateLca()
192  {
193  _lca = 0;
194 
195  for (EdgeIt e(_g); e != lemon::INVALID; ++e) {
196  _lca += std::abs(_c[_g.u(e)] - _c[_g.v(e)]);
197  }
198 
199  return _lca;
200  }
201 
205  inline int maxK()
206  {
207  return _max_k;
208  }
209 };
210 
211 }
212 
213 #endif
MinLCAStatus _s
The status of the solution.
Definition: base.hh:41
long long lcaValue() const
Value (cost) of the current solution.
Definition: base.hh:175
virtual void init()
Reset the data structures of the algorithm.
Definition: base.hh:112
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
Definition: base.hh:60
MinLCAStatus
Possible status for MinLCA solution.
Definition: base.hh:20
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
Colouring _c
The vertex colouring.
Definition: base.hh:40
Solution has been found after running.
Definition: base.hh:22
int operator[](const Node &v) const
Colour of a vertex.
Definition: base.hh:155
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
Algorithm has not run yet, or some other status.
Definition: base.hh:21
Functions for reading graphs and getting graph properties.
int _max_k
Maximum number of colours allowed.
Definition: base.hh:38
Base class for MinLCA algorithms.
Definition: base.hh:29
int operator[](const Edge &e) const
Cost of an edge.
Definition: base.hh:165
int totalColours() const
Total number of colours used.
Definition: base.hh:183
Solution has not been found after running.
Definition: base.hh:23
const Colouring & colouring() const
Current colouring.
Definition: base.hh:147
void setSolutionNotFound()
Auxiliary function to set solution status to not found.
Definition: base.hh:45
long long recalculateLca()
Force a recalculation of the cost for the current solution.
Definition: base.hh:191
IntNodeMap Colouring
Type for graph colourings.
Definition: base.hh:33
MinLCAStatus status() const
Status of the solution.
Definition: base.hh:126
virtual MinLCAStatus run()
Run the algorithm.
Definition: base.hh:138
int maxK()
Maximum number of allowed colours.
Definition: base.hh:205
MinLCA(const Graph &g, int maxK=0)
Constructor for derived classes to use.
Definition: base.hh:92
long long _lca
Cost of the current solution.
Definition: base.hh:39
void setSolutionFound()
Auxiliary function to set solution status to found.
Definition: base.hh:52
int maxDegree(const Graph &g)
Maximum degree of a graph.
Definition: graph_utils.hh:181