1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/**
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux
*
* Tulip is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tulip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
#include <algorithm>
#include <tulip/TulipPluginHeaders.h>
using namespace std;
using namespace tlp;
class CompNodes {
public :
Graph * graph;
CompNodes(Graph *g):graph(g) {
}
bool operator()(const node u, const node v) {
unsigned int du = graph->deg(u), dv = graph->deg(v);
if (du == dv)
return u.id > v.id;
return du > dv;
}
};
/*@{*/
/** \file
* \brief An implementation of the Welsh and Powell algorithm
*
* This plugin is an implementation of the Welsh and Powell
* graph coloring algorithm. It computes a set of integer measures
* for the nodes of a graph in such a way that the measures assigned to 2
* adjacent nodes are always different.
*
* (see http://en.wikipedia.org/wiki/Graph_coloring for more details)
*
* <b>HISTORY</b>
*
* - 2005 Version 1.0: Initial release
* by David Auber, LaBRI, University Bordeaux I, France
*
*
*/
class WelshPowell:public DoubleAlgorithm {
public:
PLUGININFORMATION("Welsh & Powell","David Auber","03/01/2005","Nodes coloring measure,<br/>values assigned to adjacent nodes are always different.","1.0", "Graph")
WelshPowell(const tlp::PluginContext *context):DoubleAlgorithm(context) {}
bool hasNeighbourColoredWith(const node n, const int color) {
node u;
forEach(u, graph->getInOutNodes(n))
if (result->getNodeValue(u) == color)
return true;
return false;
}
void colorize() {
vector<node> toSort(graph->numberOfNodes());
node n;
unsigned int i = 0;
forEach(n,graph->getNodes())
toSort[i++]=n;
CompNodes cmp(graph);
sort(toSort.begin(),toSort.end(),cmp);
result->setAllNodeValue(-1);
int currentColor = 0;
unsigned int numberOfColoredNodes = 0;
unsigned int minIndex = 0;
unsigned int maxIndex = toSort.size();
while (numberOfColoredNodes != graph->numberOfNodes()) {
#ifndef NDEBUG
cout << "nbColored :" << numberOfColoredNodes << endl;
#endif
unsigned int nextMaxIndex = minIndex;
for(unsigned int i= minIndex; i < maxIndex; ++i) {
#ifndef NDEBUG
cout << "i:" << i << endl;
#endif
node n = toSort[i];
if (result->getNodeValue(n) == -1) {
if (!hasNeighbourColoredWith(n, currentColor)) {
#ifndef NDEBUG
cout << "new node found color : " << currentColor << endl;
#endif
result->setNodeValue(toSort[i], currentColor);
++numberOfColoredNodes;
if (i == minIndex)
++minIndex;
}
else
nextMaxIndex = i + 1;
}
else if (i == minIndex)
++minIndex;
}
maxIndex = nextMaxIndex;
++currentColor;
}
}
bool run() {
colorize();
return true;
}
};
/*@}*/
PLUGIN(WelshPowell)
|