Advertisement
Guest User

Untitled

a guest
Mar 25th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.23 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 thoses vertices are neighbours============================
  255. bool verticesareneighbours(Graph const& g, int const& a, int const& b) {
  256.     int bn = 0;
  257.     if (graphconnexe(g)) {
  258.  
  259.         if (num_edges(g) != 0) {
  260.             edge_pair ep;
  261.             for (ep = edges(g); ep.first != ep.second; ++ep.first) // ep edge number
  262.             {
  263.                 vertex_t from = source(*ep.first, g);
  264.                 vertex_t to = target(*ep.first, g);
  265.  
  266.                 if (((g[from].id == a) || (g[to].id == a)) && ((g[from].id == b) || (g[to].id == b))) {
  267.                     bn++;
  268.                 }
  269.             }
  270.         }
  271.     }
  272.  
  273.     return (bn != 0);
  274. }
  275. //=============test if those edges are neighbours=============================
  276.     template <typename Graph, typename E = typename boost::graph_traits<Graph>::edge_descriptor>
  277.     bool edgesareneighbours(Graph const& g, E e1, E e2) {
  278.  
  279.         std::set<vertex_t> vertex_set {
  280.             source(e1, g), target(e1, g),
  281.             source(e2, g), target(e2, g),
  282.         };
  283.  
  284.         return graphconnexe(g) && vertex_set.size() < 4;
  285.     }
  286. //===============if the graph is empty add the edge with vertices===========================
  287. void emptygraphaddedge(Graph& testg,std::vector<Graph> dataG) {
  288.  
  289.       if (!dataG.empty()) {
  290.         auto const& gr = dataG.front(); // firslt graph in G_list
  291.         auto ep = edges(gr).first; // first edge in gr
  292.  
  293.         vertex_t from = source(*ep, gr);
  294.         vertex_t to = target(*ep, gr);
  295.  
  296.         //int cpt=0;
  297.         boost::add_vertex(VertexProperties(gr[from].id, gr[from].label),testg);
  298.         //boost::add_vertex(VertexProperties(cpt, gr[from].label),testg);
  299.  
  300.         boost::add_vertex(VertexProperties(gr[to].id, gr[to].label),testg);
  301.         //boost::add_vertex(VertexProperties(cpt+1, gr[to].label),testg);
  302.  
  303.  
  304.  
  305.         if(verticeexist(testg,gr[from].id,gr[from].label)&&verticeexist(testg,gr[to].id,gr[to].label))
  306.         {
  307.             int cpt=0;
  308.             Graph::edge_descriptor copied_edge = boost::add_edge(cpt, cpt++, gr[*ep],testg).first;
  309.             testg[source(copied_edge, gr)] = gr[from];
  310.             testg[target(copied_edge,gr)] = gr[to];
  311.             cout<<"e ("<<testg[source(copied_edge, gr)].id<<","<<testg[target(copied_edge, gr)].id<<") Added successfully."<<endl;
  312.         }
  313.         else{cout<<"vertices doesn't exist ! "<<endl;}
  314.  
  315.     }
  316.  
  317. }
  318. //======================================================
  319. float frequency(Graph const& g1,std::vector<Graph> dataG)
  320. {
  321.     int iso=0;
  322.  
  323.     if(emptygraph(g1)){return 1;}
  324.     else{
  325.     for (Graph g2: dataG)
  326.     {
  327.         if(vf2_subgraph_iso(g1,g2,my_callback())){iso++;}
  328.     }
  329.  
  330.     return (iso/dataG.size());
  331.     }
  332.  
  333. }
  334. //==========test if the edge is connexe with the graph=============
  335. bool graphconnexewithedge(Graph g, int const& fromid, int const& toid) {
  336.     return (idverticeexist(g,fromid)|| idverticeexist(g,toid));
  337. }
  338. //=========test if the aumentation is is a subgraph of the original graph=================================
  339. void augmentation(Graph &M, Graph &g, int const& fromid, int const& toid, unsigned const& elabel, unsigned const& edgeid) {
  340.  
  341.  
  342.     if(!idverticeexist(g,fromid)){ int vlabel=getvlabel(M,fromid);
  343.                boost::add_vertex(VertexProperties(fromid, vlabel), g);
  344.                                   }
  345.  
  346.     if(!idverticeexist(g,toid)){ int vlabel=getvlabel(M,toid);
  347.         boost::add_vertex(VertexProperties(toid, vlabel), g);
  348.     }
  349.  
  350.     boost::add_edge(fromid, toid, EdgeProperties(edgeid, elabel), g);
  351.  
  352. }
  353. //===========================================================
  354.  
  355. std::vector<edge_iter> edgesdiff(Graph const& g1,Graph const& g2){
  356.  
  357. std::vector<edge_iter> v1;
  358. std::vector<edge_iter> v2;
  359. std::vector<edge_iter> result;
  360.  
  361. typedef graph_traits<Graph>::edge_iterator edge_iter;
  362. std::pair<edge_iter, edge_iter> ep;
  363.  
  364. edge_iter ei, ei_end;
  365. for (tie(ei, ei_end) = edges(g1); ei != ei_end; ++ei){v1.push_back(ei);}
  366. for (tie(ei, ei_end) = edges(g2); ei != ei_end; ++ei){v2.push_back(ei);}
  367.  
  368. /* d i f f e r e n c e*/
  369. return result;
  370. }
  371.  
  372. //==============================M A I N   P R O G R A M =======================================
  373. int main() {
  374.     //clock_t start = std::clock();
  375.     size_t length;
  376.     std::vector<Graph> dataG =creategraphs(splitstringtolines(readfromfile("5.txt", length)));
  377.  
  378. //cout<<"getvlabel(dataG[0],7)="<<getvlabel(dataG[0],7)<<endl;
  379.     cout<<edgesdiff(dataG[0],dataG[1]).size()<<endl;
  380.  
  381. /*
  382.     cout<<"\n\n\n****testg****"<<endl;
  383.     Graph testg;
  384.     cout<<"frequency(testg,dataG)="<<frequency(testg,dataG)<<endl;
  385.     emptygraphaddedge(testg,dataG);
  386.     printgraph(testg);
  387.     cout<<"frequency(dataG[0],dataG)="<<frequency(dataG[0],dataG)<<endl;
  388.  
  389.     cout << "FILE Contains " << dataG.size() << " graphs.\nTIME: " << (std::clock() - start) / (double)CLOCKS_PER_SEC<< "s" << endl; // fin du programme.
  390.  
  391. */
  392.  
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement