#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "StrengthClustering.h" CLUSTERINGPLUGIN(StrengthClustering,"Strength","David Auber","27/01/2003","Alpha","0","1"); using namespace std; //================================================================================ StrengthClustering::StrengthClustering(ClusterContext context):Clustering(context) {} //================================================================================ StrengthClustering::~StrengthClustering() {} //================================================================================ double StrengthClustering::e(set &U,set &V) { set::const_iterator itU; double result=0; for (itU=U.begin();itU!=U.end();++itU) { Iterator *itN=superGraph->getInOutNodes(*itU); for (;itN->hasNext();) { node itn=itN->next(); if (V.find(itn)!=V.end()) result+=1; }delete itN; } return result; } //============================================================================== double StrengthClustering::e(set &U) { set::const_iterator itU; double result=0; for (itU=U.begin();itU!=U.end();++itU) { Iterator *itN=superGraph->getInOutNodes(*itU); for (;itN->hasNext();) { node itn=itN->next(); if (U.find(itn)!=U.end()) result+=1; }delete itN; } return result/2; } //============================================================================== double StrengthClustering::s(set &U,set &V) { if ((U.size()==0) || (V.size()==0)) return 0; return (e(U,V) / (U.size()*V.size())); } //============================================================================== double StrengthClustering::s(set &U) { if (U.size()<2) return 0; return (e(U)) * 2 / (U.size()*(U.size()-1)); } //============================================================================== double StrengthClustering::computeMQValue(vector > partition) { double positive=0; for (int i=0;i1) negative/=partition.size()*(partition.size()-1)/2; // cout << "positive :"<< positive << " , negative :" << negative << endl; return positive-negative; } //============================================================================== vector< set > StrengthClustering::computeNodePartition(double threshold) { bool cached,results; string errMsg; MetricProxy *values=getLocalProxy(superGraph,"Strength",cached,results,errMsg); // double threshold=2; // step One build a filtered subgraph // All edges with a values less than the threshold are removed // cerr << "1" << endl << flush; SuperGraph *tmpGraph=TlpTools::newCloneSubGraph(superGraph); Iterator *itE=superGraph->getEdges(); for (;itE->hasNext();) { edge ite=itE->next(); if (values->getEdgeValue(ite)deg(superGraph->source(ite))>1 && superGraph->deg(superGraph->target(ite))>1) tmpGraph->delEdge(ite); } } delete itE; set singleton; // Select SubGraph singleton in superGraph // cerr << "2" << endl << flush; Iterator *itN=tmpGraph->getNodes(); for (;itN->hasNext();) { node itn=itN->next(); if (tmpGraph->deg(itn)==0) singleton.insert(itn); }delete itN; // restore edges to reconnect singleton by computing induced subgraph itE=superGraph->getEdges(); for (;itE->hasNext();) { edge ite=itE->next(); if (singleton.find(superGraph->source(ite))!=singleton.end() && singleton.find(superGraph->target(ite))!=singleton.end()) { tmpGraph->addEdge(ite); } }delete itE; //Extract connected componnent //cerr << "5" << endl << flush; MetricProxy *connected=getLocalProxy(tmpGraph,"Connected Component",cached,results,errMsg); if (cached) connected->recompute(errMsg); //Put individual nodes in the same cluster double val=-1; itN=tmpGraph->getNodes(); for (;itN->hasNext();) { node itn=itN->next(); if (tmpGraph->deg(itn)==0) { if (val==-1) val=connected->getNodeValue(itn); else connected->setNodeValue(itn,val); } } delete itN; //Compute the node partition vector< set > result; int index=0; map resultIndex; itN=tmpGraph->getNodes(); for (;itN->hasNext();) { node itn=itN->next(); double val=connected->getNodeValue(itn); if (resultIndex.find(val)!=resultIndex.end()) result[resultIndex[val]].insert(itn); else { set tmp; result.push_back(tmp); resultIndex[val]=index; result[index].insert(itn); ++index; } }delete itN; tmpGraph->getPropertyProxyContainer()->delLocalProxy("Connected Component"); superGraph->delAllView(tmpGraph->getSubGraph()); return result; } void drawGraph(SuperGraph *tmpg) { bool cached,result; string errMsg; string layoutName="GEM (Frick)"; string sizesName="Auto_sizing"; *getLocalProxy(tmpg, "viewLayout") = *getLocalProxy(tmpg, layoutName, result, cached, errMsg); *getLocalProxy(tmpg, "viewSize") = *getLocalProxy(tmpg, sizesName, result, cached, errMsg); tmpg->getPropertyProxyContainer()->delLocalProxy(layoutName); tmpg->getPropertyProxyContainer()->delLocalProxy(sizesName); } //============================================================================== bool StrengthClustering::run() { // cerr << __PRETTY_FUNCTION__ << " Start" << endl; // cout << "Average Path Length :" << TlpTools::averagePathLength(superGraph) << endl; // cout << "Average clustering :" << TlpTools::averageCluster(superGraph) << endl; bool cached,result; string errMsg; MetricProxy *values=getLocalProxy(superGraph,"Strength",cached,result,errMsg); double maxMQ=0; int numberOfStep=100; double threshold=0; double deltaThreshold=(values->getEdgeMax(superGraph)-values->getEdgeMin(superGraph))/numberOfStep; int debugI=1; // cerr << debugI++ << " ....." << endl; for (double i=values->getEdgeMin(superGraph);igetEdgeMax(superGraph);i+=deltaThreshold) { vector< set > tmp; tmp=computeNodePartition(i); double mq=computeMQValue(tmp); if (mq>maxMQ) { threshold=i; maxMQ=mq; } } cout << " MQ value for clustering : " << maxMQ << endl << flush; // cerr << debugI++ << " ....." << endl; vector< set > tmp; tmp.clear(); // threshold=(values->getEdgeMax())*80/step; tmp=computeNodePartition(threshold); if (tmp.size()==1) { drawGraph(superGraph); if (dataSet!=0) { dataSet->set("strengthGraph",superGraph); } return true; } SuperGraph *tmpGraph=TlpTools::newCloneSubGraph(superGraph); // cerr << debugI++ << " ....." << endl; map mapGraph; for (int i=0;inumberOfNodes() << endl; cout << "Number of edges :" << tmpg->numberOfEdges() << endl; */ SuperGraph *tmpGr=tmpg; if ( tmp.size()>1 && avPath>1 && avPath<4 && avCluster>0.25 && tmpg->numberOfNodes()>10) { DataSet tmpData; TlpTools::clusterizeGraph(tmpg,errMsg,&tmpData,"Strength"); tmpData.get("strengthGraph",tmpGr); } mapGraph[tmpg]=tmpGr; if (tmpg==tmpGr) { drawGraph(tmpg); } tmpg=tmpGr; } // cerr << debugI++ << " ....." << endl; DataSet tmpData; TlpTools::clusterizeGraph(tmpGraph,errMsg,&tmpData,"QuotientClustering"); superGraph->getPropertyProxyContainer()->delLocalProxy("Strength"); SuperGraph *quotientGraph; tmpData.get("quotientGraph",quotientGraph); drawGraph(quotientGraph); if (dataSet!=0) { dataSet->set("strengthGraph",quotientGraph); } if (quotientGraph!=superGraph) { // cerr << debugI++ << " .....A" << endl; SuperGraph *rootGraph=superGraph->getClusterTree()->getRootSubGraph()->getAssociatedSuperGraph(); MetaGraphProxy *meta=getProxy(rootGraph,"viewMetaGraph"); MetaGraphProxy *meta2=getProxy(rootGraph,"strengthMetaGraph"); // cerr << debugI++ << " .....B" << endl; Iterator *itN=quotientGraph->getNodes(); for (;itN->hasNext();) { node itn=itN->next(); meta2->setNodeValue(itn,meta->getNodeValue(itn)); meta->setNodeValue(itn,mapGraph[meta->getNodeValue(itn)]); } delete itN; // cerr << __PRETTY_FUNCTION__ << " End" << endl; } return true; } //================================================================================ bool StrengthClustering::check(string &erreurMsg) { erreurMsg=""; return true; } //================================================================================ void StrengthClustering::reset() { } //================================================================================