Advertisement
Guest User

Untitled

a guest
Mar 15th, 2015
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.84 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. // for mmap:
  13. #include <sys/mman.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. using namespace std;
  17. using namespace boost;
  18.  
  19. //==========STRUCTURES==========
  20. // vertex
  21. struct VertexProperties {
  22.     int id;
  23.     int label;
  24.     VertexProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
  25. };
  26.  
  27. // edge
  28. struct EdgeProperties {
  29.     unsigned id;
  30.     unsigned label;
  31.     EdgeProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
  32. };
  33.  
  34. // Graph
  35. struct GraphProperties {
  36.     unsigned id;
  37.     unsigned label;
  38.     GraphProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
  39. };
  40.  
  41. // adjency list
  42. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties,
  43.                               GraphProperties> Graph;
  44.  
  45. // descriptors
  46.  
  47. typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
  48. typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
  49. // iterators
  50. typedef graph_traits<Graph>::vertex_iterator vertex_iter;
  51. typedef graph_traits<Graph>::edge_iterator edge_iter;
  52. typedef std::pair<edge_iter, edge_iter> edge_pair;
  53. //=================callback used fro subgraph_iso=================================================================
  54. struct my_callback {
  55.     template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
  56.     bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1 g) const {
  57.         return false;
  58.     }
  59. };
  60.  
  61. //==========handle_error==========
  62. void handle_error(const char *msg) {
  63.     perror(msg);
  64.     exit(255);
  65. }
  66. //============READ ALL THE FILE AND RETURN A STRING===================
  67. const char *readfromfile(const char *fname, size_t &length) {
  68.     int fd = open(fname, O_RDONLY);
  69.     if (fd == -1)
  70.         handle_error("open");
  71.  
  72.     // obtain file size
  73.     struct stat sb;
  74.     if (fstat(fd, &sb) == -1)
  75.         handle_error("fstat");
  76.  
  77.     length = sb.st_size;
  78.  
  79.     const char *addr = static_cast<const char *>(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0u));
  80.     if (addr == MAP_FAILED)
  81.         handle_error("mmap");
  82.  
  83.     // TODO close fd at some point in time, call munmap(...)
  84.     return addr;
  85. }
  86.  
  87. //==========SPLIT THE STRING BY NEWLINE (\n) ==========
  88. vector<string> splitstringtolines(string const& str) {
  89.     vector<string> split_vector;
  90.     split(split_vector, str, is_any_of("\n"));
  91.  
  92.     return split_vector;
  93. }
  94.  
  95. //============Get a string starting from pos============
  96. string getpos(int const& pos, string const& yy) {
  97.     size_t i = pos;
  98.     string str;
  99.     for (; yy[i] != ' ' && i < yy.length(); i++)
  100.         str += yy[i];
  101.     return str;
  102. }
  103. //==================read string vector and return graphs vector===================
  104. std::vector<Graph> creategraphs(std::vector<string> const& fichlines) {
  105.     std::vector<Graph> dataG;
  106.     int compide = 0; // compteur de id edge
  107.     for (string yy : fichlines) {
  108.         switch (yy[0]) {
  109.         case 't': {
  110.             string str2 = getpos(4, yy);
  111.             unsigned gid = atoi(str2.c_str());
  112.             dataG.emplace_back(GraphProperties(gid, gid));
  113.             compide = 0;
  114.         } break;
  115.         case 'v': {
  116.             assert(!dataG.empty()); // assert will terminate the program  if its argument turns out to be false
  117.             // cout<<yy<<endl;
  118.             int vId, vLabel;
  119.             string vvv = getpos(2, yy);
  120.             vId = atoi(vvv.c_str());
  121.             string vvvv = getpos((int)vvv.length() + 3, yy);
  122.             // cout<<vvvv<<endl;
  123.             vLabel = atoi(vvvv.c_str());
  124.             boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
  125.         }
  126.  
  127.         break;
  128.  
  129.         case 'e': { // cout<<yy<<endl;
  130.             assert(!dataG.empty()); // assert will terminate the program  if its argument turns out to be false
  131.  
  132.             int fromId, toId, eLabel;
  133.             string eee = getpos(2, yy);
  134.             // cout<<eee<<endl;
  135.             fromId = atoi(eee.c_str());
  136.             string eee2 = getpos((int)eee.length() + 3, yy);
  137.             // cout<<eee2<<endl;
  138.             toId = atoi(eee2.c_str());
  139.             int c = (int)eee.length() + (int)eee2.length() + 4;
  140.             //    cout<<c<<endl;
  141.             string eee3 = getpos(c, yy);
  142.             //  cout<<eee3<<endl;
  143.             eLabel = atoi(eee3.c_str());
  144.             boost::add_edge(fromId, toId, EdgeProperties(compide, eLabel), dataG.back());
  145.             compide++;
  146.         } break;
  147.         }
  148.     }
  149.  
  150.     return dataG;
  151. }
  152. //============test if the graph connectivity========================================================
  153. bool graphconnexe(Graph const& g) {
  154.     return num_edges(g) >= num_vertices(g) - 1;
  155. }
  156. //====================print the graph information========================================================
  157. void printgraph(Graph const& gr) {
  158.     typedef std::pair<edge_iter, edge_iter> edge_pair;
  159.  
  160.     std::cout << " contains " << num_vertices(gr) << " vertices, and " << num_edges(gr) << " edges " << std::endl;
  161.     if (graphconnexe(gr)) {
  162.  
  163.         // Vertex list
  164.         if (num_vertices(gr) != 0) {
  165.             std::cout << "  Vertex list: " << std::endl;
  166.             for (size_t i = 0; i < num_vertices(gr); ++i) // size_t vertice number in the graph
  167.             {
  168.                 std::cout << "   v[" << i << "]   ID: " << gr[i].id << ", Label: " << gr[i].label << std::endl;
  169.             }
  170.         }
  171.         // Edge list
  172.         if (num_edges(gr) != 0) {
  173.             std::cout << "  Edge list: " << std::endl;
  174.             edge_pair ep;
  175.             for (ep = edges(gr); ep.first != ep.second; ++ep.first) // ep edge number
  176.             {
  177.                 vertex_t from = source(*ep.first, gr);
  178.                 vertex_t to = target(*ep.first, gr);
  179.                 edge_t edg = edge(from, to, gr);
  180.                 std::cout << "   e(" << gr[from].id << "," << gr[to].id << ")   ID: " << gr[edg.first].id
  181.                           << " ,  Label: " << gr[edg.first].label << std::endl;
  182.             }
  183.         }
  184.         std::cout << "\n\n" << std::endl;
  185.     } else {
  186.         cout << "Please check this graph connectivity." << endl;
  187.     }
  188. }
  189.  
  190. //=========================================================
  191. /*bool gUe(Graph &g, edge_iter ep, Graph t) {
  192.  
  193.     vertex_t from = source(*ep, t);
  194.     vertex_t to = target(*ep, t);
  195.  
  196.     Graph::edge_descriptor copied_edge = boost::add_edge(from, to, t[*ep], g).first;
  197.  
  198.     g[source(copied_edge, g)] = t[from];
  199.     g[target(copied_edge, g)] = t[to];
  200.  
  201.     if (graphconnexe(g) && graphconnexe(t)) {
  202.         return vf2_subgraph_iso(g, t, my_callback());
  203.     } else {
  204.         return false;
  205.     }
  206. }*/
  207. //=================test if the given vertice exist in the graph=========================
  208. bool verticeexist(Graph const& g, int const& vId, int const& vlabel) {
  209.     int cpt = 0;
  210.     if (num_edges(g) != 0) {
  211.         for (size_t i = 0; i < num_vertices(g); ++i) // size_t vertice number in the graph
  212.         {
  213.             if ((g[i].id == vId) && (g[i].label == vlabel)) {
  214.                 cpt++;
  215.             }
  216.         }
  217.     }
  218.     return cpt != 0;
  219. }
  220. //=============test if the given edge exist in the graph===========================
  221. bool edgeexist(Graph const& g, int const& fromid, int const& toid, unsigned const& elabel) {
  222.     int bn = 0;
  223.     if (graphconnexe(g)) {
  224.         if (num_edges(g) != 0) {
  225.             edge_pair ep;
  226.             for (ep = edges(g); ep.first != ep.second; ++ep.first) // ep edge number
  227.             {
  228.                 vertex_t from = source(*ep.first, g);
  229.                 vertex_t to = target(*ep.first, g);
  230.                 edge_t edg = edge(from, to, g);
  231.  
  232.                 if ((g[from].id == fromid) && (g[to].id == toid) && (g[edg.first].label == elabel)) {
  233.                     bn++;
  234.                 }
  235.             }
  236.         }
  237.     }
  238.  
  239.     return (bn != 0);
  240. }
  241.  
  242. // =============test if thoses vertices are neighbours============================
  243. bool verticesareneighbours(Graph const& g, int const& a, int const& b) {
  244.     int bn = 0;
  245.     if (graphconnexe(g)) {
  246.  
  247.         if (num_edges(g) != 0) {
  248.             edge_pair ep;
  249.             for (ep = edges(g); ep.first != ep.second; ++ep.first) // ep edge number
  250.             {
  251.                 vertex_t from = source(*ep.first, g);
  252.                 vertex_t to = target(*ep.first, g);
  253.  
  254.                 if (((g[from].id == a) || (g[to].id == a)) && ((g[from].id == b) || (g[to].id == b))) {
  255.                     bn++;
  256.                 }
  257.             }
  258.         }
  259.     }
  260.  
  261.     return (bn != 0);
  262. }
  263. //=============test if those edges are neighbours=============================
  264.     template <typename Graph, typename E = typename boost::graph_traits<Graph>::edge_descriptor>
  265.     bool edgesareneighbours(Graph const& g, E e1, E e2) {
  266.  
  267.         std::set<vertex_t> vertex_set {
  268.             source(e1, g), target(e1, g),
  269.             source(e2, g), target(e2, g),
  270.         };
  271.  
  272.         return graphconnexe(g) && vertex_set.size() < 4;
  273.     }
  274. //===============if the graph is empty add the edge with vertices===========================
  275. void emptygraphaddedge(Graph &g, int fromId, int toId, int eLabel) {
  276.     if (num_edges(g) == 0) {
  277.         boost::add_edge(fromId, toId, EdgeProperties(num_edges(g) + 1, eLabel), g);
  278.     }
  279. }
  280.  
  281. //==============================M A I N   P R O G R A M =======================================
  282. int main() {
  283.     clock_t start = std::clock();
  284.     size_t length;    
  285.  
  286.     std::vector<Graph> dataG = creategraphs(splitstringtolines(readfromfile("testgUe.txt", length)));
  287.  
  288.     typedef std::pair<edge_iter, edge_iter> edge_pair;
  289.  
  290.       if (!dataG.empty()) {
  291.  
  292.         cout<<"graphconnexe?"<<graphconnexe(dataG[0])<<endl;
  293.         cout<<"verticeexist?"<<verticeexist(dataG[0],1,4)<<endl;
  294.         cout<<"verticeexist?"<<verticeexist(dataG[0],4,2)<<endl;
  295.         cout<<"verticesareneighbours?"<<verticesareneighbours(dataG[0],1,4)<<endl;
  296.         cout<<"verticesareneighbours?"<<verticesareneighbours(dataG[0],4,2)<<endl;
  297.         cout<<"edgeexist?"<<edgeexist(dataG[0],1,4,16)<<endl;
  298.         cout<<"edgeexist?"<<edgeexist(dataG[0],1,4,12)<<endl;
  299.        
  300.         edge_pair ep = edges(dataG[0]);
  301.  
  302.         if (size(ep) >= 2) {
  303.              Graph::edge_descriptor e1 = *ep.first++;
  304.              Graph::edge_descriptor e2 = *ep.first++;                                                    
  305.              cout << "edgesareneighbours?" << edgesareneighbours(dataG[0], e1, e2) << endl;
  306.              
  307.         }
  308.     }
  309.  
  310.     // end and time
  311.     cout << "FILE Contains " << dataG.size() << " graphs.\nTIME: " << (std::clock() - start) / (double)CLOCKS_PER_SEC
  312.          << "s" << endl; // fin du programme.
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement