1 #ifndef __MINLCA_MINLCA_GREEDY_HH
2 #define __MINLCA_MINLCA_GREEDY_HH
7 #include <lemon/connectivity.h>
22 template<
typename Graph>
26 TEMPLATE_GRAPH_TYPEDEFS(Graph);
51 template<
template<
typename>
class Greedy,
typename Graph = lemon::SmartGraph>
53 :
public Greedy<Graph>,
54 private lemon::BfsVisitor<Graph>
57 TEMPLATE_GRAPH_TYPEDEFS(Graph);
66 lemon::BfsVisit<Graph, Visitor> *
_bfs;
68 friend class lemon::BfsVisit<Graph, Visitor>;
75 : BaseGreedy(g, maxK), _bfs(NULL)
77 _bfs =
new lemon::BfsVisit<Graph, Visitor>(_g, *
this);
102 const std::list<Node> &sources
119 _sources.push_back(v);
124 for (Node source : _sources) {
125 _bfs->addSource(source);
131 this->setSolutionFound();
134 return this->status();
144 int numCc = lemon::connectedComponents(_g, cc);
145 std::vector<bool> withNode(numCc,
false);
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;
165 this->paintVertex(v);
174 template<
typename Graph = lemon::SmartGraph>
178 TEMPLATE_GRAPH_TYPEDEFS(Graph);
198 std::vector<int> colours(
_max_k, 0);
200 for (IncEdgeIt e(
_g, v); e != lemon::INVALID; ++e) {
201 Node n =
_g.oppositeNode(v, e);
205 colours[nColour] = -1;
207 if (nColour - 1 >= 0 and colours[nColour - 1] != -1) {
208 ++colours[nColour - 1];
211 if (nColour + 1 <
_max_k and colours[nColour + 1] != -1) {
212 ++colours[nColour + 1];
219 if (colours[vColour] == -1) {
232 template<
typename Graph = lemon::SmartGraph>
236 TEMPLATE_GRAPH_TYPEDEFS(Graph);
239 typedef long long ll;
256 std::vector<int> colourCount(
_max_k, 0);
258 for (IncEdgeIt e(
_g, v); e != lemon::INVALID; ++e) {
259 Node n =
_g.oppositeNode(v, e);
263 ++colourCount[nColour];
268 ll vColourCost = std::numeric_limits<ll>::max();
270 for (
int c = 0; c <
_max_k; ++c)
271 if (colourCount[c] == 0) {
274 for (
int nColour = 0; nColour !=
_max_k; ++nColour) {
275 currentCost += ll(std::abs(nColour - c)) * ll(colourCount[nColour]);
278 if (currentCost < vColourCost) {
279 vColourCost = currentCost;
289 template<
typename Graph>
290 #ifndef USING_NOT_AVAILABLE
305 template<
typename Graph>
306 #ifndef USING_NOT_AVAILABLE
MinLCAGreedy< Graph > Base
The greedy base class.
MinLCAGreedyBfs< Greedy, Graph > Visitor
This class.
MinLCAGreedyLeastCost(const Graph &g, int maxK=0)
Constructor.
Greedy< Graph > BaseGreedy
The greedy algorithm.
std::list< Node > _sources
List of source nodes for BFS.
lemon::BfsVisit< Graph, Visitor > * _bfs
Pointer to the visitor class.
MinLCAGreedyBfs< MinLCAGreedyLeastCost, Graph > MinLCAGreedyLeastCostBfs
Class defined to ease the use of MinLCAGreedyLeastCost with Bfs.
virtual void setColour(const Node &v, int vColour)
Set the colour of a vertex.
virtual void paintVertex(const Node &v)
Assign a colour to a vertex using a greedy approach.
void setSourceNodes(const std::list< Node > &sources)
Set source vertices.
virtual void paintVertex(const Node &v)
Assign a colour to a vertex using a greedy approach.
MinLCAStatus
Possible status for MinLCA solution.
Base class for greedy algorithms.
const Graph & _g
A const reference to the graph we want to arrange.
MinLCAGreedyBfs< MinLCAGreedyNearest, Graph > MinLCAGreedyNearestBfs
Class defined to ease the use of MinLCAGreedyNearest with Bfs.
int _k
Largest index of the colours used.
Greedy nearest colour approach for MinLCA.
Colouring _c
The vertex colouring.
void setSourceNode(const Node &v)
Set source node.
void process(const Node &v)
Process vertex.
virtual void paintVertex(const Node &v)=0
Assign a colour to a vertex using a greedy approach.
Default namespace Default namespace for MinLCA algorithms.
Class for greedy algorithms using a breadth-first search.
Defines the base class for MinLCA algorithms.
MinLCA< Graph > Base
The base class, for shorter reference.
int _max_k
Maximum number of colours allowed.
Base class for MinLCA algorithms.
MinLCAGreedy< Graph > Base
The greedy base class.
MinLCAGreedyNearest(const Graph &g, int maxK=0)
Constructor.
MinLCAGreedy(const Graph &g, int maxK=0)
Constructor for derived classes to use.
virtual ~MinLCAGreedyBfs()
Destructor.
Solution has not been found after running.
int posMax(const std::vector< T > &v)
Index of the maximum position.
MinLCAGreedyBfs(const Graph &g, int maxK=0)
Constructor for the algorithm class.
int maxK()
Maximum number of allowed colours.
Utils not related to graphs.
long long _lca
Cost of the current solution.
void connectedComponentsSourceNodes()
Set sources to one vertex of each connected component.
Greedy nearest colour approach for MinLCA.