utils.hpp 135 KB

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