Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.36 KB | None | 0 0
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <boost/graph/vf2_sub_graph_iso.hpp>
  3. #include <boost/algorithm/string/split.hpp>
  4. #include <boost/algorithm/string/classification.hpp>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <ctime>
  11. #include <queue> // std::queue
  12.  
  13. //for mmap:
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17.  
  18. using namespace std;
  19. using namespace boost;
  20.  
  21. //==========STRUCTURES==========
  22. // vertex
  23. struct VertexProperties {
  24.     int id;
  25.     int label;
  26.     VertexProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
  27. };
  28.  
  29. // edge
  30. struct EdgeProperties {
  31.     unsigned id;
  32.     unsigned label;
  33.     EdgeProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
  34. };
  35.  
  36. // Graph
  37. struct GraphProperties {
  38.     unsigned id;
  39.     unsigned label;
  40.     GraphProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
  41. };
  42.  
  43. // adjency list
  44. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties,
  45.                               GraphProperties> Graph;
  46.  
  47. // descriptors
  48.  
  49. typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
  50. typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
  51. // iterators
  52. typedef graph_traits<Graph>::vertex_iterator vertex_iter;
  53. typedef graph_traits<Graph>::edge_iterator edge_iter;
  54. typedef std::pair<edge_iter, edge_iter> edge_pair;
  55. //=================callback used fro subgraph_iso=================================================================
  56. struct my_callback {
  57.     template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
  58.     bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1 g) const {
  59.         return false;
  60.     }
  61. };
  62.  
  63. //==========handle_error==========
  64. void handle_error(const char *msg) {
  65.     perror(msg);
  66.     exit(255);
  67. }
  68. //============READ ALL THE FILE AND RETURN A STRING===================
  69. const char *readfromfile(const char *fname, size_t &length) {
  70.     int fd = open(fname, O_RDONLY);
  71.     if (fd == -1)
  72.         handle_error("open");
  73.  
  74.     // obtain file size
  75.     struct stat sb;
  76.     if (fstat(fd, &sb) == -1)
  77.         handle_error("fstat");
  78.  
  79.     length = sb.st_size;
  80.  
  81.     const char *addr = static_cast<const char *>(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0u));
  82.     if (addr == MAP_FAILED)
  83.         handle_error("mmap");
  84.  
  85.     // TODO close fd at some point in time, call munmap(...)
  86.     return addr;
  87. }
  88. //==========SPLIT THE STRING BY NEWLINE (\n) ==========
  89. vector<string> splitstringtolines(string const& str) {
  90.     vector<string> split_vector;
  91.     split(split_vector, str, is_any_of("\n"));
  92.  
  93.     return split_vector;
  94. }
  95.  
  96. //============Get a string starting from pos============
  97. string getpos(int const& pos, string const& yy) {
  98.     size_t i = pos;
  99.     string str;
  100.     for (; yy[i] != ' ' && i < yy.length(); i++)
  101.         str += yy[i];
  102.     return str;
  103. }
  104. //==================read string vector and return graphs vector===================
  105. std::vector<Graph> creategraphs(std::vector<string> const& fichlines) {
  106.     std::vector<Graph> dataG;
  107.     int compide = 0; // compteur de id edge
  108.     for (string yy : fichlines) {
  109.         switch (yy[0]) {
  110.         case 't': {
  111.             string str2 = getpos(4, yy);
  112.             unsigned gid = atoi(str2.c_str());
  113.             dataG.emplace_back(GraphProperties(gid, gid));
  114.             compide = 0;
  115.         } break;
  116.         case 'v': {
  117.             assert(!dataG.empty()); // assert will terminate the program  if its argument turns out to be false
  118.             // cout<<yy<<endl;
  119.             int vId, vLabel;
  120.             string vvv = getpos(2, yy);
  121.             vId = atoi(vvv.c_str());
  122.             string vvvv = getpos((int)vvv.length() + 3, yy);
  123.             // cout<<vvvv<<endl;
  124.             vLabel = atoi(vvvv.c_str());
  125.             boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
  126.         }
  127.  
  128.         break;
  129.  
  130.         case 'e': { // cout<<yy<<endl;
  131.             assert(!dataG.empty()); // assert will terminate the program  if its argument turns out to be false
  132.  
  133.             int fromId, toId, eLabel;
  134.             string eee = getpos(2, yy);
  135.             // cout<<eee<<endl;
  136.             fromId = atoi(eee.c_str());
  137.             string eee2 = getpos((int)eee.length() + 3, yy);
  138.             // cout<<eee2<<endl;
  139.             toId = atoi(eee2.c_str());
  140.             int c = (int)eee.length() + (int)eee2.length() + 4;
  141.             //    cout<<c<<endl;
  142.             string eee3 = getpos(c, yy);
  143.             //  cout<<eee3<<endl;
  144.             eLabel = atoi(eee3.c_str());
  145.             boost::add_edge(fromId, toId, EdgeProperties(compide, eLabel), dataG.back());
  146.             compide++;
  147.         } break;
  148.         }
  149.     }
  150.  
  151.     return dataG;
  152. }
  153. //============test if the graph is empty========================================================
  154. bool emptygraph(Graph const& g) {
  155.     return ((num_edges(g)==0)&&(num_vertices(g)==0));
  156. }
  157. //============test if the graph connectivity========================================================
  158. bool graphconnexe(Graph const& g) {
  159.     return num_edges(g) >= num_vertices(g) - 1;
  160. }
  161. //====================print the graph information========================================================
  162. void printgraph(Graph const& gr) {
  163.     typedef std::pair<edge_iter, edge_iter> edge_pair;
  164.     std::cout <<"=================================="<<endl;
  165.     std::cout <<" contains " << num_vertices(gr) << " vertices, and " << num_edges(gr) << " edges " << std::endl;
  166.     if (graphconnexe(gr)) {
  167.  
  168.         // Vertex list
  169.         if (num_vertices(gr) != 0) {
  170.             std::cout << "  Vertex list: " << std::endl;
  171.             for (size_t i = 0; i < num_vertices(gr); ++i) // size_t vertice number in the graph
  172.             {
  173.                 std::cout << " ID: " << gr[i].id << ", Label: " << gr[i].label << std::endl;
  174.             }
  175.         }
  176.         // Edge list
  177.         if (num_edges(gr) != 0) {
  178.             std::cout << "  Edge list: " << std::endl;
  179.             edge_pair ep;
  180.             for (ep = edges(gr); ep.first != ep.second; ++ep.first) // ep edge number
  181.             {
  182.                 vertex_t from = source(*ep.first, gr);
  183.                 vertex_t to = target(*ep.first, gr);
  184.                 edge_t edg = edge(from, to, gr);
  185.                 std::cout << "   e(" << gr[from].id << "," << gr[to].id << ")   ID: " << gr[edg.first].id
  186.                           << " ,  Label: " << gr[edg.first].label << std::endl;
  187.             }
  188.         }
  189.         std::cout<<endl;
  190.     } else {
  191.         cout << "Please check this graph connectivity." << endl;
  192.     }
  193. }
  194.  
  195. //=================test if the given vertice exist in the graph=========================
  196. bool verticeexist(Graph const& g, int const& vId, int const& vlabel) {
  197.     int cpt = 0;
  198.  
  199.         for (size_t i = 0; i < num_vertices(g); ++i) // size_t vertice number in the graph
  200.         {
  201.             if ((g[i].id == vId) && (g[i].label == vlabel)) {
  202.                 cpt++;
  203.             }
  204.         }
  205.  
  206.     return cpt != 0;
  207. }
  208. //======================================================================
  209. bool idverticeexist(Graph const& g, int const& vId) {
  210.     int cpt = 0;
  211.     if (num_edges(g) != 0) {
  212.         for (size_t i = 0; i < num_vertices(g); ++i) // size_t vertice number in the graph
  213.         {
  214.             if (g[i].id == vId) {
  215.                 cpt++;
  216.             }
  217.         }
  218.     }
  219.     return cpt != 0;
  220. }
  221. //=======get vertice label label============================
  222. int getvlabel(Graph const& M,int const& id){
  223. int cpt=-1;
  224.  for (size_t i = 0; i < num_vertices(M); ++i){
  225.             if (M[i].id == id) {
  226.                 cpt=M[i].label;
  227.             }
  228.         }
  229.  
  230. return cpt;
  231. }
  232. //=============test if the given edge exist in the graph===========================
  233. bool edgeexist(Graph const& g, int const& fromid, int const& toid, unsigned const& elabel) {
  234.     int bn = 0;
  235.     if (graphconnexe(g)) {
  236.         if (num_edges(g) != 0) {
  237.             edge_pair ep;
  238.             for (ep = edges(g); ep.first != ep.second; ++ep.first) // ep edge number
  239.             {
  240.                 vertex_t from = source(*ep.first, g);
  241.                 vertex_t to = target(*ep.first, g);
  242.                 edge_t edg = edge(from, to, g);
  243.  
  244.                 if ((g[from].id == fromid) && (g[to].id == toid) && (g[edg.first].label == elabel)) {
  245.                     bn++;
  246.                 }
  247.             }
  248.         }
  249.     }
  250.  
  251.     return (bn != 0);
  252. }
  253.  
  254. //===============test if edges are equal=============================
  255. bool edgesareequal(Graph const& g1,edge_iter const& e1,Graph const& g2,edge_iter const& e2){
  256.  
  257. vertex_t frome1 = source(*e1, g1);
  258. vertex_t toe1 = target(*e1, g1);
  259. edge_t edg1 = edge(frome1, toe1, g1);
  260. int label1 =g1[edg1.first].label;
  261.  
  262. vertex_t frome2 = source(*e2, g2);
  263. vertex_t toe2 = target(*e2, g2);
  264. edge_t edg2 = edge(frome2, toe2, g2);
  265. int label2 =g2[edg2.first].label;
  266.  
  267.  
  268. return((frome1==frome2)&&(toe1==toe2)&&(label1==label2));
  269. }
  270.  
  271.  
  272. //===============if the graph is empty add the edge with vertices===========================
  273. void emptygraphaddedge(Graph& testg,std::vector<Graph> dataG) {
  274.  
  275.       if (!dataG.empty()) {
  276.         auto const& gr = dataG.front(); // firslt graph in G_list
  277.         auto ep = edges(gr).first; // first edge in gr
  278.  
  279.         vertex_t from = source(*ep, gr);
  280.         vertex_t to = target(*ep, gr);
  281.  
  282.         //int cpt=0;
  283.         boost::add_vertex(VertexProperties(gr[from].id, gr[from].label),testg);
  284.         //boost::add_vertex(VertexProperties(cpt, gr[from].label),testg);
  285.  
  286.         boost::add_vertex(VertexProperties(gr[to].id, gr[to].label),testg);
  287.         //boost::add_vertex(VertexProperties(cpt+1, gr[to].label),testg);
  288.  
  289.  
  290.  
  291.         if(verticeexist(testg,gr[from].id,gr[from].label)&&verticeexist(testg,gr[to].id,gr[to].label))
  292.         {
  293.             int cpt=0;
  294.             Graph::edge_descriptor copied_edge = boost::add_edge(cpt, cpt++, gr[*ep],testg).first;
  295.             testg[source(copied_edge, gr)] = gr[from];
  296.             testg[target(copied_edge,gr)] = gr[to];
  297.             cout<<"e ("<<testg[source(copied_edge, gr)].id<<","<<testg[target(copied_edge, gr)].id<<") Added successfully."<<endl;
  298.         }
  299.         else{cout<<"vertices doesn't exist ! "<<endl;}
  300.  
  301.     }
  302.  
  303. }
  304. //======================================================
  305. float frequency(Graph const& g1,std::vector<Graph> dataG)
  306. {
  307.     int iso=0;
  308.  
  309.     if(emptygraph(g1)){return 1;}
  310.     else{
  311.     for (Graph g2: dataG)
  312.     {
  313.         if(vf2_subgraph_iso(g1,g2,my_callback())){iso++;}
  314.     }
  315.  
  316.     }
  317. return (iso/dataG.size());
  318. }
  319. //==========test if the edge is connexe with the graph=============
  320. bool graphconnexewithedge(Graph g, int const& fromid, int const& toid) {
  321.     return (idverticeexist(g,fromid)|| idverticeexist(g,toid));
  322. }
  323. //=========test if the aumentation is is a subgraph of the original graph=================================
  324. void augmentation(Graph &M, Graph &g, int const& fromid, int const& toid, unsigned const& elabel, unsigned const& edgeid) {
  325.  
  326.  
  327.     if(!idverticeexist(g,fromid)){ int vlabel=getvlabel(M,fromid);
  328.                boost::add_vertex(VertexProperties(fromid, vlabel), g);
  329.                                   }
  330.  
  331.     if(!idverticeexist(g,toid)){ int vlabel=getvlabel(M,toid);
  332.         boost::add_vertex(VertexProperties(toid, vlabel), g);
  333.     }
  334.  
  335.     boost::add_edge(fromid, toid, EdgeProperties(edgeid, elabel), g);
  336.  
  337. }
  338. //===========================================================
  339. bool edgeexistinavector(Graph const& g1,edge_iter const& e,Graph const& g2,std::vector<edge_iter>  const& v){
  340. int cpt=0;
  341. for(auto x:v) {if(edgesareequal(g1,e,g2,x)){cpt++;}
  342. }
  343.  
  344. return(cpt!=0);
  345. }
  346.  
  347. //===========================================================
  348.  
  349. std::vector<int*>  edgesdiff(Graph const& g1,Graph const& g2){
  350.  
  351.  
  352. std::vector<edge_iter> v1;
  353. std::vector<edge_iter> v2;
  354. //std::vector<int [3]> result;
  355.  
  356. std::vector<int*> result = std::vector<int*>();
  357.  
  358. edge_iter ei, ei_end;
  359. for (tie(ei, ei_end) = edges(g1); ei != ei_end; ++ei){v1.push_back(ei);}
  360. for (tie(ei, ei_end) = edges(g2); ei != ei_end; ++ei){v2.push_back(ei);}
  361. cout<<"***Insertion diff***"<<endl;
  362. for(auto x:v1){if(!edgeexistinavector(g1,x,g2,v2)){
  363. int t[3];
  364. vertex_t from = source(*x, g1);
  365. t[0]=g1[from].id;
  366. cout<<"g1[from].id="<<g1[from].id<<endl;
  367. vertex_t to = target(*x, g1);
  368. t[1]=g1[to].id;
  369. cout<<"g1[to].id="<<g1[to].id<<endl;
  370. edge_t edg = edge(from, to, g1);
  371. t[2]=g1[edg.first].label;
  372. cout<<"g1[edg.first].label="<<g1[edg.first].label<<endl;
  373. result.push_back(t);
  374. }
  375. }
  376.  
  377. return result;
  378. }
  379.  
  380. //==============================M A I N   P R O G R A M =======================================
  381. int main() {
  382. clock_t start = std::clock();
  383. size_t length;
  384. std::vector<Graph> dataG =creategraphs(splitstringtolines(readfromfile("test2.txt", length)));
  385.  
  386. if (!dataG.empty())
  387.     {
  388.         auto const& g1 = dataG[0]; // firslt graph in G_list
  389.         auto const& g2 = dataG[1]; // firslt graph in G_list
  390.         /*
  391.         auto ep = edges(g1).first; // first edge in gr
  392.  
  393.  
  394.         std::vector<edge_iter> vec;
  395.         edge_iter ei, ei_end;
  396.         for (tie(ei, ei_end) = edges(g2); ei != ei_end; ++ei){vec.push_back(ei);}
  397.  
  398.         cout<<edgeexistinavector(g1,ep,g2,vec)<<endl;
  399.         */
  400.  
  401.     std::vector<int*>  diff=edgesdiff(g1,g2);
  402.     cout<<"\n\n***AFFICHAGE***"<<endl;
  403.  
  404.     for(auto d:diff)
  405.     {
  406.         for(int i=0;i<3;i++)
  407.         {
  408.             cout<<d[i]<<" ";
  409.         }
  410.         cout<<endl;
  411.     }
  412.     }
  413.  
  414.  
  415.  
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement