utils.cpp 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479
  1. /**
  2. * @file tests/boost_graph/partitioning/utils.cpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include <tests/boost_graph/partitioning/utils.hpp>
  26. #include <algorithm>
  27. #include <iostream>
  28. #include <fstream>
  29. namespace paradevs { namespace tests { namespace boost_graph {
  30. UnorientedGraph::vertex_iterator vertexIt, vertexEnd;
  31. UnorientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
  32. OrientedGraph::vertex_iterator vertexIto, vertexEndo;
  33. OrientedGraph::adjacency_iterator neighbourIto, neighbourEndo;
  34. struct myclass
  35. {
  36. bool operator() (Entiers *i, Entiers *j)
  37. { return i->at(0) < j->at(0); }
  38. } myobject;
  39. struct myclass2
  40. {
  41. bool operator() (Entiers *i, Entiers *j, UnorientedGraph *g)
  42. { return Calcul_poids(i,g) < Calcul_poids(j,g); }
  43. } mon_tri;
  44. struct myclass3
  45. {
  46. bool operator() (Entiers *i, Entiers *j)
  47. { return i->size() > j->size(); }
  48. } myobject_taille;
  49. struct myclass4
  50. {
  51. bool operator() (int i, int j, UnorientedGraph *g)
  52. { return (*g)[i]._weight > (*g)[j]._weight; }
  53. } mon_poids;
  54. /**
  55. * Fonction de verification de la connexité d'un graphe
  56. * @param *g : adresse d'un graphe de type boost graphe undirected
  57. * @param Partition : vecteur contenant des vecteurs d'entiers [tableau contenant les parties de la partition]
  58. * @param part : vecteur d'entier (une partie de la partition)
  59. * @return un booleen
  60. */
  61. bool Est_connexe(UnorientedGraph *g, EntiersEntiers Partition, Entiers &part)
  62. {
  63. /*
  64. * Copie du graphe contenu par l'adresse *g
  65. */
  66. UnorientedGraph copie_g;
  67. copie_g = *g;
  68. /*
  69. * Modification du graphe copié afin de générer des sous graphes liés aux différentes parties
  70. */
  71. for (uint i=0; i<Partition.size()-1;i++)
  72. {
  73. for (uint j=1+i; j<Partition.size();j++)
  74. {
  75. for (uint k=0; k<Partition.at(i)->size();k++)
  76. {
  77. for (uint y=0; y<Partition.at(j)->size();y++)
  78. {
  79. remove_edge(Partition.at(i)->at(k),Partition.at(j)->at(y),copie_g); //suppression de certains arcs
  80. }
  81. }
  82. }
  83. }
  84. /*
  85. * Objectif : déterminer s'il existe un chemin reliant tous les noeuds d'une même partie
  86. * Méthode : partir d'un sommet de départ tiré aléatoirement dans la partie "part" et parcourir l'ensemble de ces voisins.
  87. * Si le voisin recontré n'est pas contenu dans le vecteur "sommets" il est ajouté. Le processus est répété pour chaque
  88. * nouveau sommet ajouté au vecteur.
  89. * Résultat : si le nombre de sommets contenu dans le vecteur "part" est égale au nombre de sommets du vecteur "sommets"
  90. * alors le graphe est connexe
  91. */
  92. uint val;
  93. Entiers sommets;
  94. if(part.size()==1)
  95. val = 0;
  96. else
  97. val=rand_fini(0,part.size()-1); //tirage aléatoire d'un sommets
  98. uint vertex = part.at(val);
  99. sommets.push_back(vertex); //ajout du sommets à la lsite des sommets parcouru
  100. /*
  101. * Recherche de voisins n'appartenant pas à la liste des sommets parcourus
  102. */
  103. for(uint i = 0;i<sommets.size();i++){
  104. tie(neighbourIt, neighbourEnd) = adjacent_vertices(sommets.at(i),copie_g);
  105. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  106. if(In_tab(sommets,*neighbourIt)!=1)
  107. sommets.push_back(*neighbourIt);
  108. }
  109. }
  110. /*
  111. * Retour de la réponse vrai ou faux
  112. */
  113. if(part.size()!=sommets.size())
  114. return false;
  115. else
  116. return true;
  117. }
  118. /**
  119. * Fonction de projection
  120. * @param Partition : vecteur contenant des vecteurs d'entiers [tableau contenant les parties de la partition]
  121. * @param lit : itérateur sur une liste contenant une vecteur de vecteur d'entier
  122. * @return
  123. */
  124. /*
  125. * Objectif : obtenir la correspondance entre les sommets d'un graphe Gn et celui de Gn-1
  126. * Méthode : modification des sommets contenus dans "Partition" à l'aide de la liste de correspondance *lit
  127. */
  128. void projection(EntiersEntiers &Partition,ListEntiersEntiers::iterator lit)
  129. {
  130. /*
  131. * Création d'un nouveau vecteur contenant les adresses d'autres vecteur d'entiers.
  132. * Celui-ci est conçu pour recevoir les sommets contenus dans la liste de correspondance,
  133. * correspondant à la projection des sommets du graphe Gn à celui de Gn-1
  134. */
  135. EntiersEntiers new_partition;
  136. for(uint i = 0; i< Partition.size() ; i++)
  137. {
  138. Entiers *new_part = new Entiers();
  139. for(uint j = 0 ; j<Partition.at(i)->size() ; j++)
  140. {
  141. for(uint k = 0; k<((*lit)->at(Partition.at(i)->at(j)))->size();k++){
  142. new_part->push_back((*lit)->at(Partition.at(i)->at(j))->at(k));
  143. }
  144. }
  145. new_partition.push_back(new_part);
  146. }
  147. /*
  148. * Désalocation mémoire des pointeurs que contient "Partition"
  149. */
  150. for(EntiersEntiers::iterator it = Partition.begin(); it != Partition.end(); it++)
  151. {
  152. delete *it;
  153. *it = NULL;
  154. }
  155. Partition = new_partition; // copie de new_partition dans Partition
  156. for(uint i =0; i<Partition.size(); i++) {
  157. // permet de trier chaque sous vecteur de "Partition"
  158. std::sort(Partition[i]->begin(),Partition[i]->end());
  159. }
  160. new_partition.clear();
  161. }
  162. /**
  163. * Fonction qui calcul le poids d'une partie
  164. * @param * part : adresse d'un vecteur d'entier, ici une partie de la partition
  165. * @param *g : adresse d'un graphe de type boost graphe undirected
  166. * @return un type double contenant le poids associé à la partie
  167. */
  168. double Calcul_poids(Entiers *partie, UnorientedGraph *g)
  169. {
  170. double poids=0; // initialisation du poids à 0
  171. /*
  172. * Pour chaque sommet de la partie concerné on ajoute son poids au poids total
  173. */
  174. for(uint j = 0; j<partie->size();j++){
  175. poids+=(*g)[partie->at(j)]._weight;
  176. }
  177. return poids;
  178. }
  179. /**
  180. * Fonction d'affinage suivant un critère de poids
  181. * @param *g : adresse d'un graphe de type boost graphe undirected
  182. * @param Partition : vecteur contenant des vecteurs d'entiers [tableau contenant les parties de la partition]
  183. * @return modification de la partition
  184. */
  185. void Affinage_equilibrage_charge(UnorientedGraph *g, EntiersEntiers &Partition)
  186. {
  187. /*
  188. * Calcule de la somme des poids du graphe et le poids moyen à atteindre
  189. */
  190. double poids_moyen = 0.;
  191. for(uint i = 0; i < num_vertices(*g); i++) {
  192. poids_moyen += (*g)[i]._weight;
  193. }
  194. // détermination du poids moyen à atteindre pour chaque partie
  195. poids_moyen /= Partition.size();
  196. std::vector < double > poids_parties;
  197. /*
  198. * Calcul du poids de chaque partie de la partition
  199. */
  200. for (uint i = 0; i < Partition.size(); i++) {
  201. double tmp = Calcul_poids(Partition.at(i),g);
  202. poids_parties.push_back(tmp);
  203. }
  204. std::clog << "Poids initial des parties : " << std::endl;
  205. // for (uint i = 0; i < poids_parties.size(); i++){
  206. // std::cout << poids_parties.at(i) << " ";
  207. // }
  208. // std::cout << "\n" << std::endl;
  209. /*
  210. * Le critère d'amélioration consiste à faire tendre vers 0 la somme
  211. * des écarts entre le poids des parties et le poids moyen
  212. * le "critere" est la somme pour chaque partie de la différence
  213. * en valeurs absolues du poids
  214. * d'une partie moins le poids moyen divisé par le nombre de parties
  215. */
  216. double critere = 0.;
  217. for (uint i = 0; i < poids_parties.size(); i++){
  218. critere += abs(poids_parties.at(i) - poids_moyen);
  219. }
  220. critere /= Partition.size();
  221. // on conserve le poids maximum
  222. double p_max = *max_element(poids_parties.begin(), poids_parties.end());
  223. // std::cout << "Valeurs du criètre de départ : " << critere << std::endl;
  224. // création d'un second critère légérement plsu faible que le premier
  225. double best_critere = critere - 1e-7;
  226. uint nbr_passage = 1; // initialisation du nombre de passages à 1
  227. /*
  228. * Tant que le critère n'est pas amélioré etque le nombre de
  229. * passage est inférieur au nombre de parties on réalise
  230. * des modifications sur la partition
  231. */
  232. while (best_critere < critere or nbr_passage < Partition.size()) {
  233. critere = best_critere; //critere devient best_critere
  234. // recherche la partie associé au poids maximum
  235. int cpt = recherche_val_double(poids_parties,p_max);
  236. bool decision = false; //initialisatio d'un booleen a false
  237. int nbr_pass_interne = 0;
  238. /*
  239. * tirage aléatoire des sommets de la partie de poids maximum
  240. */
  241. Entiers random_orders(Partition.at(cpt)->size());
  242. for (uint i=0 ; i<Partition.at(cpt)->size() ; i++)
  243. random_orders.at(i)=Partition.at(cpt)->at(i);
  244. for (uint j=0 ; j<Partition.at(cpt)->size()-1 ; j++) {
  245. int rand_pos = (rand() % Partition.at(cpt)->size()-j)+j;
  246. int tmp = random_orders[j];
  247. random_orders[j] = random_orders[rand_pos];
  248. random_orders[rand_pos] = tmp;
  249. }
  250. /*
  251. * Si le nombre de sommets d'une partie excéde les 400, on tire au hasar 400 sommets sans remise
  252. * et on effectue les modifications (ceci permet d'eviter une explosion des temps de calcul)
  253. */
  254. int size;
  255. if(Partition.at(cpt)->size()>400)
  256. size = 400;
  257. else
  258. size = Partition.at(cpt)->size();
  259. /*
  260. * Seconde boucle Tant que sur les sommets d'une partie.
  261. * Tant que le critere booleen est vrai et que le nombre de passe interne est inférieur au seuil size faire
  262. */
  263. while(decision!=true && nbr_pass_interne < size){
  264. int vertex = random_orders.at(nbr_pass_interne); //tirage d'un sommets dans la parite de poids maximum
  265. Entiers community = Neigh_community(g,Partition,vertex,cpt); // recherche des communautés voisines à ce sommet (s'il est au bord)
  266. if(community.size()!=0) // s'il existe au moins une communauté voisine
  267. {
  268. std::vector<double> new_poids_parties; // initialisation d'un nouveau vecteur contenant des doubles
  269. std::vector<double> tmp_critere; // initialisation d'un nouveau vecteur contenant des doubles
  270. /*
  271. * Pour chacune des parties (communauté) voisine au sommet vertexs faire
  272. */
  273. for(uint k = 0; k < community.size();k++)
  274. {
  275. new_poids_parties = poids_parties; //copie du tableau de poids des parties dans new_poids_parties
  276. /*
  277. * Modification des poids des parties :
  278. * on ajoute le poids du sommets à la partie voisine
  279. * et on soustrait son poids à sa partie d'origine
  280. */
  281. new_poids_parties.at(community.at(k))+=(*g)[vertex]._weight;
  282. new_poids_parties.at(cpt)-=(*g)[vertex]._weight;
  283. /*
  284. * Calcul ldu nouveau critère associé à cette modification
  285. */
  286. double new_critere = 0.;
  287. for(uint i = 0; i<poids_parties.size();i++){
  288. new_critere += abs(new_poids_parties.at(i)-poids_moyen);
  289. }
  290. new_critere/=Partition.size();
  291. tmp_critere.push_back(new_critere); // enregistrement du résutlat dans le tableau tmp_critere
  292. }
  293. double val_min = *min_element(tmp_critere.begin(),tmp_critere.end()); // enregistrement de la valeur minimale du tableau tmp_critere
  294. int val = recherche_val_double(tmp_critere,val_min); // recherche de la communauté correspondant à cette valeur
  295. /*
  296. * Si la valeur associé est plus petite et que la partie selectionné n'est pas vérouillée faire
  297. */
  298. if(val_min<critere && poids_parties.at(val)!=-1)
  299. {
  300. /*
  301. * On change le sommet vertex de partie, il est déplacé vers la partie
  302. * qui permet la meilleure amélioration du critère
  303. */
  304. Partition.at(community.at(val))->push_back(vertex);
  305. suprim_val(*Partition.at(cpt),vertex);
  306. std::sort(Partition.at(community.at(val))->begin(), Partition.at(community.at(val))->end());
  307. /*
  308. * Vérification de la sauvegarde de la connexsité,
  309. * si se n'est pas le cas retour à l'état précédent
  310. */
  311. if(Est_connexe(g,Partition,*Partition.at(cpt))!=1)
  312. {
  313. suprim_val(*Partition.at(community.at(val)),vertex);
  314. Partition.at(cpt)->push_back(vertex);
  315. std::sort(Partition.at(cpt)->begin(), Partition.at(cpt)->end());
  316. // std::cout<<" C'EST MORT RETOUR EN ARRIERE ! "<<std::endl
  317. ;
  318. }
  319. else
  320. {
  321. poids_parties = new_poids_parties;
  322. decision = true;
  323. // std::cout<<" Modification reussi ! "<<std::endl;
  324. }
  325. }
  326. }
  327. nbr_pass_interne++;
  328. }
  329. /*
  330. * Si aucune modification n'a été réalisé pour cett partie de poids maximum
  331. */
  332. if(decision==false)
  333. {
  334. nbr_passage++; // augmentation du nombre de passage
  335. poids_parties.at(cpt)=-1; // vérrouillage de la partie
  336. std::clog<<"nouveau passag ! "<<std::endl;
  337. }
  338. else
  339. {
  340. best_critere = 0.;
  341. for(uint i = 0; i<poids_parties.size();i++){
  342. best_critere += abs(poids_parties.at(i)-poids_moyen);
  343. }
  344. best_critere/=Partition.size();
  345. nbr_passage = 0;
  346. }
  347. // std::clog<<"Poids des parties modifié : "<<std::endl;
  348. // for(uint i = 0; i<poids_parties.size(); i++){
  349. // std::cout<<poids_parties.at(i)<<" ";
  350. // }
  351. // std::cout<<"\n"<<std::endl;
  352. p_max = *max_element(poids_parties.begin(),poids_parties.end());
  353. // std::cout<<"Valeurs du criètre : "<<critere<<std::endl;
  354. // std::cout<<"Valeurs du best_criètre : "<<best_critere<<std::endl;
  355. // std::cout<<"Nombre de passage : "<<nbr_passage<<std::endl;
  356. // std::cout<<"\n"<<std::endl;
  357. }
  358. }
  359. Entiers Neigh_community(UnorientedGraph *g, EntiersEntiers &Partition, int vertex, int comm_in)
  360. {
  361. Entiers community;
  362. int comm;
  363. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex,*g);
  364. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  365. comm = In_community_dichotomie(Partition,*neighbourIt);
  366. if(In_tab(community,comm)!=1 && comm!=comm_in)
  367. community.push_back(comm);
  368. }
  369. return community;
  370. }
  371. void Affinage_recherche_locale(UnorientedGraph *g, EntiersEntiers &Partition, double &cut, std::string name)
  372. {
  373. Entiers random_orders(num_vertices(*g)); //gestion d'un tableau contenant tout les sommets et ranger de façon aléatoire
  374. for (uint i=0 ; i<num_vertices(*g) ; i++)
  375. random_orders.at(i)=i;
  376. for (uint j=0 ; j<num_vertices(*g)-1 ; j++) {
  377. int rand_pos = (rand() % num_vertices(*g)-j)+j;
  378. int tmp = random_orders[j];
  379. random_orders[j] = random_orders[rand_pos];
  380. random_orders[rand_pos] = tmp;
  381. }
  382. uint size = random_orders.size();
  383. if(num_vertices(*g)>500)
  384. size=500;
  385. std::vector<std::vector<double> > tabe_cut;
  386. //std::cout<<"Passage : "<<Partition.size()<<std::endl;
  387. for(uint k = 0; k < Partition.size();k++){
  388. std::vector<double> tmp;
  389. double vol = 0.;
  390. double cut = Modif_Cut_one_cluster(*Partition.at(k), *g, vol, name);
  391. tmp.push_back(cut);
  392. tmp.push_back(vol);
  393. tabe_cut.push_back(tmp);
  394. }
  395. for(uint i = 0; i < size; i++){
  396. if(random_orders.at(i)!=-1){
  397. int vertex = random_orders.at(i);
  398. //std::cout<<vertex<<std::endl;
  399. int comm = In_community_dichotomie(Partition, vertex);
  400. Entiers community = Neigh_community(g,Partition,vertex,comm);
  401. std::vector<double> tmp_cut;
  402. if(community.size()!=0 && Partition.at(comm)->size()!=1){
  403. tmp_cut = modif_cut_tmp(g,Partition,tabe_cut,vertex,comm,community,cut,name);
  404. /*for(uint z = 0; z<tmp_cut.size(); z++){
  405. std::cout<<tmp_cut.at(z)<<std::endl;
  406. }
  407. std::cout<<"\n"<<std::endl;*/
  408. double cut_min = *min_element(tmp_cut.begin(),tmp_cut.end());
  409. //std::cout<<"cout de coupe minimum de la liste : "<<cut_min<<std::endl;
  410. if(cut_min<cut){
  411. // std::clog<<"Changement ! "<<std::endl;
  412. int tmp = recherche_val_double(tmp_cut,cut_min);
  413. cut=cut_min;
  414. Partition.at(community.at(tmp))->push_back(vertex);
  415. suprim_val(*Partition.at(comm),vertex);
  416. std::sort(Partition.at(community.at(tmp))->begin(), Partition.at(community.at(tmp))->end());
  417. tabe_cut.clear();
  418. for(uint k = 0; k < Partition.size();k++){
  419. std::vector<double> tmp;
  420. double vol = 0.;
  421. double cut = Modif_Cut_one_cluster(*Partition.at(k), *g, vol, name);
  422. tmp.push_back(cut);
  423. tmp.push_back(vol);
  424. tabe_cut.push_back(tmp);
  425. }
  426. }
  427. }
  428. Modif_fonction_Gain_Cut(Partition,g,community,vertex,cut,name);
  429. /*if(Est_connexe(g,Partition,*Partition.at(comm))!=1)
  430. {
  431. suprim_val(*Partition.at(community.at(tmp)),vertex);
  432. Partition.at(comm)->push_back(vertex);
  433. std::sort(*Partition.at(comm));
  434. std::cout<<" C'EST MORT RETOUR EN ARRIERE ! "<<std::endl;
  435. }*/
  436. }
  437. }
  438. }
  439. double Modif_Cut_one_cluster(Entiers &cluster, UnorientedGraph &g, double &vol, std::string name)
  440. {
  441. edge_t e1;
  442. bool found;
  443. double cpt= 0.;
  444. if(name == "norm"){
  445. for(uint i=0;i<cluster.size();i++){
  446. tie(neighbourIt, neighbourEnd) = adjacent_vertices(cluster.at(i), g);
  447. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  448. tie(e1,found)=edge(vertex(cluster[i],g),vertex(*neighbourIt,g),g);
  449. if(In_tab(cluster,*neighbourIt)!=1){
  450. cpt+=g[e1]._weight;
  451. }
  452. }
  453. }
  454. vol = Cluster_Degree(g,cluster);
  455. } else if(name == "ratio"){
  456. for(uint i=0;i<cluster.size();i++){
  457. tie(neighbourIt, neighbourEnd) = adjacent_vertices(cluster.at(i), g);
  458. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  459. tie(e1,found)=edge(vertex(cluster[i],g),vertex(*neighbourIt,g),g);
  460. if(In_tab(cluster,*neighbourIt)!=1){
  461. cpt+=g[e1]._weight;
  462. }
  463. }
  464. }
  465. vol = Cluster_Weight(g,cluster);
  466. }
  467. return cpt;
  468. }
  469. std::vector<double> modif_cut_tmp(UnorientedGraph *g, EntiersEntiers &Partition, std::vector<std::vector<double> > tabe_cut, int vertexs, int comm_in, Entiers community, double cut,std::string name){
  470. edge_t e1;
  471. bool found;
  472. //std::cout<<"le sommet tiré est : "<<vertexs<<std::endl;
  473. if(name == "cut")
  474. {
  475. std::vector<double> modif_cut(community.size());
  476. double cpt_comm_in;
  477. double cpt_comm_out;
  478. for(uint i =0; i<community.size(); i++){
  479. double tmp_cut = cut;
  480. cpt_comm_in=0;
  481. cpt_comm_out=0;
  482. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertexs,*g);
  483. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  484. tie(e1,found)=edge(vertex(vertexs,*g),vertex(*neighbourIt,*g),*g);
  485. if(In_tab(*Partition.at(comm_in),*neighbourIt)==1)
  486. cpt_comm_in+=(*g)[e1]._weight;
  487. else if(In_tab(*Partition.at(community.at(i)),*neighbourIt)==1)
  488. cpt_comm_out+=(*g)[e1]._weight;
  489. }
  490. tmp_cut+=cpt_comm_in;
  491. tmp_cut-=cpt_comm_out;
  492. modif_cut.at(i)=tmp_cut;
  493. }
  494. return modif_cut;
  495. }
  496. else if(name == "norm"){
  497. std::vector<double> modif_cut(community.size());
  498. double cpt_comm_in;
  499. double cpt_comm_out;
  500. double tmp_cut;
  501. for(uint i =0; i<community.size(); i++){
  502. std::vector<std::vector<double> > tab_cut = tabe_cut;
  503. tmp_cut =0.;
  504. cpt_comm_in=0.;
  505. cpt_comm_out=0.;
  506. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertexs,*g);
  507. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  508. tie(e1,found)=edge(vertex(vertexs,*g),vertex(*neighbourIt,*g),*g);
  509. if(In_tab(*Partition.at(comm_in),*neighbourIt)==1)
  510. cpt_comm_in+=(*g)[e1]._weight;
  511. else if(In_tab(*Partition.at(community.at(i)),*neighbourIt)==1)
  512. cpt_comm_out+=(*g)[e1]._weight;
  513. }
  514. cpt_comm_in/=2.;
  515. cpt_comm_out/=2.;
  516. tab_cut.at(comm_in).at(0)+=cpt_comm_in;
  517. tab_cut.at(comm_in).at(0)-=cpt_comm_out;
  518. tab_cut.at(comm_in).at(1)-= Degree(*g ,vertexs);
  519. tab_cut.at(community.at(i)).at(0)+=cpt_comm_in;
  520. tab_cut.at(community.at(i)).at(0)-=cpt_comm_out;
  521. tab_cut.at(community.at(i)).at(1)+= Degree(*g ,vertexs);
  522. for(uint j = 0; j < tab_cut.size();j++){
  523. tmp_cut+=((tab_cut.at(j).at(0))/(tab_cut.at(j).at(1)));
  524. }
  525. modif_cut.at(i)=tmp_cut;
  526. }
  527. }else if(name == "ratio"){
  528. std::vector<double> modif_cut(community.size());
  529. double cpt_comm_in;
  530. double cpt_comm_out;
  531. double tmp_cut;
  532. for(uint i =0; i<community.size(); i++){
  533. std::vector<std::vector<double> > tab_cut = tabe_cut;
  534. tmp_cut =0.;
  535. cpt_comm_in=0.;
  536. cpt_comm_out=0.;
  537. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertexs,*g);
  538. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  539. tie(e1,found)=edge(vertex(vertexs,*g),vertex(*neighbourIt,*g),*g);
  540. if(In_tab(*Partition.at(comm_in),*neighbourIt)==1)
  541. cpt_comm_in+=(*g)[e1]._weight;
  542. else if(In_tab(*Partition.at(community.at(i)),*neighbourIt)==1)
  543. cpt_comm_out+=(*g)[e1]._weight;
  544. }
  545. /*cpt_comm_in/=2.;
  546. cpt_comm_out/=2.;*/
  547. tab_cut.at(comm_in).at(0)+=cpt_comm_in;
  548. tab_cut.at(comm_in).at(0)-=cpt_comm_out;
  549. tab_cut.at(comm_in).at(1)-= (*g)[vertexs]._weight;
  550. tab_cut.at(community.at(i)).at(0)+=cpt_comm_in;
  551. tab_cut.at(community.at(i)).at(0)-=cpt_comm_out;
  552. tab_cut.at(community.at(i)).at(1)+= (*g)[vertexs]._weight;
  553. for(uint j = 0; j < tab_cut.size();j++){
  554. tmp_cut+=((tab_cut.at(j).at(0))/(tab_cut.at(j).at(1)));
  555. }
  556. modif_cut.at(i)=tmp_cut;
  557. }
  558. return modif_cut;
  559. }
  560. }
  561. void Modif_fonction_Gain_Cut(EntiersEntiers &Partition,UnorientedGraph *g, Entiers &community, int val, double &cut,std::string name)
  562. {
  563. /*std::cout<<"Nombre de communauté voisine : "<<community.size()<<std::endl;
  564. std::cout<<"\n"<<std::endl;*/
  565. for(uint i = 0; i<community.size();i++){
  566. EntiersEntiers new_partition;
  567. for(uint k = 0; k < Partition.size();k++){
  568. Entiers * tmp = new Entiers();
  569. for(uint j = 0;j<Partition.at(k)->size();j++){
  570. tmp->push_back(Partition.at(k)->at(j));
  571. }
  572. new_partition.push_back(tmp);
  573. }
  574. /*std::cout<<"Avant Modification partition"<<std::endl;
  575. std::cout<<"************"<<std::endl;
  576. for(uint t = 0; t< new_partition.size() ; t++)
  577. {
  578. for(uint j = 0 ; j<new_partition.at(t)->size() ; j++)
  579. {
  580. std::cout<<new_partition.at(t)->at(j)<<std::endl;
  581. }
  582. std::cout<<"\n"<<std::endl;
  583. }
  584. std::cout<<"************"<<std::endl;*/
  585. new_partition.at(community.at(i))->push_back(val);
  586. suprim_val(*new_partition.at(In_community_dichotomie(Partition,val)),val);
  587. std::sort(new_partition.at(community.at(i))->begin(),
  588. new_partition.at(community.at(i))->end());
  589. /*std::cout<<"Modification partition"<<std::endl;
  590. std::cout<<"************"<<std::endl;
  591. for(uint t= 0; t< new_partition.size() ; t++)
  592. {
  593. for(uint j = 0 ; j<new_partition.at(t)->size() ; j++)
  594. {
  595. std::cout<<new_partition.at(t)->at(j)<<std::endl;
  596. }
  597. std::cout<<"\n"<<std::endl;
  598. }
  599. std::cout<<"************"<<std::endl;*/
  600. double coupe = Cut_cluster(new_partition,*g,name);
  601. //std::cout<<"cout de coupe : "<<coupe<<std::endl;
  602. if(coupe<cut)
  603. {
  604. for(EntiersEntiers::iterator it = Partition.begin(); it != Partition.end(); it++)
  605. {
  606. delete *it;
  607. *it = NULL;
  608. }
  609. Partition = new_partition;
  610. cut = coupe;
  611. }
  612. else
  613. {
  614. for(EntiersEntiers::iterator it = new_partition.begin(); it != new_partition.end(); it++)
  615. {
  616. delete *it;
  617. *it = NULL;
  618. }
  619. }
  620. }
  621. }
  622. bool contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
  623. UnorientedGraph *gtmp = new UnorientedGraph();
  624. boost::copy_graph(*g, *gtmp);
  625. Entiers Random_list_vertices;//, Index_Vertex; // Initialisation du tableau de sommets rangés aléatoirements
  626. EntiersEntiers *tableau_de_correspondance = new EntiersEntiers();
  627. edge_t e1,e2; // Iterateurs sur les arcs
  628. bool found;
  629. uint nbr_vertex = num_vertices(*gtmp);
  630. Entiers sommets_a_detruire; // Initialisation d'un tableau pret à recevoir les "sommets à détruire"
  631. /*
  632. * Création d'un vecteur contenant l'ensemble des sommets du graphe. Ces sommets sont rangés
  633. * aléatoirement afin de simuler un tirage aléatoire
  634. */
  635. for (uint i=0 ; i<nbr_vertex ; i++)
  636. Random_list_vertices.push_back(i);
  637. //Index_Vertex.push_back(i);
  638. for (uint j=0 ; j<nbr_vertex-1 ; j++) {
  639. int rand_pos = rand()%(nbr_vertex-j)+j;
  640. int tmp = Random_list_vertices[j];
  641. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  642. Random_list_vertices[rand_pos] = tmp;
  643. }
  644. /*
  645. * Pour chaque sommet non verrouiller faire ....
  646. */
  647. //std::cout<<"Nouvelle contraction !!!"<<std::endl;
  648. //std::cout<<std::endl;
  649. for(uint i=0; i<nbr_vertex; i++){
  650. int vertexs = Random_list_vertices[i]; // Index_Vertex.at(Random_list_vertices.at(i));
  651. //std::cout<<"Le sommet tiré est : "<<vertexs<<std::endl;
  652. if(vertexs!=-1){
  653. Entiers liste_voisin = Liste_adjacence(*gtmp,vertexs,Random_list_vertices); // Recherche des sommets adjacents au sommets tiré
  654. if(liste_voisin.size()!=0){
  655. /*
  656. * S'il en existe au mois un sommet adjacent au sommet tiré qui n'est pas verrouillé, on
  657. * choisi celui dont l'arc les reliants est le plus fort. Dans le cas où les arcs ont tous
  658. * le même poids, on selectionne le sommet d'index le plus petit
  659. */
  660. double poids_a = 0.;
  661. int best_vertexs = -1;
  662. for(uint j=0;j<liste_voisin.size();j++){
  663. tie(e1,found)=edge(vertex(vertexs,*gtmp),vertex(liste_voisin[j],*gtmp),*gtmp);
  664. if((*gtmp)[e1]._weight>poids_a){
  665. best_vertexs = liste_voisin[j];
  666. poids_a = (*gtmp)[e1]._weight;
  667. }
  668. }
  669. Entiers * couple = new Entiers(); // Initialisation du vecteur contenant le couple de sommet fusionné
  670. int vertex_delete = std::max(vertexs, best_vertexs); // Sommet d'index le plus grand (qui sera détruit)
  671. //std::cout<<"sommet détruit : "<<(*gtmp)[vertex_delete]._index<<std::endl;
  672. int vertex_save = std::min(vertexs,best_vertexs); // Sommet d'identifiant le plus petit (qui sera conservé)
  673. //std::cout<<"sommet sauvé : "<<(*gtmp)[vertex_save]._index<<std::endl;
  674. sommets_a_detruire.push_back(vertex_delete); // On ajoute le sommet détruit au tableau des sommets à détruire
  675. /*
  676. * On ajoute au tableau "couple" le couple de sommet à fusionner
  677. */
  678. couple->push_back(vertex_save);
  679. couple->push_back(vertex_delete);
  680. tableau_de_correspondance->push_back(couple); // Ajout du "couple" à la liste de correspondance
  681. remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
  682. Entiers neigh_vertex_save; // Initialisation du vecteur contenant les sommets adjacents au "sommet sauvegardé"
  683. Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
  684. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
  685. /*
  686. * Remplissage de ces deux tableaux à l'aide de la fonction adjacent_vertices de boost graph
  687. * [La création de ces tableaux est nécéssaire du fait que certains arcs sont détruit au cours
  688. * du processus]
  689. */
  690. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  691. neigh_vertex_save.push_back(*neighbourIt);
  692. }
  693. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_delete,*gtmp);
  694. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  695. neigh_vertex_delete.push_back(*neighbourIt);
  696. }
  697. /*
  698. * Recherche de sommets communs entre le "sommet sauvegardé" et le "sommet à détruire"
  699. * S'il existe un tel sommet "v" alors on ajoute le poids de l'arcs (vertex_delet,v)
  700. * à celui de l'arcs (vertex_save,v) et on détruit l'arcs reliant "v" au "sommet à détruire"
  701. */
  702. for(uint j=0;j<neigh_vertex_delete.size();j++){
  703. if(In_tab(neigh_vertex_save,neigh_vertex_delete[j])==1){
  704. tie(e2,found)=edge(vertex(vertex_save,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  705. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  706. (*gtmp)[e2]._weight+=(*gtmp)[e1]._weight;
  707. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  708. }
  709. else
  710. {
  711. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  712. add_edge(vertex_save,neigh_vertex_delete[j],EdgeProperties((*gtmp)[e1]._weight),*gtmp);
  713. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  714. }
  715. }
  716. (*gtmp)[vertex_save]._weight+=(*gtmp)[vertex_delete]._weight; // ajout du poids du sommet détruit au sommet conservé
  717. /*
  718. * Vérouillage du "sommet sauvegardé" et du "sommet à détruire"
  719. */
  720. Random_list_vertices[i]=-1;
  721. Random_list_vertices[recherche_val(Random_list_vertices,best_vertexs)]=-1;
  722. val_cpt--;
  723. // std::cout<<val_cpt<<std::endl;
  724. }
  725. else{
  726. /*
  727. * Et si le sommet tiré ne possède pas de sommet adjacent non verrouillé
  728. * alors on l'ajoute à la liste de correspondance des sommets et on
  729. * le verrouille
  730. */
  731. Entiers *couple = new Entiers();
  732. couple->push_back(Random_list_vertices.at(i));
  733. tableau_de_correspondance->push_back(couple);
  734. Random_list_vertices[i]=-1;
  735. }
  736. /*std::cout<<"Poids noeud graphe contracté : "<<std::endl;
  737. tie(vertexIt, vertexEnd) = vertices(*gtmp);
  738. for (; vertexIt != vertexEnd; ++vertexIt) {
  739. std::cout << (*gtmp)[*vertexIt]._index
  740. << " -> ";
  741. std::cout << (*gtmp)[*vertexIt]._weight<<" ; ";
  742. }
  743. std::cout << std::endl;*/
  744. }
  745. if(val_cpt == val_reduc){
  746. for(uint j=i+1; j <nbr_vertex; j++){
  747. if(Random_list_vertices[j] !=-1){
  748. Entiers *couple = new Entiers();
  749. couple->push_back(Random_list_vertices.at(j));
  750. tableau_de_correspondance->push_back(couple);}
  751. }
  752. break;
  753. }
  754. }
  755. std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
  756. // std::cout<<"\n"<<std::endl;
  757. /*
  758. * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
  759. * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
  760. * des sommets
  761. */
  762. for(int j=(sommets_a_detruire.size()-1);j>-1;j--){
  763. //std::cout<<"Noeuds a supprimer : "<<sommets_a_detruire.at(j)<<std::endl;
  764. remove_vertex(sommets_a_detruire[j],*gtmp);
  765. }
  766. // std::clog<<"Affichage avant tri "<<std::endl;
  767. // for(uint k = 0;k<tableau_de_correspondance->size();k++){
  768. // for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
  769. // std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
  770. // }
  771. // std::cout<<"\n"<<std::endl;
  772. // }
  773. std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
  774. // std::clog<<"Tableau de correspondance "<<std::endl;
  775. // for(uint k = 0;k<tableau_de_correspondance->size();k++){
  776. // for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
  777. // std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
  778. // }
  779. // std::cout<<"\n"<<std::endl;
  780. // }
  781. liste_corr.push_back(tableau_de_correspondance);
  782. // std::cout<<"\n"<<std::endl;
  783. baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
  784. if(val_cpt == val_reduc)
  785. return true;
  786. else
  787. return false;
  788. }
  789. /*** Problème de segmentation avec cette méthode !!! ***/
  790. bool contraction_HEM_tests(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
  791. UnorientedGraph *gtmp = new UnorientedGraph();
  792. boost::copy_graph(*g, *gtmp);
  793. Entiers Random_list_vertices, Index_Vertex; // Initialisation du tableau de sommets rangés aléatoirements
  794. EntiersEntiers *tableau_de_correspondance = new EntiersEntiers();
  795. edge_t e1,e2; // Iterateurs sur les arcs
  796. bool found;
  797. uint nbr_vertex = num_vertices(*gtmp);
  798. Entiers sommets_a_detruire; // Initialisation d'un tableau pret à recevoir les "sommets à détruire"
  799. /*
  800. * Création d'un vecteur contenant l'ensemble des sommets du graphe. Ces sommets sont rangés
  801. * aléatoirement afin de simuler un tirage aléatoire
  802. */
  803. for (uint i=0 ; i<nbr_vertex ; i++){
  804. Random_list_vertices.push_back(i);
  805. Index_Vertex.push_back(i);}
  806. for (uint j=0 ; j<nbr_vertex-1 ; j++) {
  807. int rand_pos = rand()%(nbr_vertex-j)+j;
  808. int tmp = Random_list_vertices[j];
  809. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  810. Random_list_vertices[rand_pos] = tmp;
  811. }
  812. /*
  813. * Pour chaque sommet non verrouiller faire ....
  814. */
  815. //std::cout<<"Nouvelle contraction !!!"<<std::endl;
  816. //std::cout<<std::endl;
  817. for(uint i=0; i<nbr_vertex; i++){
  818. int vertexs = Index_Vertex.at(Random_list_vertices.at(i));
  819. //std::cout<<"Le sommet tiré est : "<<vertexs<<" ça place est : "<<Random_list_vertices.at(i)<<" place : "<<i<<std::endl;
  820. if(vertexs!=-1){
  821. Entiers liste_voisin = Liste_adjacence_tests(*gtmp,vertexs,Index_Vertex); // Recherche des sommets adjacents au sommets tiré
  822. if(liste_voisin.size()!=0){
  823. /*
  824. * S'il en existe au mois un sommet adjacent au sommet tiré qui n'est pas verrouillé, on
  825. * choisi celui dont l'arc les reliants est le plus fort. Dans le cas où les arcs ont tous
  826. * le même poids, on selectionne le sommet d'index le plus petit
  827. */
  828. double poids_a = -1.;
  829. int best_vertexs = -1;
  830. for(uint j=0;j<liste_voisin.size();j++){
  831. tie(e1,found)=edge(vertex(vertexs,*gtmp),vertex(liste_voisin[j],*gtmp),*gtmp);
  832. if((*gtmp)[e1]._weight>poids_a){
  833. best_vertexs = liste_voisin[j];
  834. poids_a = (*gtmp)[e1]._weight;
  835. }
  836. }
  837. Entiers *couple = new Entiers(); // Initialisation du vecteur contenant le couple de sommet fusionné
  838. int vertex_delete = std::max(vertexs, best_vertexs); // Sommet d'index le plus grand (qui sera détruit)
  839. //std::cout<<"sommet détruit : "<<(*gtmp)[vertex_delete]._index<<std::endl;
  840. int vertex_save = std::min(vertexs,best_vertexs); // Sommet d'identifiant le plus petit (qui sera conservé)
  841. //std::cout<<"sommet sauvé : "<<(*gtmp)[vertex_save]._index<<std::endl;
  842. sommets_a_detruire.push_back(vertex_delete); // On ajoute le sommet détruit au tableau des sommets à détruire
  843. /*
  844. * On ajoute au tableau "couple" le couple de sommet à fusionner
  845. */
  846. couple->push_back(vertex_save);
  847. couple->push_back(vertex_delete);
  848. tableau_de_correspondance->push_back(couple); // Ajout du "couple" à la liste de correspondance
  849. remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
  850. Entiers neigh_vertex_save; // Initialisation du vecteur contenant les sommets adjacents au "sommet sauvegardé"
  851. Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
  852. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
  853. /*
  854. * Remplissage de ces deux tableaux à l'aide de la fonction adjacent_vertices de boost graph
  855. * [La création de ces tableaux est nécéssaire du fait que certains arcs sont détruit au cours
  856. * du processus]
  857. */
  858. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  859. neigh_vertex_save.push_back(*neighbourIt);
  860. }
  861. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_delete,*gtmp);
  862. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  863. neigh_vertex_delete.push_back(*neighbourIt);
  864. }
  865. sort(neigh_vertex_save.begin(),neigh_vertex_save.end());
  866. sort(neigh_vertex_delete.begin(),neigh_vertex_delete.end());
  867. /*
  868. * Recherche de sommets communs entre le "sommet sauvegardé" et le "sommet à détruire"
  869. * S'il existe un tel sommet "v" alors on ajoute le poids de l'arcs (vertex_delet,v)
  870. * à celui de l'arcs (vertex_save,v) et on détruit l'arcs reliant "v" au "sommet à détruire"
  871. */
  872. for(uint j=0;j<neigh_vertex_delete.size();j++){
  873. if(In_tab_dichotomie(neigh_vertex_save,neigh_vertex_delete[j])==1){
  874. tie(e2,found)=edge(vertex(vertex_save,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  875. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  876. (*gtmp)[e2]._weight+=(*gtmp)[e1]._weight;
  877. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  878. }
  879. else
  880. {
  881. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  882. add_edge(vertex_save,neigh_vertex_delete[j],EdgeProperties((*gtmp)[e1]._weight),*gtmp);
  883. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  884. }
  885. }
  886. (*gtmp)[vertex_save]._weight+=(*gtmp)[vertex_delete]._weight; // ajout du poids du sommet détruit au sommet conservé
  887. /*
  888. * Vérouillage du "sommet sauvegardé" et du "sommet à détruire"
  889. */
  890. Index_Vertex.at(Random_list_vertices.at(i))=-1;
  891. Index_Vertex.at(best_vertexs)=-1;
  892. val_cpt--;
  893. //std::cout<<std::endl;
  894. }
  895. else{
  896. /*
  897. * Et si le sommet tiré ne possède pas de sommet adjacent non verrouillé
  898. * alors on l'ajoute à la liste de correspondance des sommets et on
  899. * le verrouille
  900. */
  901. Entiers *couple = new Entiers();
  902. couple->push_back(vertexs);
  903. tableau_de_correspondance->push_back(couple);
  904. Index_Vertex.at(Random_list_vertices.at(i))=-1;
  905. }
  906. /*std::cout<<"Poids noeud graphe contracté : "<<std::endl;
  907. tie(vertexIt, vertexEnd) = vertices(*gtmp);
  908. for (; vertexIt != vertexEnd; ++vertexIt) {
  909. std::cout << (*gtmp)[*vertexIt]._index
  910. << " -> ";
  911. std::cout << (*gtmp)[*vertexIt]._weight<<" ; ";
  912. }
  913. std::cout << std::endl;*/
  914. }
  915. if(val_cpt == val_reduc){
  916. for(uint j=i+1; j < nbr_vertex; j++){
  917. if(Index_Vertex.at(Random_list_vertices.at(j)) != -1){
  918. Entiers *couple = new Entiers();
  919. couple->push_back(Index_Vertex.at(Random_list_vertices.at(j)));
  920. tableau_de_correspondance->push_back(couple);}
  921. }
  922. break;
  923. }
  924. }
  925. std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
  926. // std::cout<<"\n"<<std::endl;
  927. /*
  928. * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
  929. * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
  930. * des sommets
  931. */
  932. for(int j=(sommets_a_detruire.size()-1);j>-1;j--){
  933. //std::cout<<"Noeuds a supprimer : "<<sommets_a_detruire.at(j)<<std::endl;
  934. remove_vertex(sommets_a_detruire[j],*gtmp);
  935. }
  936. // std::clog<<"Affichage avant tri "<<std::endl;
  937. // for(uint k = 0;k<tableau_de_correspondance->size();k++){
  938. // for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
  939. // std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
  940. // }
  941. // std::cout<<"\n"<<std::endl;
  942. // }
  943. std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
  944. // std::clog<<"Tableau de correspondance "<<std::endl;
  945. // for(uint k = 0;k<tableau_de_correspondance->size();k++){
  946. // for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
  947. // std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
  948. // }
  949. // std::cout<<"\n"<<std::endl;
  950. // }
  951. liste_corr.push_back(tableau_de_correspondance);
  952. // std::cout<<"\n"<<std::endl;
  953. baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
  954. if(val_cpt == val_reduc)
  955. return true;
  956. else
  957. return false;
  958. }
  959. bool contraction_HEM_max_degree_selection(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
  960. UnorientedGraph *gtmp = new UnorientedGraph();
  961. boost::copy_graph(*g, *gtmp);
  962. Entiers Random_list_vertices, Index_Vertex; // Initialisation du tableau de sommets rangés aléatoirements
  963. EntiersEntiers *tableau_de_correspondance = new EntiersEntiers();
  964. edge_t e1,e2; // Iterateurs sur les arcs
  965. bool found;
  966. uint nbr_vertex = num_vertices(*gtmp);
  967. Entiers sommets_a_detruire; // Initialisation d'un tableau pret à recevoir les "sommets à détruire"
  968. /*
  969. * Création d'un vecteur contenant l'ensemble des sommets du graphe. Ces sommets sont rangés
  970. * aléatoirement afin de simuler un tirage aléatoire
  971. */
  972. for (uint i=0 ; i<nbr_vertex ; i++){
  973. Random_list_vertices.push_back(i);
  974. Index_Vertex.push_back(i);}
  975. for (uint j=0 ; j<nbr_vertex-1 ; j++) {
  976. int rand_pos = rand()%(nbr_vertex-j)+j;
  977. int tmp = Random_list_vertices[j];
  978. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  979. Random_list_vertices[rand_pos] = tmp;
  980. }
  981. /*
  982. * Pour chaque sommet non verrouiller faire ....
  983. */
  984. //std::cout<<"Nouvelle contraction !!!"<<std::endl;
  985. for(uint i=0; i<nbr_vertex; i++){
  986. int vertexs = Index_Vertex.at(Random_list_vertices.at(i));
  987. //std::cout<<"Le sommet tiré est : "<<(*gtmp)[vertexs]._index<<" ça place est : "<<Random_list_vertices.at(i)<<" place : "<<i<<std::endl;
  988. if(vertexs!=-1){
  989. Entiers liste_voisin = Liste_adjacence_tests(*gtmp,vertexs,Index_Vertex); // Recherche des sommets adjacents au sommets tiré
  990. if(liste_voisin.size()!=0){
  991. /*
  992. * S'il en existe au mois un sommet adjacent au sommet tiré qui n'est pas verrouillé, on
  993. * choisi celui dont l'arc les reliants est le plus fort. Dans le cas où les arcs ont tous
  994. * le même poids, on selectionne le sommet d'index le plus petit
  995. */
  996. double poids_a = -1.;
  997. int best_vertexs = -1;
  998. for(uint j=0;j<liste_voisin.size();j++){
  999. tie(e1,found)=edge(vertex(vertexs,*gtmp),vertex(liste_voisin[j],*gtmp),*gtmp);
  1000. if((*gtmp)[e1]._weight>poids_a){
  1001. best_vertexs = liste_voisin[j];
  1002. poids_a = (*gtmp)[e1]._weight;
  1003. }
  1004. }
  1005. Entiers *couple = new Entiers(); // Initialisation du vecteur contenant le couple de sommet fusionné
  1006. /* Sélection du sommet possedant un degrès maximum */
  1007. std::pair<double,int> couple1, couple2, best_min, best_max;
  1008. couple1.first = Degree(*gtmp,vertexs);
  1009. couple1.second = vertexs;
  1010. couple2.first = Degree(*gtmp,best_vertexs);
  1011. couple2.second = best_vertexs;
  1012. best_min = std::min(couple1,couple2);
  1013. best_max = std::max(couple1,couple2);
  1014. int vertex_delete = best_min.second; // Sommet d'index le plus grand (qui sera détruit)
  1015. //std::cout<<"sommet détruit : "<<(*gtmp)[vertex_delete]._index<<std::endl;
  1016. int vertex_save = best_max.second; // Sommet d'identifiant le plus petit (qui sera conservé)
  1017. //std::cout<<"sommet sauvé : "<<(*gtmp)[vertex_save]._index<<std::endl;
  1018. sommets_a_detruire.push_back(vertex_delete); // On ajoute le sommet détruit au tableau des sommets à détruire
  1019. /*
  1020. * On ajoute au tableau "couple" le couple de sommet à fusionner
  1021. */
  1022. couple->push_back(vertex_save);
  1023. couple->push_back(vertex_delete);
  1024. tableau_de_correspondance->push_back(couple); // Ajout du "couple" à la liste de correspondance
  1025. remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
  1026. Entiers neigh_vertex_save; // Initialisation du vecteur contenant les sommets adjacents au "sommet sauvegardé"
  1027. Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
  1028. /*
  1029. * Remplissage de ces deux tableaux à l'aide de la fonction adjacent_vertices de boost graph
  1030. * [La création de ces tableaux est nécéssaire du fait que certains arcs sont détruit au cours
  1031. * du processus]
  1032. */
  1033. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
  1034. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1035. neigh_vertex_save.push_back(*neighbourIt);
  1036. }
  1037. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_delete,*gtmp);
  1038. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1039. neigh_vertex_delete.push_back(*neighbourIt);
  1040. }
  1041. sort(neigh_vertex_save.begin(),neigh_vertex_save.end());
  1042. sort(neigh_vertex_delete.begin(),neigh_vertex_delete.end());
  1043. /*
  1044. * Recherche de sommets communs entre le "sommet sauvegardé" et le "sommet à détruire"
  1045. * S'il existe un tel sommet "v" alors on ajoute le poids de l'arcs (vertex_delet,v)
  1046. * à celui de l'arcs (vertex_save,v) et on détruit l'arcs reliant "v" au "sommet à détruire"
  1047. */
  1048. for(uint j=0;j<neigh_vertex_delete.size();j++){
  1049. if(In_tab_dichotomie(neigh_vertex_save,neigh_vertex_delete[j])==1){
  1050. tie(e2,found)=edge(vertex(vertex_save,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1051. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1052. (*gtmp)[e2]._weight+=(*gtmp)[e1]._weight;
  1053. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1054. }
  1055. else
  1056. {
  1057. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1058. add_edge(vertex_save,neigh_vertex_delete[j],EdgeProperties((*gtmp)[e1]._weight),*gtmp);
  1059. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1060. }
  1061. }
  1062. (*gtmp)[vertex_save]._weight+=(*gtmp)[vertex_delete]._weight; // ajout du poids du sommet détruit au sommet conservé
  1063. /*
  1064. * Vérouillage du "sommet sauvegardé" et du "sommet à détruire"
  1065. */
  1066. Index_Vertex.at(Random_list_vertices.at(i))=-1;
  1067. Index_Vertex.at(best_vertexs)=-1;
  1068. val_cpt--;
  1069. }
  1070. else{
  1071. /*
  1072. * Et si le sommet tiré ne possède pas de sommet adjacent non verrouillé
  1073. * alors on l'ajoute à la liste de correspondance des sommets et on
  1074. * le verrouille
  1075. */
  1076. Entiers *couple = new Entiers();
  1077. couple->push_back(vertexs);
  1078. tableau_de_correspondance->push_back(couple);
  1079. Index_Vertex.at(Random_list_vertices.at(i))=-1;
  1080. }
  1081. }
  1082. if(val_cpt == val_reduc){
  1083. for(uint j=i+1; j < nbr_vertex; j++){
  1084. if(Index_Vertex.at(Random_list_vertices.at(j)) != -1){
  1085. Entiers *couple = new Entiers();
  1086. couple->push_back(Index_Vertex.at(Random_list_vertices.at(j)));
  1087. tableau_de_correspondance->push_back(couple);}
  1088. }
  1089. break;
  1090. }
  1091. }
  1092. std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
  1093. /*
  1094. * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
  1095. * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
  1096. * des sommets
  1097. */
  1098. for(int j=(sommets_a_detruire.size()-1);j>-1;j--){
  1099. remove_vertex(sommets_a_detruire[j],*gtmp);
  1100. }
  1101. std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
  1102. liste_corr.push_back(tableau_de_correspondance);
  1103. baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
  1104. if(val_cpt == val_reduc)
  1105. return true;
  1106. else
  1107. return false;
  1108. }
  1109. bool contraction_HEM_degree(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
  1110. UnorientedGraph *gtmp = new UnorientedGraph();
  1111. boost::copy_graph(*g, *gtmp);
  1112. Entiers Index_Vertex; // Initialisation du tableau de sommets rangés aléatoirements
  1113. std::vector<double> vertex_degree;
  1114. EntiersEntiers *tableau_de_correspondance = new EntiersEntiers();
  1115. edge_t e1,e2; // Iterateurs sur les arcs
  1116. bool found;
  1117. uint nbr_vertex = num_vertices(*gtmp);
  1118. Entiers sommets_a_detruire; // Initialisation d'un tableau pret à recevoir les "sommets à détruire"
  1119. int cpt = nbr_vertex;
  1120. /*
  1121. * Création d'un vecteur contenant l'ensemble des sommets du graphe. Ces sommets sont rangés
  1122. * aléatoirement afin de simuler un tirage aléatoire
  1123. */
  1124. /*for (uint i=0 ; i<nbr_vertex ; i++){
  1125. Random_list_vertices.push_back(i);
  1126. Index_Vertex.push_back(i);}
  1127. for (uint j=0 ; j<nbr_vertex-1 ; j++) {
  1128. int rand_pos = rand()%(nbr_vertex-j)+j;
  1129. int tmp = Random_list_vertices[j];
  1130. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  1131. Random_list_vertices[rand_pos] = tmp;
  1132. }*/
  1133. for (uint i=0 ; i<nbr_vertex ; i++){
  1134. Index_Vertex.push_back(i);
  1135. vertex_degree.push_back(Degree(*g,i));
  1136. }
  1137. while(cpt != 0){
  1138. double max_weight = *std::max_element(vertex_degree.begin(),vertex_degree.end());
  1139. int vertexs, compteur;
  1140. //std::cout<<"max_weight : "<<max_weight<<std::endl;
  1141. //Entiers Vertex_select;
  1142. for(uint id = 0; id <vertex_degree.size(); id++){
  1143. if(vertex_degree.at(id) == max_weight){
  1144. compteur = id;
  1145. vertexs = Index_Vertex.at(id);
  1146. break;
  1147. }
  1148. }
  1149. //std::cout<<"min : "<<max_weight<<" - compteur : "<<(*gtmp)[compteur]._index;
  1150. //std::cout<<"Le sommet tiré est : "<<(*gtmp)[vertexs]._index<<" ça place est : "<<compteur<<std::endl;
  1151. Entiers liste_voisin = Liste_adjacence_tests(*gtmp,vertexs,Index_Vertex); // Recherche des sommets adjacents au sommets tiré
  1152. if(liste_voisin.size() != 0){
  1153. /*
  1154. * S'il en existe au mois un sommet adjacent au sommet tiré qui n'est pas verrouillé, on
  1155. * choisi celui dont l'arc les reliants est le plus fort. Dans le cas où les arcs ont tous
  1156. * le même poids, on selectionne le sommet d'index le plus petit
  1157. */
  1158. std::vector<double> Neight_weight, Best_neight;
  1159. int best_vertexs;
  1160. for(uint j=0;j<liste_voisin.size();j++){
  1161. tie(e1,found)=edge(vertex(vertexs,*gtmp),vertex(liste_voisin[j],*gtmp),*gtmp);
  1162. Neight_weight.push_back((*gtmp)[e1]._weight);
  1163. }
  1164. max_weight = *std::max_element(Neight_weight.begin(),Neight_weight.end());
  1165. for(uint j=0;j<liste_voisin.size();j++){
  1166. if(Neight_weight.at(j) == max_weight)
  1167. Best_neight.push_back(liste_voisin.at(j));
  1168. }
  1169. if(Best_neight.size() > 1){
  1170. int ind;
  1171. double deg =1000000000;
  1172. double tmp_deg;
  1173. for(uint j=0;j<Best_neight.size();j++){
  1174. tmp_deg = Degree(*gtmp,Best_neight.at(j));
  1175. if(tmp_deg < deg){
  1176. deg = tmp_deg;
  1177. ind = j;
  1178. }
  1179. }
  1180. best_vertexs = Best_neight.at(ind);
  1181. }else{
  1182. best_vertexs = Best_neight.at(0);
  1183. }
  1184. //std::cout<<" -> "<<(*gtmp)[best_vertexs]._index;
  1185. Entiers *couple = new Entiers(); // Initialisation du vecteur contenant le couple de sommet fusionné
  1186. /* Sélection du sommet possedant un degrès maximum */
  1187. std::pair<double,int> couple1, couple2, best_min, best_max;
  1188. couple1.first = Degree(*gtmp,vertexs);
  1189. couple1.second = vertexs;
  1190. couple2.first = Degree(*gtmp,best_vertexs);
  1191. couple2.second = best_vertexs;
  1192. best_min = std::min(couple1,couple2);
  1193. best_max = std::max(couple1,couple2);
  1194. int vertex_delete = best_min.second; // Sommet d'index le plus grand (qui sera détruit)
  1195. //std::cout<<"sommet détruit : "<<(*gtmp)[vertex_delete]._index<<std::endl;
  1196. int vertex_save = best_max.second; // Sommet d'identifiant le plus petit (qui sera conservé)
  1197. //std::cout<<"sommet sauvé : "<<(*gtmp)[vertex_save]._index<<std::endl;
  1198. sommets_a_detruire.push_back(vertex_delete); // On ajoute le sommet détruit au tableau des sommets à détruire
  1199. /*
  1200. * On ajoute au tableau "couple" le couple de sommet à fusionner
  1201. */
  1202. couple->push_back(vertex_save);
  1203. couple->push_back(vertex_delete);
  1204. tableau_de_correspondance->push_back(couple); // Ajout du "couple" à la liste de correspondance
  1205. remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
  1206. Entiers neigh_vertex_save; // Initialisation du vecteur contenant les sommets adjacents au "sommet sauvegardé"
  1207. Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
  1208. /*
  1209. * Remplissage de ces deux tableaux à l'aide de la fonction adjacent_vertices de boost graph
  1210. * [La création de ces tableaux est nécéssaire du fait que certains arcs sont détruit au cours
  1211. * du processus]
  1212. */
  1213. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
  1214. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1215. neigh_vertex_save.push_back(*neighbourIt);
  1216. }
  1217. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_delete,*gtmp);
  1218. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1219. neigh_vertex_delete.push_back(*neighbourIt);
  1220. }
  1221. sort(neigh_vertex_save.begin(),neigh_vertex_save.end());
  1222. sort(neigh_vertex_delete.begin(),neigh_vertex_delete.end());
  1223. /*
  1224. * Recherche de sommets communs entre le "sommet sauvegardé" et le "sommet à détruire"
  1225. * S'il existe un tel sommet "v" alors on ajoute le poids de l'arcs (vertex_delet,v)
  1226. * à celui de l'arcs (vertex_save,v) et on détruit l'arcs reliant "v" au "sommet à détruire"
  1227. */
  1228. for(uint j=0;j<neigh_vertex_delete.size();j++){
  1229. if(In_tab_dichotomie(neigh_vertex_save,neigh_vertex_delete[j])==1){
  1230. tie(e2,found)=edge(vertex(vertex_save,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1231. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1232. (*gtmp)[e2]._weight+=(*gtmp)[e1]._weight;
  1233. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1234. }
  1235. else
  1236. {
  1237. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1238. add_edge(vertex_save,neigh_vertex_delete[j],EdgeProperties((*gtmp)[e1]._weight),*gtmp);
  1239. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1240. }
  1241. }
  1242. (*gtmp)[vertex_save]._weight+=(*gtmp)[vertex_delete]._weight; // ajout du poids du sommet détruit au sommet conservé
  1243. /*
  1244. * Vérouillage du "sommet sauvegardé" et du "sommet à détruire"
  1245. */
  1246. Index_Vertex.at(compteur)=-1;
  1247. vertex_degree.at(compteur)=-1;
  1248. Index_Vertex.at(best_vertexs)=-1;
  1249. vertex_degree.at(best_vertexs)=-1;
  1250. val_cpt--;
  1251. cpt -= 2;
  1252. //std::cout<<" ** "<<std::endl;
  1253. //std::cout<<cpt<<std::endl;
  1254. }
  1255. else{
  1256. /*
  1257. * Et si le sommet tiré ne possède pas de sommet adjacent non verrouillé
  1258. * alors on l'ajoute à la liste de correspondance des sommets et on
  1259. * le verrouille
  1260. */
  1261. Entiers *couple = new Entiers();
  1262. couple->push_back(vertexs);
  1263. tableau_de_correspondance->push_back(couple);
  1264. Index_Vertex.at(compteur)=-1;
  1265. vertex_degree.at(compteur)=-1;
  1266. cpt --;
  1267. //std::cout<<" * "<<std::endl;
  1268. }
  1269. if(val_cpt == val_reduc){
  1270. for(uint j=0; j < nbr_vertex; j++){
  1271. if(Index_Vertex.at(j) != -1){
  1272. Entiers *couple = new Entiers();
  1273. couple->push_back(Index_Vertex.at(j));
  1274. tableau_de_correspondance->push_back(couple);}
  1275. }
  1276. break;
  1277. }
  1278. }
  1279. //std::cout<<"cpt : "<<cpt<<std::endl;
  1280. std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
  1281. /*
  1282. * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
  1283. * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
  1284. * des sommets
  1285. */
  1286. for(int j=(sommets_a_detruire.size()-1);j>-1;j--){
  1287. remove_vertex(sommets_a_detruire[j],*gtmp);
  1288. }
  1289. std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
  1290. liste_corr.push_back(tableau_de_correspondance);
  1291. baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
  1292. if(val_cpt == val_reduc)
  1293. return true;
  1294. else
  1295. return false;
  1296. }
  1297. bool contraction_HEM_mds_ameliore_KK(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
  1298. UnorientedGraph *gtmp = new UnorientedGraph();
  1299. boost::copy_graph(*g, *gtmp);
  1300. Entiers Random_list_vertices, Index_Vertex; // Initialisation du tableau de sommets rangés aléatoirements
  1301. EntiersEntiers *tableau_de_correspondance = new EntiersEntiers();
  1302. edge_t e1,e2; // Iterateurs sur les arcs
  1303. bool found;
  1304. uint nbr_vertex = num_vertices(*gtmp);
  1305. Entiers sommets_a_detruire; // Initialisation d'un tableau pret à recevoir les "sommets à détruire"
  1306. /*
  1307. * Création d'un vecteur contenant l'ensemble des sommets du graphe. Ces sommets sont rangés
  1308. * aléatoirement afin de simuler un tirage aléatoire
  1309. */
  1310. for (uint i=0 ; i<nbr_vertex ; i++){
  1311. Random_list_vertices.push_back(i);
  1312. Index_Vertex.push_back(i);}
  1313. for (uint j=0 ; j<nbr_vertex-1 ; j++) {
  1314. int rand_pos = rand()%(nbr_vertex-j)+j;
  1315. int tmp = Random_list_vertices[j];
  1316. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  1317. Random_list_vertices[rand_pos] = tmp;
  1318. }
  1319. /*
  1320. * Pour chaque sommet non verrouiller faire ....
  1321. */
  1322. //std::cout<<"Nouvelle contraction !!!"<<std::endl;
  1323. for(uint i=0; i<nbr_vertex; i++){
  1324. int vertexs = Index_Vertex.at(Random_list_vertices.at(i));
  1325. //std::cout<<"Le sommet tiré est : "<<(*gtmp)[vertexs]._index<<" ça place est : "<<Random_list_vertices.at(i)<<" place : "<<i<<std::endl;
  1326. if(vertexs!=-1){
  1327. Entiers liste_voisin = Liste_adjacence_tests(*gtmp,vertexs,Index_Vertex); // Recherche des sommets adjacents au sommets tiré
  1328. sort(liste_voisin.begin(),liste_voisin.end());
  1329. if(liste_voisin.size()!=0){
  1330. /*
  1331. * S'il en existe au mois un sommet adjacent au sommet tiré qui n'est pas verrouillé, on
  1332. * choisi celui dont l'arc les reliants est le plus fort. Dans le cas où les arcs ont tous
  1333. * le même poids, on selectionne le sommet d'index le plus petit
  1334. */
  1335. /*std::cout<<"Le sommet tiré a des voisins "<<std::endl;
  1336. for(uint id = 0; id<liste_voisin.size(); id++){
  1337. std::cout<<(*gtmp)[liste_voisin.at(id)]._index<<" ";
  1338. }
  1339. std::cout<<std::endl;*/
  1340. double poids_a = -1.;
  1341. std::vector<double> adjacent_weight;
  1342. //std::cout<<"adjacent_weight"<<std::endl;
  1343. for(uint j=0;j<liste_voisin.size();j++){
  1344. tie(e1,found)=edge(vertex(vertexs,*gtmp),vertex(liste_voisin[j],*gtmp),*gtmp);
  1345. adjacent_weight.push_back((*gtmp)[e1]._weight);
  1346. //std::cout<<(*gtmp)[e1]._weight<<" ";
  1347. }
  1348. //std::cout<<std::endl;
  1349. //std::cout<<"Top *"<<std::endl;
  1350. double max_weight = *std::max_element(adjacent_weight.begin(),adjacent_weight.end());
  1351. //std::cout<<"max_weight : "<<max_weight<<std::endl;
  1352. Entiers Vertex_select;
  1353. for(uint id = 0; id <adjacent_weight.size(); id++){
  1354. if(adjacent_weight.at(id) == max_weight)
  1355. Vertex_select.push_back(liste_voisin.at(id));
  1356. }
  1357. int index = 0;
  1358. if(Vertex_select.size()>1){
  1359. //std::cout<<"Top **"<<std::endl;
  1360. for(uint id = 0; id<Vertex_select.size(); id++){
  1361. suprim_val(liste_voisin,Vertex_select.at(id)); /*** modification possible ***/
  1362. }
  1363. //std::cout<<"Top ***"<<std::endl;
  1364. adjacent_weight.clear();
  1365. for(uint id_Vs = 0; id_Vs<Vertex_select.size(); id_Vs++){
  1366. double neigh_weight = 0.;
  1367. for(uint id_Lv = 0; id_Lv<liste_voisin.size(); id_Lv++){
  1368. //std::cout<<"Top ***!"<<std::endl;
  1369. bool rep = Est_voisin(gtmp,liste_voisin.at(id_Lv),Vertex_select.at(id_Vs));
  1370. if(rep == true){
  1371. tie(e1,found)=edge(vertex(Vertex_select.at(id_Vs),*gtmp),vertex(liste_voisin.at(id_Lv),*gtmp),*gtmp);
  1372. //std::cout<<"Top ***!!"<<std::endl;
  1373. //std::cout<<e1<<std::endl;
  1374. //std::cout<<"Top ***!!!"<<std::endl;
  1375. neigh_weight += (*gtmp)[e1]._weight;
  1376. }
  1377. //std::cout<<"Top ***!!!!"<<std::endl;
  1378. }
  1379. adjacent_weight.push_back(neigh_weight);
  1380. }
  1381. //std::cout<<"Top ****"<<std::endl;
  1382. max_weight = *std::max_element(adjacent_weight.begin(),adjacent_weight.end());
  1383. for(uint id = 0; id <adjacent_weight.size(); id++){
  1384. if(adjacent_weight.at(id) == max_weight){
  1385. index = id;
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. //std::cout<<"Index "<<index<<std::endl;
  1391. //std::cout<<"Top *****"<<std::endl;
  1392. int best_vertexs = Vertex_select.at(index);
  1393. //std::cout<<"Index "<<best_vertexs<<std::endl;
  1394. Entiers *couple = new Entiers(); // Initialisation du vecteur contenant le couple de sommet fusionné
  1395. /* Sélection du sommet possedant un degrès maximum */
  1396. std::pair<double,int> couple1, couple2, best_min, best_max;
  1397. couple1.first = Degree(*gtmp,vertexs);
  1398. couple1.second = vertexs;
  1399. couple2.first = Degree(*gtmp,best_vertexs);
  1400. couple2.second = best_vertexs;
  1401. best_min = std::min(couple1,couple2);
  1402. best_max = std::max(couple1,couple2);
  1403. int vertex_delete = best_min.second; // Sommet d'index le plus grand (qui sera détruit)
  1404. //std::cout<<"sommet détruit : "<<(*gtmp)[vertex_delete]._index<<std::endl;
  1405. int vertex_save = best_max.second; // Sommet d'identifiant le plus petit (qui sera conservé)
  1406. //std::cout<<"sommet sauvé : "<<(*gtmp)[vertex_save]._index<<std::endl;
  1407. sommets_a_detruire.push_back(vertex_delete); // On ajoute le sommet détruit au tableau des sommets à détruire
  1408. /*
  1409. * On ajoute au tableau "couple" le couple de sommet à fusionner
  1410. */
  1411. couple->push_back(vertex_save);
  1412. couple->push_back(vertex_delete);
  1413. tableau_de_correspondance->push_back(couple); // Ajout du "couple" à la liste de correspondance
  1414. remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
  1415. Entiers neigh_vertex_save; // Initialisation du vecteur contenant les sommets adjacents au "sommet sauvegardé"
  1416. Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
  1417. /*
  1418. * Remplissage de ces deux tableaux à l'aide de la fonction adjacent_vertices de boost graph
  1419. * [La création de ces tableaux est nécéssaire du fait que certains arcs sont détruit au cours
  1420. * du processus]
  1421. */
  1422. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
  1423. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1424. neigh_vertex_save.push_back(*neighbourIt);
  1425. }
  1426. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_delete,*gtmp);
  1427. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1428. neigh_vertex_delete.push_back(*neighbourIt);
  1429. }
  1430. sort(neigh_vertex_save.begin(),neigh_vertex_save.end());
  1431. sort(neigh_vertex_delete.begin(),neigh_vertex_delete.end());
  1432. /*
  1433. * Recherche de sommets communs entre le "sommet sauvegardé" et le "sommet à détruire"
  1434. * S'il existe un tel sommet "v" alors on ajoute le poids de l'arcs (vertex_delet,v)
  1435. * à celui de l'arcs (vertex_save,v) et on détruit l'arcs reliant "v" au "sommet à détruire"
  1436. */
  1437. for(uint j=0;j<neigh_vertex_delete.size();j++){
  1438. if(In_tab_dichotomie(neigh_vertex_save,neigh_vertex_delete[j])==1){
  1439. tie(e2,found)=edge(vertex(vertex_save,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1440. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1441. (*gtmp)[e2]._weight+=(*gtmp)[e1]._weight;
  1442. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1443. }
  1444. else
  1445. {
  1446. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1447. add_edge(vertex_save,neigh_vertex_delete[j],EdgeProperties((*gtmp)[e1]._weight),*gtmp);
  1448. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1449. }
  1450. }
  1451. (*gtmp)[vertex_save]._weight+=(*gtmp)[vertex_delete]._weight; // ajout du poids du sommet détruit au sommet conservé
  1452. /*
  1453. * Vérouillage du "sommet sauvegardé" et du "sommet à détruire"
  1454. */
  1455. Index_Vertex.at(Random_list_vertices.at(i))=-1;
  1456. Index_Vertex.at(best_vertexs)=-1;
  1457. val_cpt--;
  1458. }
  1459. else{
  1460. /*
  1461. * Et si le sommet tiré ne possède pas de sommet adjacent non verrouillé
  1462. * alors on l'ajoute à la liste de correspondance des sommets et on
  1463. * le verrouille
  1464. */
  1465. //std::cout<<"Le sommet tiré est isolé "<<std::endl;
  1466. Entiers *couple = new Entiers();
  1467. couple->push_back(vertexs);
  1468. tableau_de_correspondance->push_back(couple);
  1469. Index_Vertex.at(Random_list_vertices.at(i))=-1;
  1470. }
  1471. }else{
  1472. //std::cout<<"Le sommet est bloqué "<<std::endl;
  1473. //std::cout<<" ça place est : "<<Random_list_vertices.at(i)<<" valeur : "<<Index_Vertex.at(Random_list_vertices.at(i))<<std::endl;
  1474. }
  1475. if(val_cpt == val_reduc){
  1476. //std::cout<<"Taille obtenue !"<<std::endl;
  1477. for(uint j=i+1; j < nbr_vertex; j++){
  1478. if(Index_Vertex.at(Random_list_vertices.at(j)) != -1){
  1479. Entiers *couple = new Entiers();
  1480. couple->push_back(vertexs);
  1481. tableau_de_correspondance->push_back(couple);}
  1482. }
  1483. break;
  1484. }
  1485. }
  1486. std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
  1487. /*
  1488. * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
  1489. * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
  1490. * des sommets
  1491. */
  1492. for(int j=(sommets_a_detruire.size()-1);j>-1;j--){
  1493. remove_vertex(sommets_a_detruire[j],*gtmp);
  1494. }
  1495. std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
  1496. liste_corr.push_back(tableau_de_correspondance);
  1497. baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
  1498. if(val_cpt == val_reduc)
  1499. return true;
  1500. else
  1501. return false;
  1502. }
  1503. bool Est_voisin(UnorientedGraph *g, int vertex, int vertex_select){
  1504. bool reponse = false;
  1505. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex, *g);
  1506. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1507. if(*neighbourIt == vertex_select)
  1508. reponse = true;
  1509. }
  1510. return reponse;
  1511. }
  1512. bool contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
  1513. UnorientedGraph *gtmp = new UnorientedGraph();
  1514. *gtmp=*g;
  1515. Entiers Random_list_vertices; // Initialisation du tableau de sommets rangés aléatoirements
  1516. EntiersEntiers *tableau_de_correspondance = new EntiersEntiers();
  1517. edge_t e1,e2; // Iterateurs sur les arcs
  1518. bool found;
  1519. uint nbr_vertex = num_vertices(*gtmp);
  1520. Entiers sommets_a_detruire; // Initialisation d'un tableau pret à recevoir les "sommets à détruire"
  1521. /*
  1522. * Création d'un vecteur contenant l'ensemble des sommets du graphe. Ces sommets sont rangés
  1523. * aléatoirement afin de simuler un tirage aléatoire
  1524. */
  1525. for (uint i=0 ; i<nbr_vertex ; i++)
  1526. Random_list_vertices.push_back(i);
  1527. for (uint j=0 ; j<nbr_vertex-1 ; j++) {
  1528. int rand_pos = rand()%(nbr_vertex-j)+j;
  1529. int tmp = Random_list_vertices[j];
  1530. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  1531. Random_list_vertices[rand_pos] = tmp;
  1532. }
  1533. /*
  1534. * Pour chaque sommet non verrouiller faire ....
  1535. */
  1536. for(uint i=0; i<nbr_vertex; i++){
  1537. int vertexs = Random_list_vertices[i];
  1538. if(vertexs!=-1){
  1539. Entiers liste_voisin = Liste_adjacence(*gtmp,vertexs,Random_list_vertices); // Recherche des sommets adjacents au sommets tiré
  1540. if(liste_voisin.size()!=0){
  1541. /*
  1542. * S'il en existe au mois un sommet adjacent au sommet tiré qui n'est pas verrouillé, on
  1543. * choisi celui dont l'arc les reliants est le plus fort. Dans le cas où les arcs ont tous
  1544. * le même poids, on selectionne le sommet d'identifiant le plus petit
  1545. */
  1546. int tmp;
  1547. if(liste_voisin.size()==1)
  1548. tmp = 0;
  1549. else
  1550. tmp = rand_fini(0,liste_voisin.size()-1);
  1551. int best_vertexs = liste_voisin.at(tmp);
  1552. Entiers * couple = new Entiers(); // Initialisation du vecteur contenant le couple de sommet fusionné
  1553. int vertex_delete = std::max(vertexs, best_vertexs); // Sommet d'indentifiant le plus grand (qui sera détruit)
  1554. //std::cout<<"sommet détruit : "<<vertex_delete<<std::endl;
  1555. int vertex_save = std::min(vertexs,best_vertexs); // Sommet d'identifiant le plus petit (qui sera conservé)
  1556. //std::cout<<"sommet sauvé : "<<vertex_save<<std::endl;
  1557. sommets_a_detruire.push_back(vertex_delete); // On ajoute le sommet détruit au tableau des sommets à détruire
  1558. /*
  1559. * On ajoute au tableau "couple" le couple de sommet à fusionner
  1560. */
  1561. couple->push_back(vertex_save);
  1562. couple->push_back(vertex_delete);
  1563. tableau_de_correspondance->push_back(couple); // Ajout du "couple" à la liste de correspondance
  1564. remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
  1565. Entiers neigh_vertex_save; // Initialisation du vecteur contenant les somemts adjacents au "sommet sauvegardé"
  1566. Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
  1567. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
  1568. /*
  1569. * Remplissage de ces deux tableaux à l'aide de la fonction adjacent_vertices de boost graph
  1570. * [La création de ces tableaux est nécéssaire du fait que certains arcs sont détruit au cours
  1571. * du processus]
  1572. */
  1573. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1574. neigh_vertex_save.push_back(*neighbourIt);
  1575. }
  1576. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_delete,*gtmp);
  1577. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1578. neigh_vertex_delete.push_back(*neighbourIt);
  1579. }
  1580. /*
  1581. * Recherche de sommets communs entre le "sommet sauvegardé" et le "sommet à détruire"
  1582. * S'il existe un tel sommet "v" alors on ajoute le poids de l'arcs (vertex_delet,v)
  1583. * à celui de l'arcs (vertex_save,v) et on détruit l'arcs reliant "v" au "sommet à détruire"
  1584. */
  1585. for(uint j=0;j<neigh_vertex_delete.size();j++){
  1586. if(In_tab(neigh_vertex_save,neigh_vertex_delete[j])==1){
  1587. tie(e2,found)=edge(vertex(vertex_save,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1588. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1589. (*gtmp)[e2]._weight+=(*gtmp)[e1]._weight;
  1590. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1591. }
  1592. else
  1593. {
  1594. tie(e1,found)=edge(vertex(vertex_delete,*gtmp),vertex(neigh_vertex_delete[j],*gtmp),*gtmp);
  1595. add_edge(vertex_save,neigh_vertex_delete[j],EdgeProperties((*gtmp)[e1]._weight),*gtmp);
  1596. remove_edge(vertex_delete,neigh_vertex_delete[j],*gtmp);
  1597. }
  1598. }
  1599. (*gtmp)[vertex_save]._weight+=(*gtmp)[vertex_delete]._weight; // ajout du poids du sommet détruit au sommet conservé
  1600. /*
  1601. * Vérouillage du "sommet sauvegardé" et du "sommet à détruire"
  1602. */
  1603. Random_list_vertices[i]=-1;
  1604. Random_list_vertices[recherche_val(Random_list_vertices,best_vertexs)]=-1;
  1605. val_cpt--;
  1606. // std::cout<<val_cpt<<std::endl;
  1607. }
  1608. else{
  1609. /*
  1610. * Et si le sommet tiré ne possède pas de sommet adjacent non verrouillé
  1611. * alors on l'ajoute à la liste de correspondance des sommets et on
  1612. * le verrouille
  1613. */
  1614. Entiers *couple = new Entiers();
  1615. couple->push_back(Random_list_vertices.at(i));
  1616. tableau_de_correspondance->push_back(couple);
  1617. Random_list_vertices[i]=-1;
  1618. }
  1619. }
  1620. if(val_cpt == val_reduc){
  1621. for(uint j=i+1; j <nbr_vertex; j++){
  1622. if(Random_list_vertices[j] !=-1){
  1623. Entiers *couple = new Entiers();
  1624. couple->push_back(Random_list_vertices.at(j));
  1625. tableau_de_correspondance->push_back(couple);}
  1626. }
  1627. break;
  1628. }
  1629. }
  1630. std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
  1631. //std::cout<<"\n"<<std::endl;
  1632. /*
  1633. * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
  1634. * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
  1635. * des sommets
  1636. */
  1637. for(int j=(sommets_a_detruire.size()-1);j>-1;j--){
  1638. //std::cout<<"Noeuds a supprimer : "<<sommets_a_detruire.at(j)<<std::endl;
  1639. remove_vertex(sommets_a_detruire[j],*gtmp);
  1640. }
  1641. /**std::clog<<"Affichage avant tri "<<std::endl;
  1642. for(uint k = 0;k<tableau_de_correspondance->size();k++){
  1643. for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
  1644. std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
  1645. }
  1646. std::cout<<"\n"<<std::endl;
  1647. }*/
  1648. std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
  1649. // std::clog<<"Tableau de correspondance "<<std::endl;
  1650. // for(uint k = 0;k<tableau_de_correspondance->size();k++){
  1651. // for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
  1652. // std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
  1653. // }
  1654. // std::cout<<"\n"<<std::endl;
  1655. // }
  1656. liste_corr.push_back(tableau_de_correspondance);
  1657. // std::cout<<"\n"<<std::endl;
  1658. baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
  1659. if(val_cpt == val_reduc)
  1660. return true;
  1661. else
  1662. return false;
  1663. }
  1664. Entiers Liste_adjacence(UnorientedGraph &g, int vertexs,const Entiers &random_vertices){ // a revoir !!!!
  1665. Entiers liste_voisin;
  1666. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertexs, g);
  1667. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1668. if(In_tab(random_vertices,*neighbourIt)==1)
  1669. liste_voisin.push_back(*neighbourIt);
  1670. }
  1671. return liste_voisin;
  1672. }
  1673. Entiers Liste_adjacence_tests(UnorientedGraph &g, int vertexs,const Entiers &Index_Vertex){ // a revoir !!!!
  1674. Entiers liste_voisin;
  1675. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertexs, g);
  1676. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1677. if(Index_Vertex.at(*neighbourIt)!=-1)
  1678. liste_voisin.push_back(*neighbourIt);
  1679. }
  1680. return liste_voisin;
  1681. }
  1682. int rand_fini(int a, int b){
  1683. return rand()%(b-a)+a;
  1684. }
  1685. /**
  1686. * Fonction de recherche d'une valeur dans un tableau.
  1687. * @param tab
  1688. * @param val
  1689. * @return
  1690. */
  1691. int recherche_val2(const std::vector<float> &tab,float val){
  1692. int cpt=0;
  1693. while(tab[cpt]!=val)
  1694. cpt++;
  1695. return cpt;
  1696. }
  1697. int recherche_val_double(const std::vector<double> &tab,double val){
  1698. int cpt=0;
  1699. while(tab[cpt]!=val)
  1700. cpt++;
  1701. return cpt;
  1702. }
  1703. int recherche_val(const Entiers &tab,int val){
  1704. int cpt=0;
  1705. while(tab[cpt]!=val)
  1706. cpt++;
  1707. return cpt;
  1708. }
  1709. /**
  1710. * @param tab
  1711. * @param i
  1712. * @return
  1713. */
  1714. int dichotomie(const Entiers &tab, int i){
  1715. /* déclaration des variables locales à la fonction */
  1716. int id; //indice de début
  1717. int ifin; //indice de fin
  1718. int im; //indice de "milieu"
  1719. /* initialisation de ces variables avant la boucle de recherche */
  1720. id = 0; //intervalle de recherche compris entre 0...
  1721. ifin = tab.size(); //...et nbVal
  1722. /* boucle de recherche */
  1723. while ((ifin - id) > 1){
  1724. im = (id + ifin)/2; //on détermine l'indice de milieu
  1725. if(tab[im] > i) ifin = im; //si la valeur qui est à la case "im" est supérieure à la valeur recherchée, l'indice de fin "ifin" << devient >> l'indice de milieu, ainsi l'intervalle de recherche est restreint lors du prochain tour de boucle
  1726. else id = im; //sinon l'indice de début << devient >> l'indice de milieu et l'intervalle est de la même façon restreint
  1727. }
  1728. /* test conditionnant la valeur que la fonction va renvoyer */
  1729. if (tab[id] == i) return id; //si on a trouvé la bonne valeur, on retourne l'indice
  1730. else return -1; //sinon on retourne -1
  1731. }
  1732. /**
  1733. * Fonction permettant de supprimer une case d'un tableau.
  1734. * @param tab une référence sur un tableau d'entiers
  1735. * @param i un indice dans tab
  1736. */
  1737. void suprim_val(Entiers &tab,int i) {
  1738. tab.erase(tab.begin() + dichotomie(tab,i));
  1739. }
  1740. /**
  1741. * Détermine si une valeur se trouve dans un tableau.
  1742. * @param tab une référence sur un tableau d'entiers
  1743. * @param val une valeur
  1744. * @return true si la valeur a été trouvée, false sinon
  1745. */
  1746. bool In_tab(const Entiers &tab, int val)
  1747. {
  1748. for (uint i=0; i < tab.size(); i++)
  1749. if(tab[i]==val)
  1750. return true;
  1751. return false;
  1752. }
  1753. bool In_tab_dichotomie(const Entiers &tab, int val)
  1754. {
  1755. if(dichotomie(tab,val)!=-1)
  1756. return true;
  1757. else
  1758. return false;
  1759. }
  1760. void Liste_Voisin(const Entiers &P,Entiers &tab,const UnorientedGraph &g)
  1761. {
  1762. tie(neighbourIt, neighbourEnd) = adjacent_vertices(P.at(P.size()-1), g);
  1763. for (; neighbourIt != neighbourEnd; ++neighbourIt)
  1764. {
  1765. if((In_tab(tab,*neighbourIt) == false ) && (In_tab(P,*neighbourIt) == false ))
  1766. tab.push_back(*neighbourIt);
  1767. }
  1768. }
  1769. int Cout_coupe(Entiers P,int val, UnorientedGraph &g)
  1770. {
  1771. int cpt=0;
  1772. P.push_back(val);
  1773. for(uint i=0;i<P.size();i++){
  1774. tie(neighbourIt, neighbourEnd) = adjacent_vertices(P[i], g);
  1775. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1776. if(In_tab(P,*neighbourIt)!=1){
  1777. cpt++;
  1778. }
  1779. }
  1780. }
  1781. return cpt;
  1782. }
  1783. double Cut_one_cluster(const Entiers &cluster, UnorientedGraph &g, std::string name)
  1784. {
  1785. if(name=="norm"){
  1786. edge_t e1;
  1787. bool found;
  1788. double cpt=0.;
  1789. for(uint i=0;i<cluster.size();i++){
  1790. tie(neighbourIt, neighbourEnd) = adjacent_vertices(cluster.at(i), g);
  1791. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1792. tie(e1,found)=edge(vertex(cluster[i],g),vertex(*neighbourIt,g),g);
  1793. if(In_tab(cluster,*neighbourIt)!=1){
  1794. cpt+=g[e1]._weight;
  1795. }
  1796. }
  1797. }
  1798. double deg = Cluster_Degree(g,cluster);
  1799. return cpt/deg;
  1800. }
  1801. else if(name == "cut"){
  1802. edge_t e1;
  1803. bool found;
  1804. double cpt=0.;
  1805. for(uint i=0;i<cluster.size();i++){
  1806. tie(neighbourIt, neighbourEnd) = adjacent_vertices(cluster.at(i), g);
  1807. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1808. tie(e1,found)=edge(vertex(cluster.at(i),g),vertex(*neighbourIt,g),g);
  1809. if(In_tab(cluster,*neighbourIt)!=1){
  1810. cpt+=g[e1]._weight;
  1811. }
  1812. }
  1813. }
  1814. return cpt/2.;
  1815. }
  1816. else if(name == "ratio"){
  1817. edge_t e1;
  1818. bool found;
  1819. double cpt=0.;
  1820. for(uint i=0;i<cluster.size();i++){
  1821. tie(neighbourIt, neighbourEnd) = adjacent_vertices(cluster.at(i), g);
  1822. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1823. tie(e1,found)=edge(vertex(cluster.at(i),g),vertex(*neighbourIt,g),g);
  1824. if(In_tab(cluster,*neighbourIt)!=1){
  1825. cpt+=g[e1]._weight;
  1826. }
  1827. }
  1828. }
  1829. double vol = Cluster_Weight(g,cluster);
  1830. return (cpt/2.)/vol;
  1831. }
  1832. /*Vérification de la formule : doute sur le /2.*/
  1833. }
  1834. double Cut_cluster(const EntiersEntiers &tab_cluster,UnorientedGraph &g,std::string name)
  1835. {
  1836. double cpt=0.;
  1837. for(uint i=0;i<tab_cluster.size();i++){
  1838. cpt+=Cut_one_cluster(*tab_cluster[i],g,name);
  1839. }
  1840. return cpt;
  1841. }
  1842. double Cout_coupe_pond(Entiers P, int val, UnorientedGraph &g)
  1843. {
  1844. edge_t e1;
  1845. bool found;
  1846. double cpt=0;
  1847. P.push_back(val);
  1848. for(uint i=0;i<P.size();i++){
  1849. tie(neighbourIt, neighbourEnd) = adjacent_vertices(P[i], g);
  1850. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1851. tie(e1,found)=edge(vertex(P[i],g),vertex(*neighbourIt,g),g);
  1852. if(In_tab(P,*neighbourIt)!=1){
  1853. cpt+=g[e1]._weight;
  1854. }
  1855. }
  1856. }
  1857. return cpt;
  1858. }
  1859. int In_community_dichotomie(const EntiersEntiers &part, int val)
  1860. {
  1861. for (uint i = 0; i < part.size() ; i++) {
  1862. if (In_tab_dichotomie(*part[i], val)) {
  1863. return i;
  1864. }
  1865. }
  1866. return -1;
  1867. }
  1868. double Degree(UnorientedGraph& g, int node)
  1869. {
  1870. edge_t e1;
  1871. bool found;
  1872. double val = 0.;
  1873. tie(neighbourIt, neighbourEnd) = adjacent_vertices(node, g);
  1874. for (; neighbourIt != neighbourEnd; ++neighbourIt) {
  1875. tie(e1, found) = edge(vertex(node, g), vertex(*neighbourIt, g), g);
  1876. val += g[e1]._weight;
  1877. }
  1878. return val;
  1879. }
  1880. double Cluster_Degree(UnorientedGraph &g , const Entiers &cluster)
  1881. {
  1882. double val = 0.;
  1883. for(uint i = 0; i < cluster.size(); i++){
  1884. val += Degree(g, cluster.at(i));
  1885. }
  1886. return val;
  1887. }
  1888. double Cluster_Weight(UnorientedGraph &g , const Entiers &cluster)
  1889. {
  1890. double val = 0.;
  1891. for(uint i = 0; i < cluster.size(); i++){
  1892. val += g[cluster.at(i)]._weight;;
  1893. }
  1894. return val;
  1895. }
  1896. void List_edge_partie(Entiers *Partie, OrientedGraph *go, Edges &edge_partie,
  1897. OutputEdges &outputedgespartie){
  1898. edge_to e1;
  1899. //bool found;
  1900. for(uint i = 0; i < Partie->size(); i++) {
  1901. tie(neighbourIto, neighbourEndo) = adjacent_vertices(Partie->at(i),
  1902. *go);
  1903. for (; neighbourIto != neighbourEndo; ++neighbourIto) {
  1904. if(In_tab_dichotomie(*Partie,*neighbourIto)) {
  1905. Edge new_edge;
  1906. new_edge.first = Partie->at(i);
  1907. new_edge.second = *neighbourIto;
  1908. edge_partie.push_back(new_edge);
  1909. } else {
  1910. Edge new_edge;
  1911. new_edge.first = Partie->at(i);
  1912. new_edge.second = *neighbourIto;
  1913. outputedgespartie.push_back(new_edge);
  1914. }
  1915. }
  1916. }
  1917. }
  1918. void Global_Neigh_community(UnorientedGraph *g,
  1919. const EntiersEntiers &Partition,
  1920. Entiers *community, int vertex, int comm_in)
  1921. {
  1922. int comm;
  1923. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex, *g);
  1924. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  1925. comm = In_community_dichotomie(Partition, *neighbourIt);
  1926. if (In_tab(*community,comm) != 1 and comm != comm_in)
  1927. community->push_back(comm);
  1928. }
  1929. }
  1930. OrientedGraphs Graph_Partition(const EntiersEntiers& Partition,
  1931. OrientedGraph *go,
  1932. UnorientedGraph *g,
  1933. OutputEdgeList &outputedgelist,
  1934. InputEdgeList &inputedgelist,
  1935. Connections &connections)
  1936. {
  1937. OrientedGraphs graph_partie;
  1938. EntiersEntiers neigh_community;
  1939. for (uint i = 0; i < Partition.size();i++){
  1940. Edges edge_partie;
  1941. List_edge_partie(Partition.at(i),go,edge_partie,outputedgelist.at(i));
  1942. OrientedGraph graph;
  1943. std::vector<vertex_to> tab_vertex_to;
  1944. Entiers *community = new Entiers();
  1945. for (uint j = 0; j < Partition.at(i)->size(); j++) {
  1946. Global_Neigh_community(g, Partition, community,
  1947. Partition.at(i)->at(j),i);
  1948. vertex_to v = add_vertex(graph);
  1949. tab_vertex_to.push_back(v);
  1950. graph[v] = VertexProperties((*go)[Partition.at(i)->at(j)]._index,
  1951. (*go)[Partition.at(i)->at(j)]._weight,
  1952. (*go)[Partition.at(i)->at(j)]._type);
  1953. }
  1954. neigh_community.push_back(community);
  1955. for(uint k = 0; k < edge_partie.size(); k++) {
  1956. add_edge(tab_vertex_to.at(recherche_val(*Partition.at(i),
  1957. edge_partie.at(k).first)),
  1958. tab_vertex_to.at(recherche_val(*Partition.at(i),
  1959. edge_partie.at(k).second)),
  1960. graph);
  1961. }
  1962. graph_partie.push_back(graph);
  1963. }
  1964. for (uint i = 0; i < neigh_community.size(); i++) {
  1965. InputEdges inputedges;
  1966. for (uint j = 0; j < neigh_community.at(i)->size(); j++) {
  1967. for (uint k = 0;
  1968. k < outputedgelist.at(neigh_community.at(i)->at(j)).size();
  1969. k++) {
  1970. if (In_tab_dichotomie(
  1971. *Partition.at(i),
  1972. outputedgelist.at(
  1973. neigh_community.at(i)->at(j)).at(k).second))
  1974. inputedges.push_back(
  1975. outputedgelist.at(
  1976. neigh_community.at(i)->at(j)).at(k));
  1977. }
  1978. }
  1979. inputedgelist.push_back(inputedges);
  1980. }
  1981. for (uint i = 0; i < outputedgelist.size(); i++){
  1982. Connection connec;
  1983. for(uint j = 0; j < outputedgelist.at(i).size(); j++){
  1984. Port port1;
  1985. port1.first = i + 1;
  1986. port1.second = outputedgelist.at(i).at(j).first;
  1987. Port port2;
  1988. port2.first = In_community_dichotomie(
  1989. Partition,outputedgelist.at(i).at(j).second) + 1;
  1990. port2.second = outputedgelist.at(i).at(j).second;
  1991. connec.first = port1;
  1992. connec.second = port2;
  1993. connections.push_back(connec);
  1994. }
  1995. }
  1996. for (EntiersEntiers::iterator it = neigh_community.begin();
  1997. it != neigh_community.end(); it++) {
  1998. delete *it;
  1999. *it = NULL;
  2000. }
  2001. return graph_partie;
  2002. }
  2003. double Best_Cut_cluster(EntiersEntiers &tab_cluster,Entiers *cluster1, Entiers *cluster2, int index_cluster1, UnorientedGraph &g,std::string name)
  2004. {
  2005. tab_cluster.push_back(cluster2);
  2006. double cpt=0.;
  2007. for(int i=0;i<tab_cluster.size();i++){
  2008. if(i!=index_cluster1){
  2009. cpt+=Cut_one_cluster(*tab_cluster[i],g,name);
  2010. }
  2011. }
  2012. cpt+=Cut_one_cluster(*cluster1,g,name);
  2013. tab_cluster.pop_back();
  2014. return cpt;
  2015. }
  2016. double In_modularity(UnorientedGraph *g , const Entiers &cluster){
  2017. //property_map<UnorientedGraph,edge_weight_t>::type poids_arc=get(edge_weight_t(),g);
  2018. edge_t e1;
  2019. bool found;
  2020. int val=0;
  2021. for(uint i=0;i<cluster.size();i++){
  2022. tie(neighbourIt, neighbourEnd) = adjacent_vertices(cluster.at(i),*g);
  2023. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2024. tie(e1,found)=edge(vertex(cluster[i],*g),vertex(*neighbourIt,*g),*g);
  2025. if(In_tab(cluster,*neighbourIt)==1)
  2026. val+=(*g)[e1]._weight;
  2027. //val+=get(poids_arc,e1);
  2028. }
  2029. }
  2030. return val/2.;
  2031. }
  2032. /**
  2033. *
  2034. * @param g
  2035. * @param cluster
  2036. * @return
  2037. */
  2038. /**
  2039. *
  2040. * @param g
  2041. * @param part
  2042. * @return
  2043. */
  2044. double Modularity(UnorientedGraph *g,const EntiersEntiers &part){
  2045. double q = 0.;
  2046. int tmp=num_edges(*g);
  2047. for(uint i=0;i<part.size();i++){
  2048. q+=In_modularity(g,*part.at(i))/tmp-(Cluster_Degree(*g,*part.at(i))/(2*tmp))*(Cluster_Degree(*g,*part.at(i))/(2*tmp));
  2049. }
  2050. return q;
  2051. }
  2052. /**
  2053. *
  2054. * @param part
  2055. * @param val
  2056. * @return
  2057. */
  2058. /**
  2059. * Fonction de calcul du gain de modularité de déplacement d'un sommet d'une communoté à une autre !!!!!
  2060. * ajoute le sommet à part[val] et on calcul la nouvelle modularité
  2061. * on prend la différence entre la modularité et la nouvouvelle !
  2062. * @param cur_mod
  2063. * @param val
  2064. * @param neight
  2065. * @param node_comm
  2066. * @param part
  2067. * @param g
  2068. */
  2069. /*double Modularity_gain(double cur_mod , int val , int neight , int node_comm , EntiersEntiers part , UnorientedGraph &g) {
  2070. double q;
  2071. part[neight]->push_back(val);
  2072. std::sort(*part[neight]);
  2073. q=Modularity(g,part);
  2074. return q-cur_mod;
  2075. }*/
  2076. /**
  2077. * Fonction de calcul du gain de modularité de déplacement d'un sommet d'une communoté à une autre !!!!!
  2078. * ajoute le sommet à part[val] et on calcul la nouvelle modularité
  2079. * on prend la différence entre la modularité et la nouvouvelle !
  2080. * @param cur_mod
  2081. * @param tmp_community
  2082. * @param neight
  2083. * @param node_comm
  2084. * @param part
  2085. * @param g
  2086. */
  2087. /*double Modularity_gain_phase_2(double cur_mod, Entiers tmp_community, int neight, int node_comm, EntiersEntiers part, UnorientedGraph &g) {
  2088. double q;
  2089. for (uint i=0;i<tmp_community.size();i++)
  2090. part[neight]->push_back(tmp_community[i]);
  2091. std::sort(*part[neight]);
  2092. q = Modularity(g,part);
  2093. return q - cur_mod;
  2094. }*/
  2095. /**
  2096. * Donne la liste des communautés voisines à celle contenant le sommet val.
  2097. * @param part
  2098. * @param val
  2099. * @param g
  2100. * @return
  2101. */
  2102. /*Entiers Neight_community(const EntiersEntiers &part, int val , UnorientedGraph &g){
  2103. Entiers Neight;
  2104. tie(neighbourIt, neighbourEnd) = adjacent_vertices(val, g);
  2105. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2106. int tmp=In_community(part,*neighbourIt);
  2107. if(In_tab(Neight,tmp)!=1 && In_tab(*part[In_community(part,val)],*neighbourIt)!=1)
  2108. Neight.push_back(tmp);
  2109. }
  2110. std::sort(Neight);
  2111. return Neight;
  2112. }*/
  2113. /**
  2114. *
  2115. * @param part
  2116. * @param community
  2117. * @param g
  2118. * @return
  2119. */
  2120. /*Entiers Part_Neight_community(const EntiersEntiers &part,int community, UnorientedGraph &g){
  2121. Entiers Neight;
  2122. for(uint i =0;i<part[community]->size();i++){
  2123. tie(neighbourIt, neighbourEnd) = adjacent_vertices(part[community]->at(i), g);
  2124. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2125. int tmp=In_community(part,*neighbourIt);
  2126. if(In_tab(Neight,tmp)!=1 && tmp!=community)
  2127. Neight.push_back(tmp);
  2128. }
  2129. }
  2130. std::sort(Neight);
  2131. return Neight;
  2132. }*/
  2133. void make_unoriented_graph(const OrientedGraph& og, UnorientedGraph& ug)
  2134. {
  2135. std::vector < vertex_t > ug_vertex_list;
  2136. std::vector < vertex_t > og_vertex_list;
  2137. for (uint i = 0; i < num_vertices(og); ++i) {
  2138. ug_vertex_list.push_back(add_vertex(ug));
  2139. }
  2140. OrientedGraph::vertex_iterator it_og, end_og;
  2141. UnorientedGraph::vertex_iterator it_ug, end_ug;
  2142. tie(it_og, end_og) = vertices(og);
  2143. tie(it_ug, end_ug) = vertices(ug);
  2144. for (; it_og != end_og; ++it_og, ++it_ug) {
  2145. ug[*it_ug] = og[*it_og];
  2146. og_vertex_list.push_back(*it_og);
  2147. }
  2148. OrientedGraph::edge_iterator ite_og, ende_og;
  2149. tie(ite_og, ende_og) = edges(og);
  2150. for (; ite_og != ende_og; ++ite_og) {
  2151. boost::add_edge(source(*ite_og, og), target(*ite_og, og),
  2152. og[*ite_og], ug);
  2153. }
  2154. // std::cout << "Oriented graph: " << std::endl;
  2155. // tie(it_og, end_og) = vertices(og);
  2156. // for (; it_og != end_og; ++it_og) {
  2157. // OrientedGraph::adjacency_iterator neighbour_it, neighbour_end;
  2158. // std::cout << og[*it_og]._index << " is connected with ";
  2159. // tie(neighbour_it, neighbour_end) = adjacent_vertices(*it_og, og);
  2160. // for (; neighbour_it != neighbour_end; ++neighbour_it)
  2161. // std::cout << og[*neighbour_it]._index << " ";
  2162. // std::cout << " and weight = " << og[*it_og]._weight << std::endl;
  2163. // }
  2164. // std::cout << std::endl;
  2165. // std::cout << "Unoriented graph: " << std::endl;
  2166. // tie(it_ug, end_ug) = vertices(ug);
  2167. // for (; it_ug != end_ug; ++it_ug) {
  2168. // UnorientedGraph::adjacency_iterator neighbour_it, neighbour_end;
  2169. // std::cout << ug[*it_ug]._index << " is connected with ";
  2170. // tie(neighbour_it, neighbour_end) = adjacent_vertices(*it_ug, ug);
  2171. // for (; neighbour_it != neighbour_end; ++neighbour_it)
  2172. // std::cout << ug[*neighbour_it]._index << " ";
  2173. // std::cout << " and weight = " << ug[*it_ug]._weight << std::endl;
  2174. // }
  2175. // std::cout << std::endl;
  2176. }
  2177. void adjacence_ggp(int vertex, Entiers &sommets_adj, UnorientedGraph *g)
  2178. {
  2179. tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex, *g);
  2180. for (; neighbourIt != neighbourEnd; ++neighbourIt)
  2181. {
  2182. sommets_adj.push_back(*neighbourIt);
  2183. }
  2184. }
  2185. double modif_Cout_coupe(const Entiers &P, int val, double cut, UnorientedGraph *g)
  2186. {
  2187. //std::cout<<"Cout de coupe initiale : "<<cut<<std::endl;
  2188. //std::cout<<"degré du sommet tiré : "<<Degree(*g,val)<<std::endl;
  2189. double cpt = 0.;
  2190. double new_cut;
  2191. bool found;
  2192. edge_t e1;
  2193. tie(neighbourIt, neighbourEnd) = adjacent_vertices(val, *g);
  2194. for (; neighbourIt != neighbourEnd; neighbourIt++){
  2195. if(In_tab(P,*neighbourIt)==1){
  2196. tie(e1,found)=edge(vertex(val,*g),vertex(*neighbourIt,*g),*g);
  2197. cpt += (*g)[e1]._weight;
  2198. }
  2199. }
  2200. new_cut = cut + (Degree(*g,val) - 2*cpt);
  2201. return new_cut;
  2202. }
  2203. int decimal(int valeur){
  2204. int res;
  2205. switch(valeur){
  2206. case 0 ... 9 : res = 0;
  2207. break;
  2208. case 10 ... 99 : res = 1;
  2209. break;
  2210. case 100 ... 999 : res = 2;
  2211. break;
  2212. case 1000 ... 9999 : res = 3;
  2213. break;
  2214. case 10000 ... 99999 : res = 4;
  2215. break;
  2216. case 100000 ... 999999 : res = 5;
  2217. break;
  2218. case 1000000 ... 9999999 : res = 6;
  2219. break;
  2220. case 10000000 ... 99999999 : res = 7;
  2221. break;
  2222. case 100000000 ... 999999999 : res = 8;
  2223. break;
  2224. default :
  2225. std::cout<<"L'interval n'est pas de taille suffisante"<<std::endl;
  2226. break;
  2227. }
  2228. return res;
  2229. }
  2230. void Graph_constructor_txt(const char* text, OrientedGraph * Og){
  2231. //Traitement initial
  2232. std::ifstream fichier (text, std::ios::in);
  2233. int lines = std::count(std::istreambuf_iterator<char>( fichier ),
  2234. std::istreambuf_iterator<char>(),'\n' );
  2235. //std::cout<<"Nombre de ligne : "<<lines<<std::endl;
  2236. fichier.seekg(0, std::ios::beg);
  2237. std::string caractere;
  2238. getline(fichier, caractere);
  2239. int caractere_size = caractere.size()+1;
  2240. fichier.seekg(0, std::ios::beg);
  2241. int nbr_vertices;
  2242. fichier >> nbr_vertices;
  2243. //std::cout << "Nombre de sommets : "<< nbr_vertices<< std::endl;
  2244. //Création des sommets
  2245. std::vector<vertex_to> vect_vertex;
  2246. for(int i =0; i<nbr_vertices; i++){
  2247. vertex_to v0 = boost::add_vertex(*Og);
  2248. vect_vertex.push_back(v0);
  2249. }
  2250. //Création des arcs
  2251. int deplacement_sup = 0;
  2252. for(int i = 0; i <(lines-nbr_vertices-1); i++){
  2253. fichier.seekg((8)*i+caractere_size+deplacement_sup, std::ios::beg);
  2254. int vertex_in, vertex_out;
  2255. double weight;
  2256. fichier >> vertex_in >> vertex_out >> weight ;
  2257. add_edge(vect_vertex.at(vertex_in), vect_vertex.at(vertex_out), EdgeProperties(weight), *Og);
  2258. //std::cout << vertex_in << " " << vertex_out << " " << weight << std::endl;
  2259. int tmp = decimal(vertex_in) + decimal(vertex_out) + decimal(floor(weight));
  2260. deplacement_sup += tmp;
  2261. }
  2262. //Pondération des sommets
  2263. int cpt =0;
  2264. for(int i = lines-nbr_vertices-1; i <lines-1; i++){
  2265. fichier.seekg((8)*(lines-nbr_vertices-1)+caractere_size+deplacement_sup, std::ios::beg);
  2266. double poids;
  2267. std::string txt;
  2268. fichier >> poids >> txt ;
  2269. DynamicsType type;
  2270. if(txt == "NORMAL_PIXEL"){
  2271. type = NORMAL_PIXEL;
  2272. }else{
  2273. type = TOP_PIXEL;
  2274. }
  2275. (*Og)[vect_vertex.at(cpt)] = VertexProperties(cpt, poids, type);
  2276. //std::cout << poids << std::endl;
  2277. int tmp = decimal(floor(poids)) + 17;
  2278. deplacement_sup += tmp;
  2279. cpt++;
  2280. }
  2281. fichier.close();
  2282. }
  2283. void Text_generator_graph(const char *texte, OrientedGraph *go){
  2284. bool found;
  2285. edge_to e1;
  2286. std::ofstream fichier (texte, std::ios::out);
  2287. fichier<<num_vertices(*go)<<std::endl;
  2288. tie(vertexIto, vertexEndo) = vertices(*go);
  2289. for (; vertexIto != vertexEndo; ++vertexIto) {
  2290. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  2291. *go);
  2292. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  2293. tie(e1,found)=edge(vertex(*vertexIto,*go),vertex(*neighbourIto,*go),*go);
  2294. if(((*go)[e1]._weight - floor((*go)[e1]._weight)) == 0 ){
  2295. fichier<<(*go)[*vertexIto]._index<<" "<<(*go)[*neighbourIto]._index<<" "<<(*go)[e1]._weight<<".0"<<std::endl;
  2296. }else{
  2297. fichier<<(*go)[*vertexIto]._index<<" "<<(*go)[*neighbourIto]._index<<" "<<(*go)[e1]._weight<<std::endl;
  2298. }
  2299. }
  2300. }
  2301. tie(vertexIto, vertexEndo) = vertices(*go);
  2302. for (; vertexIto != vertexEndo; ++vertexIto) {
  2303. if(((*go)[*vertexIto]._weight - floor((*go)[*vertexIto]._weight)) == 0 && (*go)[*vertexIto]._type == TOP_PIXEL){
  2304. fichier<<(*go)[*vertexIto]._weight<<".0"<<" "<<"TOP_PIXEL"<<" "<<std::endl;
  2305. }else if(((*go)[*vertexIto]._weight - floor((*go)[*vertexIto]._weight)) == 0 && (*go)[*vertexIto]._type == NORMAL_PIXEL){
  2306. fichier<<(*go)[*vertexIto]._weight<<".0"<<" "<<"NORMAL_PIXEL"<<std::endl;
  2307. }else if(((*go)[*vertexIto]._weight - floor((*go)[*vertexIto]._weight)) != 0 && (*go)[*vertexIto]._type == TOP_PIXEL){
  2308. fichier<<(*go)[*vertexIto]._weight<<" "<<"TOP_PIXEL"<<std::endl;
  2309. }else{
  2310. fichier<<(*go)[*vertexIto]._weight<<" "<<"NORMAL_PIXEL"<<std::endl;
  2311. }
  2312. }
  2313. fichier.close();
  2314. }
  2315. double Diff_cut_ratio(UnorientedGraph *g, const EntiersEntiers &Partition, int partie, int node, std::string name){
  2316. double Dif;
  2317. double Int = 0.;
  2318. double Ext = 0.;
  2319. edge_t e1;
  2320. bool found;
  2321. tie(neighbourIt, neighbourEnd) = adjacent_vertices(node, *g);
  2322. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2323. tie(e1, found) = edge(vertex(node, *g), vertex(*neighbourIt, *g), *g);
  2324. if(In_tab_dichotomie(*Partition.at(partie),*neighbourIt) == 1){
  2325. Int+= (*g)[e1]._weight;
  2326. }else{
  2327. Ext+= (*g)[e1]._weight;
  2328. }
  2329. }
  2330. if(name == "ratio"){
  2331. Int/=Cluster_Weight(*g,*Partition.at(partie));
  2332. Ext/=Cluster_Weight(*g,*Partition.at(partie));
  2333. }
  2334. Dif = Ext - Int;
  2335. return Dif;
  2336. }
  2337. double Diff_cut_ratio_bissection(UnorientedGraph *g, Entiers *part, int node, std::string name){
  2338. double Ext = 0.;
  2339. edge_t e1;
  2340. bool found;
  2341. tie(neighbourIt, neighbourEnd) = adjacent_vertices(node, *g);
  2342. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2343. tie(e1, found) = edge(vertex(node, *g), vertex(*neighbourIt, *g), *g);
  2344. if(In_tab_dichotomie(*part,*neighbourIt) != 1){
  2345. Ext+= (*g)[e1]._weight;
  2346. }
  2347. }
  2348. return Ext;
  2349. }
  2350. std::vector<std::vector<int>> Vector_diff_cut_ratio(UnorientedGraph *g, const EntiersEntiers &Partition, std::string name){
  2351. std::vector<std::vector<int>> Diff_vector;
  2352. for(uint i = 0; i < Partition.size(); i++){
  2353. std::vector<std::pair<double,int>> D_vector;
  2354. for(uint j = 0; j < Partition.at(i)->size(); j++){
  2355. double gain_d = Diff_cut_ratio(g, Partition, i, Partition.at(i)->at(j), name);
  2356. //std::cout<<gain_d<<std::endl;
  2357. if(gain_d > 0){
  2358. std::pair<double, int> D;
  2359. D.first = gain_d;
  2360. D.second = Partition.at(i)->at(j);
  2361. D_vector.push_back(D);
  2362. }
  2363. }
  2364. sort(D_vector.begin(),D_vector.end());
  2365. std::reverse(D_vector.begin(),D_vector.end());
  2366. std::vector<int> index_vector;
  2367. for(uint id = 0; id < D_vector.size(); id++){
  2368. index_vector.push_back(D_vector.at(id).second);
  2369. }
  2370. Diff_vector.push_back(index_vector);
  2371. }
  2372. /*std::cout<<"Tableau des différences "<<std::endl;
  2373. for(uint i = 0; i<Diff_vector.size(); i++){
  2374. std::cout<<"*"<<i<<"* ";
  2375. for(uint j = 0; j<Diff_vector.at(i).size(); j++){
  2376. std::cout<<Diff_vector.at(i).at(j)<<" ";
  2377. }
  2378. std::cout<<std::endl;
  2379. }*/
  2380. return Diff_vector;
  2381. }
  2382. std::vector<int> Vector_diff_cut_ratio_2(UnorientedGraph *g, const EntiersEntiers &Partition, std::string name){
  2383. std::vector<int> Diff_vector;
  2384. std::vector<std::pair<double,int>> D_vector;
  2385. for(uint i = 0; i < Partition.size(); i++){
  2386. for(uint j = 0; j < Partition.at(i)->size(); j++){
  2387. double gain_d = Diff_cut_ratio(g, Partition, i, Partition.at(i)->at(j), name);
  2388. //std::cout<<gain_d<<std::endl;
  2389. if(gain_d > 0){
  2390. std::pair<double, int> D;
  2391. D.first = gain_d;
  2392. D.second = Partition.at(i)->at(j);
  2393. D_vector.push_back(D);
  2394. }
  2395. }
  2396. }
  2397. sort(D_vector.begin(),D_vector.end());
  2398. for(uint id = 0; id < D_vector.size(); id++){
  2399. Diff_vector.push_back(D_vector.at(id).second);
  2400. }
  2401. /*std::cout<<"Tableau des différences "<<std::endl;
  2402. for(uint i = 0; i<Diff_vector.size(); i++){
  2403. std::cout<<"*"<<i<<"* ";
  2404. for(uint j = 0; j<Diff_vector.at(i).size(); j++){
  2405. std::cout<<Diff_vector.at(i).at(j)<<" ";
  2406. }
  2407. std::cout<<std::endl;
  2408. }*/
  2409. /*for(uint j = 0; j<Diff_vector.size(); j++){
  2410. std::cout<<Diff_vector.at(j)<<" ";
  2411. }
  2412. std::cout<<std::endl;*/
  2413. return Diff_vector;
  2414. }
  2415. void Modif_vector_diff_cut_ratio_2(UnorientedGraph *g, const EntiersEntiers &Partition, std::vector<int> &Diff_vector, int node, std::string name){
  2416. std::vector<std::pair<double,int>> D_vector;
  2417. tie(neighbourIt, neighbourEnd) = adjacent_vertices(node, *g);
  2418. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2419. //std::cout<<"node : "<<node<<std::endl;
  2420. //std::cout<<"voisin : "<<*neighbourIt<<std::endl;
  2421. int neigh_ind = In_community_dichotomie(Partition, *neighbourIt);
  2422. //std::cout<<"dans : "<<neigh_ind<<std::endl;
  2423. double gain_d = Diff_cut_ratio(g, Partition, neigh_ind, *neighbourIt, name);
  2424. //std::cout<<"gain_d : "<<gain_d<<std::endl;
  2425. if(gain_d > 0){
  2426. std::pair<double, int> D;
  2427. D.first = gain_d;
  2428. D.second = *neighbourIt;
  2429. D_vector.push_back(D);
  2430. }
  2431. //suprim_val(Diff_vector,*neighbourIt);
  2432. }
  2433. if(D_vector.size() == 0){
  2434. Diff_vector.erase(Diff_vector.begin());
  2435. return;
  2436. }
  2437. //std::cout<<"**"<<std::endl;
  2438. sort(D_vector.begin(),D_vector.end());
  2439. for(uint id = 0; id < D_vector.size(); id++){
  2440. if(In_tab(Diff_vector,D_vector.at(id).second) != 1)
  2441. Diff_vector.push_back(D_vector.at(id).second);
  2442. }
  2443. //std::cout<<"***"<<std::endl;
  2444. Diff_vector.erase(Diff_vector.begin());
  2445. //std::cout<<"**!**"<<std::endl;
  2446. sort(Diff_vector.begin(),Diff_vector.end());
  2447. for(uint j = 0; j<Diff_vector.size(); j++){
  2448. std::cout<<Diff_vector.at(j)<<" ";
  2449. }
  2450. //std::cout<<std::endl;
  2451. }
  2452. void Modif_vector_diff_cut_ratio(UnorientedGraph *g, const EntiersEntiers &Partition, std::vector<std::vector<int>> &Diff_vector, int recalcul1, int recalcul2, std::string name){
  2453. std::vector<std::pair<double,int>> D_vector;
  2454. for(uint j = 0; j < Partition.at(recalcul1)->size(); j++){
  2455. double gain_d = Diff_cut_ratio(g, Partition, recalcul1, Partition.at(recalcul1)->at(j), name);
  2456. //std::cout<<gain_d<<std::endl;
  2457. if(gain_d > 0){
  2458. std::pair<double, int> D;
  2459. D.first = gain_d;
  2460. D.second = Partition.at(recalcul1)->at(j);
  2461. D_vector.push_back(D);
  2462. }
  2463. }
  2464. sort(D_vector.begin(),D_vector.end());
  2465. std::reverse(D_vector.begin(),D_vector.end());
  2466. std::vector<int> index_vector;
  2467. for(uint id = 0; id < D_vector.size(); id++){
  2468. index_vector.push_back(D_vector.at(id).second);
  2469. }
  2470. Diff_vector.at(recalcul1) = index_vector;
  2471. std::vector<std::pair<double,int>> D_vector2;
  2472. for(uint j = 0; j < Partition.at(recalcul2)->size(); j++){
  2473. double gain_d = Diff_cut_ratio(g, Partition, recalcul2, Partition.at(recalcul2)->at(j), name);
  2474. //std::cout<<gain_d<<std::endl;
  2475. if(gain_d > 0){
  2476. std::pair<double, int> D;
  2477. D.first = gain_d;
  2478. D.second = Partition.at(recalcul2)->at(j);
  2479. D_vector2.push_back(D);
  2480. }
  2481. }
  2482. sort(D_vector2.begin(),D_vector2.end());
  2483. std::reverse(D_vector2.begin(),D_vector2.end());
  2484. std::vector<int> index_vector2;
  2485. for(uint id = 0; id < D_vector2.size(); id++){
  2486. index_vector2.push_back(D_vector2.at(id).second);
  2487. }
  2488. Diff_vector.at(recalcul2) = index_vector2;
  2489. /*std::cout<<"Tableau des différences modifié "<<std::endl;
  2490. for(uint i = 0; i<Diff_vector.size(); i++){
  2491. std::cout<<"*"<<i<<"* ";
  2492. for(uint j = 0; j<Diff_vector.at(i).size(); j++){
  2493. std::cout<<Diff_vector.at(i).at(j)<<" ";
  2494. }
  2495. std::cout<<std::endl;
  2496. }*/
  2497. }
  2498. void Affinache_gain_diff(UnorientedGraph *g, EntiersEntiers &Partition, double &cut, std::string name, double poids_moy){
  2499. double old_cut = -1.;
  2500. while(old_cut != cut){
  2501. //std::cout<<"Boucle d'ammélioration "<<std::endl;
  2502. old_cut = cut;
  2503. sort(Partition.begin(), Partition.end(), myobject_taille);
  2504. /*for(uint i=0;i<Partition.size();i++){
  2505. std::cout<<Partition.at(i)->size()<<std::endl;
  2506. }*/
  2507. std::vector<std::vector<int>> diff_vector;
  2508. diff_vector = Vector_diff_cut_ratio(g, Partition, name);
  2509. /*for(uint i = 0; i<diff_vector.size(); i++){
  2510. std::cout<<diff_vector.at(i)<<std::endl;
  2511. }*/
  2512. for(uint indice = 0; indice < diff_vector.size(); indice ++){
  2513. if(diff_vector.at(indice).size() != 0 && Partition.at(indice)->size() >1){
  2514. //for(uint i = 0; i < diff_vector.at(indice).size(); i++){
  2515. int i =0;
  2516. while(diff_vector.at(indice).size() != 0 && Partition.at(indice)->size() >1 && i < diff_vector.at(indice).size() &&
  2517. Cluster_Weight(*g,*Partition.at(indice)) > (poids_moy-poids_moy/Partition.size())){
  2518. //std::cout<<"Sommet de départ : "<< (*g)[diff_vector.at(indice).at(i)]._index <<" dans "<<indice<<std::endl;
  2519. Entiers neigh_part;
  2520. neigh_part = Neigh_community(g, Partition, diff_vector.at(indice).at(i), indice);
  2521. int best_neigh_part = neigh_part.at(0);
  2522. double gain = -10000000.;
  2523. for(uint ind_neigh = 0; ind_neigh < neigh_part.size(); ind_neigh++){
  2524. double tmp_gain;
  2525. if(name == "ratio"){
  2526. tmp_gain = Gain_ratio(g,Partition,indice,neigh_part.at(ind_neigh),diff_vector.at(indice).at(i),cut);
  2527. }else{
  2528. double Int = 0.;
  2529. double Ext = 0.;
  2530. edge_t e1;
  2531. bool found;
  2532. tie(neighbourIt, neighbourEnd) = adjacent_vertices(diff_vector.at(indice).at(i), *g);
  2533. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2534. tie(e1, found) = edge(vertex(diff_vector.at(indice).at(i), *g), vertex(*neighbourIt, *g), *g);
  2535. if(In_tab_dichotomie(*Partition.at(neigh_part.at(ind_neigh)),*neighbourIt) == 1){
  2536. Ext+= (*g)[e1]._weight;
  2537. }else if(In_tab_dichotomie(*Partition.at(indice),*neighbourIt) == 1){
  2538. Int+= (*g)[e1]._weight;
  2539. }
  2540. }
  2541. tmp_gain = Ext - Int;
  2542. }
  2543. if(tmp_gain > gain & tmp_gain > 0){
  2544. gain = tmp_gain;
  2545. best_neigh_part = neigh_part.at(ind_neigh);
  2546. }
  2547. }
  2548. //std::cout<<" Ensemble de déstination "<<best_neigh_part<<" gain de : "<<gain<<std::endl;
  2549. if(gain > 0){
  2550. //std::cout<<"Modification"<<std::endl;
  2551. cut -= gain; /*Grosse modification a apporté de ce coté la*/
  2552. //std::cout<<"Ratio de coupe : "<<cut<<std::endl;
  2553. suprim_val(*Partition.at(indice),diff_vector.at(indice).at(i));
  2554. Partition.at(best_neigh_part)->push_back(diff_vector.at(indice).at(i));
  2555. sort(Partition.at(best_neigh_part)->begin(),Partition.at(best_neigh_part)->end());
  2556. //double cut2 = Cut_cluster(Partition,*g,"ratio");
  2557. //std::cout<<"Vrai ratio de coupe : "<<cut2<<std::endl;
  2558. Modif_vector_diff_cut_ratio(g,Partition,diff_vector,best_neigh_part,indice,name);
  2559. i = 0;
  2560. }else{
  2561. i++;
  2562. }
  2563. }
  2564. }
  2565. }
  2566. //std::cout<<cut<<std::endl;
  2567. }
  2568. }
  2569. void Affinache_gain_diff_2(UnorientedGraph *g, EntiersEntiers &Partition, double &cut, std::string name, double poids_moy){
  2570. double old_cut = -1.;
  2571. //while(old_cut != cut){
  2572. //std::cout<<"Boucle d'ammélioration "<<std::endl;
  2573. //old_cut = cut;
  2574. sort(Partition.begin(), Partition.end(), myobject_taille);
  2575. std::vector<int> diff_vector;
  2576. diff_vector = Vector_diff_cut_ratio_2(g, Partition, name);
  2577. //for(uint indice = 0; indice < diff_vector.size(); indice ++){
  2578. int indice = 0;
  2579. while(/*indice < diff_vector.size() &&*/ diff_vector.size() != 0){
  2580. int com = In_community_dichotomie(Partition,diff_vector.at(indice));
  2581. std::cout<<" Ensemble de départ "<<com<<" sommet : "<<diff_vector.at(indice)<<std::endl;
  2582. if(Partition.at(com)->size() >1 && Cluster_Weight(*g,*Partition.at(com)) > (poids_moy-poids_moy/Partition.size())){
  2583. Entiers neigh_part;
  2584. neigh_part = Neigh_community(g, Partition, diff_vector.at(indice), com);
  2585. int best_neigh_part = neigh_part.at(0);
  2586. double gain = -10000000.;
  2587. for(uint ind_neigh = 0; ind_neigh < neigh_part.size(); ind_neigh++){
  2588. double tmp_gain;
  2589. if(name == "ratio"){
  2590. tmp_gain = Gain_ratio(g,Partition,com,neigh_part.at(ind_neigh),diff_vector.at(indice),cut);
  2591. }else{
  2592. double Int = 0.;
  2593. double Ext = 0.;
  2594. edge_t e1;
  2595. bool found;
  2596. tie(neighbourIt, neighbourEnd) = adjacent_vertices(diff_vector.at(indice), *g);
  2597. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2598. tie(e1, found) = edge(vertex(diff_vector.at(indice), *g), vertex(*neighbourIt, *g), *g);
  2599. if(In_tab_dichotomie(*Partition.at(neigh_part.at(ind_neigh)),*neighbourIt) == 1){
  2600. Ext+= (*g)[e1]._weight;
  2601. }else if(In_tab_dichotomie(*Partition.at(com),*neighbourIt) == 1){
  2602. Int+= (*g)[e1]._weight;
  2603. }
  2604. }
  2605. tmp_gain = Ext - Int;
  2606. }
  2607. if(tmp_gain > gain & tmp_gain > 0){
  2608. gain = tmp_gain;
  2609. best_neigh_part = neigh_part.at(ind_neigh);
  2610. }
  2611. }
  2612. std::cout<<" Ensemble de déstination "<<best_neigh_part<<" gain de : "<<gain<<std::endl;
  2613. if(gain > 0){
  2614. std::cout<<"Modification"<<std::endl;
  2615. cut -= gain; /*Grosse modification a apporté de ce coté la*/
  2616. std::cout<<"Ratio de coupe : "<<cut<<std::endl;
  2617. suprim_val(*Partition.at(com),diff_vector.at(indice));
  2618. Partition.at(best_neigh_part)->push_back(diff_vector.at(indice));
  2619. sort(Partition.at(best_neigh_part)->begin(),Partition.at(best_neigh_part)->end());
  2620. double cut2 = Cut_cluster(Partition,*g,"ratio");
  2621. std::cout<<"Vrai ratio de coupe : "<<cut2<<std::endl;
  2622. //Modif_vector_diff_cut_ratio_2(g,Partition,diff_vector,diff_vector.at(indice),name);
  2623. //indice = 0;
  2624. diff_vector.erase(diff_vector.begin());
  2625. }else{
  2626. diff_vector.erase(diff_vector.begin());
  2627. }
  2628. }
  2629. }
  2630. //std::cout<<cut<<std::endl;
  2631. // }
  2632. }
  2633. double Gain_ratio(UnorientedGraph *g,const EntiersEntiers &Partition, int in, int out, int node, double ratio){
  2634. double new_ratio = ratio;
  2635. double poids_in = Cluster_Weight(*g,*Partition.at(in));
  2636. double poids_out = Cluster_Weight(*g,*Partition.at(out));
  2637. double tmp_poids_in = poids_in - (*g)[node]._weight;
  2638. double tmp_poids_out = poids_out + (*g)[node]._weight;
  2639. //std::cout<<"tmp_poids_in "<< tmp_poids_in <<std::endl;
  2640. //std::cout<<"tmp_poids_out "<< tmp_poids_out <<std::endl;
  2641. double cut_in = 0.;
  2642. double cut_out = 0.;
  2643. double tmp_cut_in = 0.;
  2644. double tmp_cut_out = 0.;
  2645. edge_t e1;
  2646. bool found;
  2647. for(uint i = 0; i < Partition.at(in)->size(); i++){
  2648. tie(neighbourIt, neighbourEnd) = adjacent_vertices(Partition.at(in)->at(i), *g);
  2649. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2650. tie(e1,found)=edge(vertex(Partition.at(in)->at(i),*g),vertex(*neighbourIt,*g),*g);
  2651. if(In_tab_dichotomie(*Partition.at(in),*neighbourIt) != 1){
  2652. if(Partition.at(in)->at(i) != node){
  2653. tmp_cut_in += (*g)[e1]._weight;
  2654. }
  2655. cut_in += (*g)[e1]._weight;
  2656. }else if(*neighbourIt == node){
  2657. tmp_cut_in += (*g)[e1]._weight;
  2658. }
  2659. }
  2660. }
  2661. for(uint i = 0; i < Partition.at(out)->size(); i++){
  2662. tie(neighbourIt, neighbourEnd) = adjacent_vertices(Partition.at(out)->at(i), *g);
  2663. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2664. tie(e1,found)=edge(vertex(Partition.at(out)->at(i),*g),vertex(*neighbourIt,*g),*g);
  2665. if(In_tab_dichotomie(*Partition.at(out),*neighbourIt) != 1){
  2666. if(*neighbourIt != node){
  2667. tmp_cut_out += (*g)[e1]._weight;
  2668. }
  2669. cut_out += (*g)[e1]._weight;
  2670. }
  2671. }
  2672. }
  2673. tie(neighbourIt, neighbourEnd) = adjacent_vertices(node, *g);
  2674. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2675. tie(e1,found)=edge(vertex(node,*g),vertex(*neighbourIt,*g),*g);
  2676. if(In_tab_dichotomie(*Partition.at(out),*neighbourIt) != 1){
  2677. tmp_cut_out += (*g)[e1]._weight;
  2678. }
  2679. }
  2680. //std::cout<<"tmp_cut_in "<< tmp_cut_in/2. <<std::endl;
  2681. //std::cout<<"tmp_cut_out "<< tmp_cut_out/2. <<std::endl;
  2682. new_ratio -= cut_in/2./poids_in;
  2683. new_ratio -= cut_out/2./poids_out;
  2684. new_ratio += tmp_cut_in/2./tmp_poids_in;
  2685. new_ratio += tmp_cut_out/2./tmp_poids_out;
  2686. //std::cout<<"Nouveau ratio : " <<new_ratio<<std::endl;
  2687. return ratio - new_ratio;
  2688. }
  2689. double Modif_ratio_cut(UnorientedGraph *g,Entiers *Ss, Entiers *Sd, int node, double ratio){/*Revoir cette fonction, modification psa forcement nécéssaire, plus simple !!!*/
  2690. double new_ratio;
  2691. double poids_in = Cluster_Weight(*g,*Ss);
  2692. double poids_out = Cluster_Weight(*g,*Sd);
  2693. double tmp_poids_in = poids_in - (*g)[node]._weight;
  2694. double tmp_poids_out = poids_out + (*g)[node]._weight;
  2695. //std::cout<<"tmp_poids_in "<< tmp_poids_in <<std::endl;
  2696. //std::cout<<"tmp_poids_out "<< tmp_poids_out <<std::endl;
  2697. double new_cut = 0.;
  2698. //double new_cut_out = 0.;
  2699. //double tmp_cut_in = 0.;
  2700. //double tmp_cut_out = 0.;
  2701. edge_t e1;
  2702. bool found;
  2703. for(uint i = 0; i < Ss->size(); i++){
  2704. if(Ss->at(i) != node){
  2705. tie(neighbourIt, neighbourEnd) = adjacent_vertices(Ss->at(i), *g);
  2706. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2707. tie(e1,found)=edge(vertex(Ss->at(i),*g),vertex(*neighbourIt,*g),*g);
  2708. if(In_tab_dichotomie(*Ss,*neighbourIt) != 1){
  2709. new_cut += (*g)[e1]._weight;
  2710. }else if(*neighbourIt == node){
  2711. new_cut += (*g)[e1]._weight;
  2712. }
  2713. }
  2714. }
  2715. }
  2716. /*for(uint i = 0; i < Sd->size(); i++){
  2717. tie(neighbourIt, neighbourEnd) = adjacent_vertices(Sd->at(i), *g);
  2718. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2719. tie(e1,found)=edge(vertex(Sd->at(i),*g),vertex(*neighbourIt,*g),*g);
  2720. if(In_tab(*Sd,*neighbourIt) != 1){
  2721. if(*neighbourIt != node){
  2722. tmp_cut_out += (*g)[e1]._weight;
  2723. }
  2724. cut_out += (*g)[e1]._weight;
  2725. }
  2726. }
  2727. }
  2728. tie(neighbourIt, neighbourEnd) = adjacent_vertices(node, *g);
  2729. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2730. tie(e1,found)=edge(vertex(node,*g),vertex(*neighbourIt,*g),*g);
  2731. if(In_tab(*Sd,*neighbourIt) != 1){
  2732. tmp_cut_out += (*g)[e1]._weight;
  2733. }
  2734. }*/
  2735. //std::cout<<"tmp_cut_in "<< tmp_cut_in/2. <<std::endl;
  2736. //std::cout<<"tmp_cut_out "<< tmp_cut_out/2. <<std::endl;
  2737. new_ratio = new_cut/2./tmp_poids_in + new_cut/2./tmp_poids_out;
  2738. /*new_ratio -= cut_out/2./poids_out;
  2739. new_ratio += tmp_cut_in/2./tmp_poids_in;
  2740. new_ratio += tmp_cut_out/2./tmp_poids_out;*/
  2741. //std::cout<<"Nouveau ratio : " <<new_ratio<<std::endl;
  2742. return new_ratio;
  2743. }
  2744. EntiersEntiers Spectral_Partition(const char* text){
  2745. //Traitement initial
  2746. EntiersEntiers Partition;
  2747. std::ifstream fichier (text, std::ios::in);
  2748. if(fichier){
  2749. int lines = std::count(std::istreambuf_iterator<char>( fichier ),
  2750. std::istreambuf_iterator<char>(),'\n' );
  2751. std::cout<<"Nombre de ligne : "<<lines<<std::endl;
  2752. /*** Récupération du dernier caractère ***/
  2753. /*** Création des paramétres contenant les informations ***/
  2754. int nmax_vertex;
  2755. fichier.seekg(0, std::ios::beg);
  2756. fichier >> nmax_vertex;
  2757. std::cout<<"nmax_vertex : "<<nmax_vertex<<std::endl;
  2758. int nmax_size = decimal(nmax_vertex) + 1;
  2759. /*** Récupération des informations ***/
  2760. int cpt = 1;
  2761. int length;
  2762. fichier.seekg(nmax_size, std::ios::beg);
  2763. while(cpt < lines){
  2764. Entiers *part = new Entiers();
  2765. for(uint i =0; i<nmax_vertex; i++){
  2766. int edge;
  2767. fichier >> edge;
  2768. if(edge != -1)
  2769. part->push_back(edge);
  2770. }
  2771. Partition.push_back(part);
  2772. length = fichier.tellg();
  2773. fichier.seekg(length+1, std::ios::beg);
  2774. cpt++;
  2775. }
  2776. }else{
  2777. std::cerr << "Impossible d'ouvrir le fichier dans Spectral_Partition !" << std::endl;
  2778. }
  2779. return(Partition);
  2780. }
  2781. void Adjacent_Matrix_Txt(UnorientedGraph *g, const char* text){
  2782. std::ofstream GRAPH4 (text, std::ios::out);
  2783. if(GRAPH4){
  2784. tie(vertexIt, vertexEnd) = vertices(*g);
  2785. edge_t e1;
  2786. bool found;
  2787. for (; vertexIt != vertexEnd; ++vertexIt) {
  2788. int cpt = 0;
  2789. tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,
  2790. *g);
  2791. for(int i = cpt; i<num_vertices(*g); i++){
  2792. if(i == *neighbourIt){
  2793. tie(e1,found)=edge(vertex(*vertexIt,*g),vertex(*neighbourIt,*g),*g);
  2794. GRAPH4<<(*g)[e1]._weight<<" ";
  2795. cpt = *neighbourIt +1;
  2796. ++neighbourIt;
  2797. if(*neighbourIt == *neighbourEnd){
  2798. for(int j = cpt; j<num_vertices(*g); j++)
  2799. GRAPH4<<0<<" ";
  2800. break;
  2801. }
  2802. }else{
  2803. GRAPH4<<0<<" ";
  2804. }
  2805. }
  2806. GRAPH4<<std::endl;
  2807. }
  2808. GRAPH4.close();
  2809. }else
  2810. std::cerr << "Impossible d'ouvrir le fichier dans Adjacent_Matrix_Txt !" << std::endl;
  2811. }
  2812. void Weight_Matrix_Txt(UnorientedGraph *g, const char* text){
  2813. std::ofstream GRAPH4 (text, std::ios::out);
  2814. if(GRAPH4){
  2815. for (int i =0 ; i<num_vertices(*g); i++) {
  2816. GRAPH4<<(*g)[i]._weight<<" ";
  2817. }
  2818. GRAPH4.close();
  2819. }else
  2820. std::cerr << "Impossible d'ouvrir le fichier dans Weight_Matrix_Txt !" << std::endl;
  2821. }
  2822. void Plot_OrientedGraph(OrientedGraph *go, const char* text){
  2823. edge_to e1;
  2824. bool found;
  2825. std::ofstream fichier2 (text, std::ios::out);
  2826. fichier2<<"digraph G {"<<std::endl;
  2827. tie(vertexIto, vertexEndo) = vertices(*go);
  2828. for (; vertexIto != vertexEndo; ++vertexIto) {
  2829. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  2830. *go);
  2831. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  2832. tie(e1,found)=edge(vertex(*vertexIto,*go),
  2833. vertex(*neighbourIto,*go),*go);
  2834. fichier2<<(*go)[*vertexIto]._index<<" -> "
  2835. <<(*go)[*neighbourIto]._index<<" [label="
  2836. <<(*go)[e1]._weight
  2837. <<", fontsize=10, fontcolor= blue];"<<std::endl;
  2838. }
  2839. }
  2840. fichier2<<"}";
  2841. fichier2.close();
  2842. }
  2843. void Plot_UnorientedGraph(UnorientedGraph *g, const char* text){
  2844. edge_t e1;
  2845. bool found;
  2846. std::ofstream GRAPH2 (text, std::ios::out);
  2847. GRAPH2<<"graph G {"<<std::endl;
  2848. tie(vertexIt, vertexEnd) = vertices(*g);
  2849. for (; vertexIt != vertexEnd; ++vertexIt) {
  2850. tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,*g);
  2851. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2852. if((*g)[*neighbourIt]._index>(*g)[*vertexIt]._index){
  2853. tie(e1,found)=edge(vertex(*vertexIt,*g),vertex(*neighbourIt,*g),*g);
  2854. GRAPH2<<(*g)[*vertexIt]._index<<" -- "<<(*g)[*neighbourIt]._index<<" [label="<<(*g)[e1]._weight<<", fontsize=10, fontcolor= blue];"<<std::endl;
  2855. }
  2856. }
  2857. }
  2858. GRAPH2<<"}";
  2859. GRAPH2.close();
  2860. }
  2861. void Plot_UnorientedGraph_All(UnorientedGraph *g, const EntiersEntiers &Partition, const char* text, bool Color){
  2862. edge_t e1;
  2863. bool found;
  2864. if(Partition.size()<16){
  2865. std::ofstream GRAPH2 (text, std::ios::out);
  2866. GRAPH2<<"graph G {"<<std::endl;
  2867. tie(vertexIt, vertexEnd) = vertices(*g);
  2868. for (; vertexIt != vertexEnd; ++vertexIt) {
  2869. tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,*g);
  2870. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2871. if((*g)[*neighbourIt]._index>(*g)[*vertexIt]._index){
  2872. tie(e1,found)=edge(vertex(*vertexIt,*g),vertex(*neighbourIt,*g),*g);
  2873. GRAPH2<<(*g)[*vertexIt]._index<<" -- "<<(*g)[*neighbourIt]._index<<" [label="<<(*g)[e1]._weight<<", fontsize=10, fontcolor= blue];"<<std::endl;}
  2874. }
  2875. }
  2876. if(Color == true){
  2877. std::vector<std::string> color;
  2878. color.push_back(", color=blue2, fontcolor=blue2];");
  2879. color.push_back(", color=red, fontcolor=red];");
  2880. color.push_back(", color=green, fontcolor=green];");
  2881. color.push_back(", color=turquoise, fontcolor=turquoise];");
  2882. color.push_back(", color=saddlebrown, fontcolor=saddlebrown];");
  2883. color.push_back(", color=indigo, fontcolor=indigo];");
  2884. color.push_back(", color=yellow, fontcolor=yellow2];");
  2885. color.push_back(", color=orange, fontcolor=orange];");
  2886. color.push_back(", color=olivedrab, fontcolor=olivedrab];");
  2887. color.push_back(", color=gold, fontcolor=gold];");
  2888. color.push_back(", color=slateblue2, fontcolor=slateblue2];");
  2889. color.push_back(", color=dimgrey, fontcolor=dimgrey];");
  2890. color.push_back(", color=cyan, fontcolor=cyan];");
  2891. color.push_back(", color=purple1, fontcolor=purpule1];");
  2892. color.push_back(", color=crimson, fontcolor=crimson];");
  2893. color.push_back(", color=black, fontcolor=black];");
  2894. for(uint k=0; k<Partition.size(); k++){
  2895. for(uint t=0; t<Partition.at(k)->size(); t++)
  2896. {
  2897. GRAPH2<<(*g)[Partition.at(k)->at(t)]._index<<" [label="<<(*g)[Partition.at(k)->at(t)]._weight<<color.at(k)<<std::endl;
  2898. }
  2899. }
  2900. }else{
  2901. for(uint k=0; k<num_vertices(*g); k++){
  2902. GRAPH2<<(*g)[k]._index<<" [label="<<(*g)[k]._index<<", weight="<<(*g)[k]._weight<<"];"<<std::endl;
  2903. }
  2904. }
  2905. GRAPH2<<"}";
  2906. GRAPH2.close();
  2907. }else{
  2908. std::cout<<"Error : Le nombre de couleur est insuffisant pour réaliser l'affichange"<<std::endl;
  2909. }
  2910. }
  2911. void Plot_OrientedGraph_All(OrientedGraph *go, const EntiersEntiers &Partition, const char* text, bool Color){
  2912. edge_to e1;
  2913. bool found;
  2914. if(Partition.size()<16){
  2915. std::vector<std::string> color;
  2916. color.push_back("[color=blue2, fontcolor=blue2];");
  2917. color.push_back("[color=red, fontcolor=red];");
  2918. color.push_back("[color=green, fontcolor=green];");
  2919. color.push_back("[color=turquoise, fontcolor=turquoise];");
  2920. color.push_back("[color=saddlebrown, fontcolor=saddlebrown];");
  2921. color.push_back("[color=indigo, fontcolor=indigo];");
  2922. color.push_back("[color=yellow, fontcolor=yellow2];");
  2923. color.push_back("[color=orange, fontcolor=orange];");
  2924. color.push_back("[color=olivedrab, fontcolor=olivedrab];");
  2925. color.push_back("[color=gold, fontcolor=gold];");
  2926. color.push_back("[color=slateblue2, fontcolor=slateblue2];");
  2927. color.push_back("[color=dimgrey, fontcolor=dimgrey];");
  2928. color.push_back("[color=cyan, fontcolor=cyan];");
  2929. color.push_back("[color=purple1, fontcolor=purpule1];");
  2930. color.push_back("[color=crimson, fontcolor=crimson];");
  2931. color.push_back("[color=black, fontcolor=black];");
  2932. std::ofstream fichier2 (text, std::ios::out);
  2933. fichier2<<"digraph G {"<<std::endl;
  2934. tie(vertexIto, vertexEndo) = vertices(*go);
  2935. for (; vertexIto != vertexEndo; ++vertexIto) {
  2936. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  2937. *go);
  2938. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  2939. tie(e1,found)=edge(vertex(*vertexIto,*go),vertex(*neighbourIto,*go),*go);
  2940. fichier2<<(*go)[*vertexIto]._index<<" -> "<<(*go)[*neighbourIto]._index<<" [label="<<(*go)[e1]._weight<<", fontsize=10, fontcolor= blue];"<<std::endl;
  2941. }
  2942. }
  2943. if(Color == true){
  2944. for(uint k=0; k<Partition.size(); k++){
  2945. for(uint j=0; j<Partition.at(k)->size(); j++)
  2946. {
  2947. fichier2<<Partition.at(k)->at(j)<<color.at(k)<<std::endl;
  2948. }
  2949. }
  2950. }
  2951. fichier2<<"}";
  2952. fichier2.close();
  2953. }else{
  2954. std::cout<<"Error : Le nombre de couleur est insuffisant pour réaliser l'affichange"<<std::endl;
  2955. }
  2956. }
  2957. void Affichage_OrientedGraph(OrientedGraph *go){
  2958. tie(vertexIto, vertexEndo) = vertices(*go);
  2959. for (; vertexIto != vertexEndo; ++vertexIto) {
  2960. std::cout<<(*go)[*vertexIto]._index<<" -> ";
  2961. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  2962. *go);
  2963. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  2964. std::cout<<(*go)[*neighbourIto]._index<<" ";
  2965. }
  2966. std::cout<<std::endl;
  2967. }
  2968. std::cout<<std::endl;
  2969. }
  2970. void Affichage_UnorientedGraph(UnorientedGraph *g){
  2971. tie(vertexIt, vertexEnd) = vertices(*g);
  2972. for (; vertexIt != vertexEnd; ++vertexIt) {
  2973. std::cout<<(*g)[*vertexIt]._index<<" -> ";
  2974. tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,
  2975. *g);
  2976. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2977. std::cout<<(*g)[*neighbourIt]._index<<" ";
  2978. }
  2979. std::cout<<std::endl;
  2980. }
  2981. std::cout<<std::endl;
  2982. }
  2983. double Total_weight_edges(UnorientedGraph *g){
  2984. double Sum_weight_edges = 0.;
  2985. edge_t e1;
  2986. bool found;
  2987. tie(vertexIt, vertexEnd) = vertices(*g);
  2988. for (; vertexIt != vertexEnd; ++vertexIt) {
  2989. tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,*g);
  2990. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  2991. if((*g)[*neighbourIt]._index>(*g)[*vertexIt]._index){
  2992. tie(e1,found)=edge(vertex(*vertexIt,*g),vertex(*neighbourIt,*g),*g);
  2993. Sum_weight_edges += (*g)[e1]._weight;
  2994. }
  2995. }
  2996. }
  2997. return Sum_weight_edges;
  2998. }
  2999. void Merge_Boost_Graph(OrientedGraph *go1, OrientedGraph *go2, std::vector<std::pair<int,int>> &connection, std::vector<double> &connection_weight){
  3000. edge_to e1;
  3001. bool found;
  3002. int nbr_go1 = num_vertices(*go1);
  3003. int nbr_go2 = num_vertices(*go2);
  3004. /*** Fusion ***/
  3005. if(nbr_go1 >= nbr_go2){
  3006. tie(vertexIto, vertexEndo) = vertices(*go2);
  3007. for (; vertexIto != vertexEndo; ++vertexIto){
  3008. vertex_to v0 = boost::add_vertex(*go1);
  3009. (*go1)[v0] = VertexProperties((*go2)[*vertexIto]._index, (*go2)[*vertexIto]._weight, NORMAL_PIXEL);
  3010. }
  3011. tie(vertexIto, vertexEndo) = vertices(*go2);
  3012. for (; vertexIto != vertexEndo; ++vertexIto){
  3013. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,*go2);
  3014. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  3015. tie(e1,found)=edge(vertex(*vertexIto,*go2),vertex(*neighbourIto,*go2),*go2);
  3016. add_edge(*vertexIto + nbr_go1, *neighbourIto + nbr_go1, (*go2)[e1]._weight, *go1);
  3017. }
  3018. }
  3019. /*** Connection ***/
  3020. /* Fonctionne si l'ordre de nomation respecte l'ordre boost sinon possibilité d'identification par nom*/
  3021. for(uint i = 0; i < connection.size(); i++){
  3022. add_edge(connection.at(i).first, connection.at(i).second, connection_weight.at(i) , *go1);
  3023. }
  3024. }else{
  3025. tie(vertexIto, vertexEndo) = vertices(*go1);
  3026. for (; vertexIto != vertexEndo; ++vertexIto){
  3027. vertex_to v0 = boost::add_vertex(*go2);
  3028. (*go2)[v0] = VertexProperties((*go1)[*vertexIto]._index, (*go1)[*vertexIto]._weight, NORMAL_PIXEL);
  3029. }
  3030. tie(vertexIto, vertexEndo) = vertices(*go1);
  3031. for (; vertexIto != vertexEndo; ++vertexIto){
  3032. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,*go1);
  3033. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  3034. tie(e1,found)=edge(vertex(*vertexIto,*go1),vertex(*neighbourIto,*go1),*go1);
  3035. add_edge(*vertexIto + nbr_go2, *neighbourIto + nbr_go2, (*go1)[e1]._weight, *go2);
  3036. }
  3037. }
  3038. /*** Connection ***/
  3039. /* Fonctionne si l'ordre de nomation respecte l'ordre boost sinon possibilité d'identification par nom*/
  3040. for(uint i = 0; i < connection.size(); i++){
  3041. add_edge(connection.at(i).first, connection.at(i).second, connection_weight.at(i) , *go2);
  3042. }
  3043. }
  3044. }
  3045. } } } // namespace paradevs tests boost_graph