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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
/**
*
* 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 "StrengthClustering.h"
using namespace std;
using namespace tlp;
PLUGIN(StrengthClustering)
//================================================================================
StrengthClustering::~StrengthClustering() {}
//==============================================================================
double StrengthClustering::computeMQValue(const vector<set<node> > & partition, Graph *sg) {
vector<unsigned int> nbIntraEdges(partition.size());
for (unsigned int i = 0; i<partition.size(); ++i)
nbIntraEdges[i] = 0;
map<pair<unsigned int, unsigned int>, unsigned int > nbExtraEdges;
MutableContainer<unsigned int> clusterId;
vector< set<node> >::const_iterator itPart = partition.begin();
for (unsigned int i=0; itPart!=partition.end(); ++itPart, ++i) {
set<node>::const_iterator itSet = itPart->begin();
for (; itSet!=itPart->end(); ++itSet) {
clusterId.set(itSet->id, i);
}
}
Iterator<edge> *itE = sg->getEdges();
while(itE->hasNext()) {
edge e = itE->next();
const pair<node, node>& eEnds = sg->ends(e);
node n1 = eEnds.first;
node n2 = eEnds.second;
if (n1.id >= n2.id) {
n1 = n2;
n2 = eEnds.first;
}
unsigned int n1ClustId = clusterId.get(n1.id);
unsigned int n2ClustId = clusterId.get(n2.id);
if ( n1ClustId == n2ClustId)
nbIntraEdges[n1ClustId] += 1;
else {
pair<unsigned int, unsigned int> pp(n1ClustId, n2ClustId);
if (nbExtraEdges.find(pp) != nbExtraEdges.end()) {
nbExtraEdges[pp] += 1;
}
else {
nbExtraEdges[pp] = 1;
}
}
}
delete itE;
double positive = 0;
for (unsigned int i=0; i<partition.size(); ++i) {
if (partition[i].size() > 1)
positive += 2.0 * double(nbIntraEdges[i]) / double(partition[i].size() * (partition[i].size() - 1));
}
positive /= double(partition.size());
double negative=0;
map<pair<unsigned int, unsigned int>, unsigned int >::const_iterator itMap = nbExtraEdges.begin();
for (; itMap != nbExtraEdges.end(); ++itMap) {
pair<unsigned int, unsigned int> pp = itMap->first;
unsigned int val = itMap->second;
if ( !partition[pp.first].empty() && !partition[pp.second].empty())
negative += double(val) / double(partition[pp.first].size() * partition[pp.second].size());
}
if (partition.size()>1)
negative /= double(partition.size()*(partition.size()-1)) / 2.0;
double result = positive - negative;
return result;
}
//==============================================================================
void StrengthClustering::computeNodePartition(double threshold,
vector<set<node> >& result ) {
//tlp::warning() << __PRETTY_FUNCTION__ << endl;
Graph *tmpGraph = graph->addCloneSubGraph();
StableIterator<edge> itE(graph->getEdges());
while (itE.hasNext()) {
edge ite=itE.next();
if (values->getEdgeValue(ite)<threshold) {
const pair<node, node>& eEnds = graph->ends(ite);
if (graph->deg(eEnds.first)>1 && graph->deg(eEnds.second)>1)
tmpGraph->delEdge(ite);
}
}
// Select SubGraph singleton in graph
set<node> singleton;
StableIterator<node> itN(tmpGraph->getNodes());
while (itN.hasNext()) {
node itn=itN.next();
if (tmpGraph->deg(itn)==0) singleton.insert(itn);
}
// restore edges to reconnect singleton by computing induced subgraph
StableIterator<edge> itE2(graph->getEdges());
while (itE2.hasNext()) {
edge ite=itE2.next();
const pair<node, node>& eEnds = graph->ends(ite);
if (singleton.find(eEnds.first)!=singleton.end() &&
singleton.find(eEnds.second)!=singleton.end()) {
tmpGraph->addEdge(ite);
}
}
//Extract connected component
DoubleProperty connected(tmpGraph);
string errMsg;
tmpGraph->applyPropertyAlgorithm("Connected Component", &connected, errMsg);
//Compute the node partition
int index=0;
map<double,int> resultIndex;
Iterator<node> *itN2=tmpGraph->getNodes();
while (itN2->hasNext()) {
node itn=itN2->next();
const double& val=connected.getNodeValue(itn);
if (resultIndex.find(val)!=resultIndex.end())
result[resultIndex[val]].insert(itn);
else {
set<node> tmp;
result.push_back(tmp);
resultIndex[val]=index;
result[index].insert(itn);
++index;
}
}
delete itN2;
graph->delAllSubGraphs(tmpGraph);
}
//==============================================================================
//void drawGraph(Graph *tmpg) {
// // tlp::warning() << __PRETTY_FUNCTION__ << endl;
// string errMsg;
// string layoutName;
// if (tmpg->numberOfNodes() > 300)
// layoutName = "Circular";
// else
// layoutName = "GEM (Frick)";
// string sizesName="Auto Sizing";
// tmpg->computeProperty(layoutName,tmpg->getLocalProperty<LayoutProperty>("viewLayout"),errMsg);
// if (tmpg->numberOfNodes() < 300)
// tmpg->computeProperty(sizesName,tmpg->getLocalProperty<SizeProperty>("viewSize"),errMsg);
//}
//==============================================================================
double StrengthClustering::findBestThreshold(int numberOfSteps, bool& stopped) {
double maxMQ=-2;
double threshold = values->getEdgeMin(graph);
double deltaThreshold = (values->getEdgeMax(graph)-values->getEdgeMin(graph))/double(numberOfSteps);
int steps = 0;
for (double i=values->getEdgeMin(graph); i<values->getEdgeMax(graph); i+=deltaThreshold) {
vector< set<node > > tmp;
computeNodePartition(i, tmp);
if (pluginProgress && ((++steps % (numberOfSteps / 10)) == 0)) {
pluginProgress->progress(steps, numberOfSteps);
if ((stopped = (pluginProgress->state() !=TLP_CONTINUE))) {
return threshold;
}
}
double mq = computeMQValue(tmp, graph);
if ( mq > maxMQ) {
threshold=i;
maxMQ=mq;
}
}
// tlp::debug() << __PRETTY_FUNCTION__ << endl;
// tlp::debug() << "\t" <<" Nb try : " << numberOfSteps << endl;
// tlp::debug() << "\t" <<" Max MQ : " << maxMQ << endl << flush;
// tlp::debug() << "\t" <<" Best threshold : " << threshold << endl << flush;
return threshold;
}
//==============================================================================
//Graph* StrengthClustering::buildSubGraphs(const vector< set<node > > &partition) {
// if (partition.size()<2) return graph;
// Graph *tmpGraph=tlp::newCloneSubGraph(graph);
// stringstream sstr;
// sstr << "clone of ";
// string graphName = graph->getAttribute<string>("name");
// if (graphName.size() == 0)
// sstr << graph->getId();
// else
// sstr << graphName;
// tmpGraph->setAttribute(string("name"), sstr.str());
// unsigned int step = partition.size() / 10;
// for (unsigned int i=0;i<partition.size();++i) {
// if (pluginProgress && step && ((i % step) == 0)) {
// pluginProgress->progress(i, partition.size());
// if (pluginProgress->state() !=TLP_CONTINUE) {
// graph->delSubGraph(tmpGraph);
// return 0;
// }
// }
// tmpGraph->inducedSubGraph(partition[i]);
// }
// return tmpGraph;
//}
//==============================================================================
//bool StrengthClustering::recursiveCall(Graph *rootGraph) {
// Iterator<Graph*> *itS = rootGraph->getSubGraphs();
// while(itS->hasNext()) {
// Graph *sg=itS->next();
// Graph *tmpGr = sg;
// if (sg->numberOfNodes() > 10) {
// double avPath;
// if (pluginProgress)
// pluginProgress->setComment("Computing average path length on subgraphs");
// if (!tlp::averagePathLength(sg, avPath, pluginProgress))
// return false;
// double avCluster;
// if (pluginProgress)
// pluginProgress->setComment("Computing average cluster on subgraphs");
// if (!tlp::averageCluster(sg, avCluster, pluginProgress))
// return false;
// /*
// tlp::debug() << "Average Path Length :" << avPath << endl;
// tlp::debug() << "Average clustering :" << avCluster << endl;
// tlp::debug() << "Number of nodes :" << tmpg->numberOfNodes() << endl;
// tlp::debug() << "Number of edges :" << tmpg->numberOfEdges() << endl;
// */
// if ( avPath > 1 && avPath < 4 && avCluster > 0.25) {
// DataSet tmpData;
// string errMsg;
// //pluginProgress->setComment("Computing strength clustering on subgraphs...");
// // propagate values for layout parameters
// tmpData.set("layout subgraphs", subgraphsLayout);
// tmpData.set("layout quotient graph", quotientLayout);
// if (!tlp::applyAlgorithm(sg, errMsg, &tmpData, "Strength Clustering",
// pluginProgress)) {
// return false;
// }
// tmpData.get("strengthGraph",tmpGr);
// }
// }
// if (subgraphsLayout && sg==tmpGr) {
// drawGraph(sg);
// }
// } delete itS;
// return true;
//}
//==============================================================================
//Graph* StrengthClustering::buildQuotientGraph(Graph *sg) {
// DataSet tmpData;
// string errMsg;
// if (!tlp::applyAlgorithm(sg, errMsg, &tmpData, "Quotient Clustering",
// pluginProgress))
// return 0;
// Graph *quotientGraph = NULL;
// tmpData.get<Graph *>("quotientGraph", quotientGraph);
// vector<edge> toRemoved;
// SimpleTest::makeSimple(quotientGraph, toRemoved);
// for(vector<edge>::iterator it= toRemoved.begin(); it!=toRemoved.end(); ++it)
// quotientGraph->delAllEdge(*it);
// if (quotientLayout)
// drawGraph(quotientGraph);
// return quotientGraph;
//}
//==============================================================================
namespace {
const char * paramHelp[] = {
// metric
HTML_HELP_OPEN() \
HTML_HELP_DEF( "type", "DoubleProperty" ) \
HTML_HELP_DEF( "value", "An existing metric property" ) \
HTML_HELP_BODY() \
"Metric used in order to multiply strength metric computed values."\
"If one is given, the complexity is O(n log(n)), O(n) neither." \
HTML_HELP_CLOSE(),
// do you mean "else it will be O(n)" instead of "O(n) neither"?
/*// layout subgraphs
HTML_HELP_OPEN() \
HTML_HELP_DEF( "type", "bool" ) \
HTML_HELP_DEF( "values", "[true, false]" ) \
HTML_HELP_DEF( "default", "true" ) \
HTML_HELP_BODY() \
"If true the layout of the newly created subgraphs is computed." \
HTML_HELP_CLOSE(),
// layout quotient graph
HTML_HELP_OPEN() \
HTML_HELP_DEF( "type", "bool" ) \
HTML_HELP_DEF( "values", "[true, false]" ) \
HTML_HELP_DEF( "default", "true" ) \
HTML_HELP_BODY() \
"If true the layout of the quotient graph is computed." \
HTML_HELP_CLOSE(),*/
};
}
//================================================================================
StrengthClustering::StrengthClustering(PluginContext* context):DoubleAlgorithm(context) {
addInParameter<NumericProperty*>("metric", paramHelp[0], "", false);
// addInParameter<bool>("layout subgraphs", paramHelp[1], "true");
// addInParameter<bool>("layout quotient graph", paramHelp[2], "true");
// addDependency("Quotient Clustering", "1.3");
// addDependency("Connected Component", "1.0");
addDependency("Strength", "1.0");
// addDependency("Circular", "1.1");
// addDependency("GEM (Frick)", "1.1");
// addDependency("Auto Sizing", "1.0");
}
//==============================================================================
bool StrengthClustering::run() {
string errMsg;
values = new DoubleProperty(graph);
if (!graph->applyPropertyAlgorithm("Strength", values, errMsg, pluginProgress))
return false;
NumericProperty *metric = NULL;
// subgraphsLayout = true;
// quotientLayout = true;
if (dataSet) {
dataSet->get("metric", metric);
// dataSet->get("layout subgraphs", subgraphsLayout);
// dataSet->get("layout quotient graph", quotientLayout);
}
if (metric) {
NumericProperty* mult = metric->copyProperty(graph);
if (pluginProgress)
pluginProgress->setComment("Computing Strength metric X specified metric on edges ...");
mult->uniformQuantification(100);
edge e;
unsigned int steps = 0, maxSteps = graph->numberOfEdges();
if (maxSteps < 10)
maxSteps = 10;
forEach (e, graph->getEdges()) {
values->setEdgeValue(e, values->getEdgeValue(e)*(mult->getEdgeDoubleValue(e) + 1));
if (pluginProgress && ((++steps % (maxSteps / 10) == 0))) {
pluginProgress->progress(steps, maxSteps);
if (pluginProgress->state() !=TLP_CONTINUE)
return pluginProgress->state()!= TLP_CANCEL;
}
}
delete mult;
}
bool stopped = false;
const unsigned int NB_TEST = 100;
if (pluginProgress) {
pluginProgress->setComment("Partitioning nodes...");
pluginProgress->progress(0, NB_TEST + 1);
}
double threshold = findBestThreshold(NB_TEST, stopped);
if (stopped)
return pluginProgress->state()!= TLP_CANCEL;
vector< set<node > > tmp;
computeNodePartition(threshold, tmp);
for(unsigned int i=0; i<tmp.size(); ++i) {
set<node>::const_iterator it;
for(it=tmp[i].begin(); it!=tmp[i].end(); ++it) {
result->setNodeValue(*it,i);
}
}
// if (tmp.size()==1) {
// if (quotientLayout)
// drawGraph(graph);
// if (dataSet!=0) {
// dataSet->set("strengthGraph",graph);
// }
// return true;
// }
// Graph *tmpGraph, *quotientGraph;
// if (pluginProgress)
// pluginProgress->setComment("Building subgraphs...");
// tmpGraph = buildSubGraphs(tmp);
// if (!tmpGraph)
// return pluginProgress->state()!= TLP_CANCEL;
// if (!recursiveCall(tmpGraph))
// return pluginProgress->state()!= TLP_CANCEL;
// if (pluginProgress)
// pluginProgress->setComment("Building quotient graph...");
// quotientGraph = buildQuotientGraph(tmpGraph);
// if (!quotientGraph)
// return pluginProgress->state()!= TLP_CANCEL;
// if (dataSet!=0) {
// dataSet->set("strengthGraph", quotientGraph);
// }
delete values;
return true;
}
//================================================================================
bool StrengthClustering::check(string &erreurMsg) {
if (!SimpleTest::isSimple(graph)) {
erreurMsg ="The graph must be simple";
return false;
}
// if (!ConnectedTest::isConnected(graph)) {
// erreurMsg ="The graph must be connected";
// return false;
// }
return true;
}
|