Advertisement
Guest User

Untitled

a guest
Mar 14th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.74 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. {
  23.     int id;
  24.     int label;
  25.     VertexProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
  26. };
  27.  
  28. // edge
  29. struct EdgeProperties
  30. {
  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. {
  39.     unsigned id;
  40.     unsigned label;
  41.     GraphProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
  42. };
  43.  
  44. // adjency list
  45. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
  46.         VertexProperties,
  47.         EdgeProperties,
  48.         GraphProperties> Graph;
  49.  
  50. // descriptors
  51.  
  52. typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
  53. typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
  54. // iterators
  55. typedef graph_traits<Graph>::vertex_iterator vertex_iter;
  56. typedef graph_traits<Graph>::edge_iterator edge_iter;
  57. typedef std::pair<edge_iter, edge_iter> edge_pair;
  58. //==========READ ALL THE FILE AND RETURN A STRING==========
  59. std::string readfromfile(string const& fich)
  60. {
  61.     ifstream inFile;
  62.     inFile.open(fich);//open the input file
  63.  
  64.     stringstream strStream;
  65.     strStream << inFile.rdbuf();//read the file
  66.     return strStream.str();//str holds the content of the file
  67. }
  68. //===============================
  69. void handle_error(const char* msg) {
  70.     perror(msg);
  71.     exit(255);
  72. }
  73. //===============================
  74. const char* readfromfile2(const char* fname, size_t& length)
  75. {
  76.     int fd = open(fname, O_RDONLY);
  77.     if (fd == -1) handle_error("open");
  78.  
  79.     // obtain file size
  80.     struct stat sb;
  81.     if (fstat(fd, &sb) == -1) handle_error("fstat");
  82.  
  83.     length = sb.st_size;
  84.  
  85.     const char* addr = static_cast<const char*>(mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0u));
  86.     if (addr == MAP_FAILED) handle_error("mmap");
  87.  
  88.     // TODO close fd at some point in time, call munmap(...)
  89.     return addr;
  90. }
  91. //==========SPLIT THE STRING BY NEWLINE (\n) ==========
  92. vector<string>  splitstringtolines(string const& str)
  93. {
  94.     vector<string> split_vector;
  95.     split(split_vector, str, is_any_of("\n"));
  96.  
  97.     return split_vector;
  98. }
  99.  
  100. //==============================================================
  101. string getpos(int pos, string yy)
  102. {
  103.     int i=pos; string str;
  104.     for(;yy[i]!=' '&&i<yy.length();i++) str+=yy[i];
  105.     return str;
  106. }
  107. //==============================================================
  108. std::vector<Graph> creategraphs(std::vector<string> const& fichlines)
  109. {
  110.     std::vector<Graph> dataG;
  111.     int compide=0; //compteur de id edge
  112.     for (string yy:fichlines)
  113.     {
  114.         switch (yy[0])
  115.         {
  116.             case 't':
  117.             {
  118.  
  119.                 string str2=getpos(4,yy);
  120.                 unsigned gid=atoi(str2.c_str());
  121.                 dataG.emplace_back(GraphProperties (gid, gid));
  122.                 compide=0;
  123.             }
  124.             break;
  125.             case 'v':
  126.             {
  127.                 assert(!dataG.empty());//assert will terminate the program  if its argument turns out to be false
  128.                // cout<<yy<<endl;
  129.                 int vId, vLabel;
  130.                 string vvv=getpos(2,yy);
  131.                 vId=atoi(vvv.c_str());
  132.                 string vvvv=getpos((int)vvv.length()+3,yy);
  133.                 //cout<<vvvv<<endl;
  134.                 vLabel=atoi(vvvv.c_str());
  135.                 boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
  136.             }
  137.  
  138.                 break;
  139.  
  140.             case 'e':
  141.             {  // cout<<yy<<endl;
  142.                 assert(!dataG.empty());//assert will terminate the program  if its argument turns out to be false
  143.  
  144.                 int fromId, toId, eLabel;
  145.                 string eee=getpos(2,yy);
  146.                 //cout<<eee<<endl;
  147.                 fromId=atoi(eee.c_str());
  148.                 string eee2=getpos((int)eee.length()+3,yy);
  149.                 //cout<<eee2<<endl;
  150.                 toId=atoi(eee2.c_str());
  151.                 int c=(int)eee.length()+(int)eee2.length()+4;
  152.             //    cout<<c<<endl;
  153.                 string eee3=getpos(c,yy);
  154.               //  cout<<eee3<<endl;
  155.                 eLabel=atoi(eee3.c_str());
  156.                     boost::add_edge(fromId, toId, EdgeProperties(compide, eLabel), dataG.back());
  157.                 compide++;
  158.                 }
  159.                 break;
  160.         }
  161.  
  162.     }
  163.  
  164.  
  165.     return dataG;
  166. }
  167. //==================================================================================
  168. bool graphconnexe(Graph const& g)
  169. {
  170.    return num_edges(g)>=num_vertices(g)-1;
  171. }
  172. //============================================================================
  173. void printgraph(Graph const& gr)
  174. {
  175.         typedef std::pair<edge_iter, edge_iter> edge_pair;
  176.      
  177.        
  178.         std::cout<< " contains " << num_vertices(gr) << " vertices, and " << num_edges(gr) << " edges " << std::endl;        
  179.     if(graphconnexe(gr))
  180.     {
  181.        
  182.         //Vertex list
  183.         if(num_vertices(gr)!=0)
  184.         {
  185.             std::cout << "  Vertex list: " << std::endl;
  186.             for (size_t i = 0 ; i < num_vertices(gr) ; ++i) //size_t vertice number in the graph
  187.             {
  188.               std::cout << "   v[" << i << "]   ID: " << gr[i].id << ", Label: " << gr[i].label << std::endl;
  189.             }
  190.         }
  191.         //Edge list
  192.         if(num_edges(gr)!=0)
  193.         {
  194.             std::cout << "  Edge list: " << std::endl;
  195.             edge_pair ep;
  196.             for (ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number
  197.             {
  198.               vertex_t from = source(*ep.first, gr);
  199.               vertex_t to = target(*ep.first, gr);
  200.               edge_t edg = edge(from, to, gr);
  201.               std::cout << "   e(" << gr[from].id << "," << gr[to].id << ")   ID: " << gr[edg.first].id << " ,  Label: " <<  gr[edg.first].label << std::endl;
  202.             }
  203.         }
  204.         std::cout<<"\n\n" << std::endl;
  205.      }
  206.      else{cout<<"Please check this graph connectivity."<<endl;}  
  207. }
  208. //==================================================================================
  209. struct my_callback
  210. {
  211.     template <typename CorrespondenceMap1To2, typename CorrespondenceMap2To1>
  212.     bool operator()(CorrespondenceMap1To2 f, CorrespondenceMap2To1 g) const
  213.     {
  214.         return false;
  215.     }
  216. };
  217. //==================================================================================
  218. /*bool graphexistance( std::vector<Graph> const& v, Graph item)
  219. {
  220.    return std::find(v.begin(), v.end(), item)!=v.end();
  221. }*/
  222.  
  223. //=========================================================
  224. bool gUe(Graph& g, edge_iter ep, Graph t)
  225. {
  226.    
  227.     vertex_t from = source(*ep, t);
  228.     vertex_t to = target(*ep, t);
  229.  
  230.     Graph::edge_descriptor copied_edge = boost::add_edge(from, to, t[*ep], g).first;
  231.  
  232.     g[source(copied_edge, g)] = t[from];
  233.     g[target(copied_edge, g)] = t[to];
  234.  
  235.     if(graphconnexe(g)&&graphconnexe(t)) {return vf2_subgraph_iso(g,t,my_callback());}
  236.     else {return false;}
  237.            
  238. }
  239. //==========================================
  240. bool verticeexist(Graph& g,int vId, int vlabel)
  241. {
  242.     int cpt=0;
  243.     if (num_edges(g)!=0)
  244.     {
  245.          for (size_t i = 0 ; i < num_vertices(g) ; ++i) //size_t vertice number in the graph
  246.             {
  247.               if((g[i].id==vId)&&(g[i].label==vlabel)){cpt++;}
  248.             }
  249.     }
  250.     return cpt!=0;
  251. }
  252. //========================================
  253.   bool edgeexist(Graph g,int fromid,int toid,int elabel)
  254.   {
  255.     int bn=0;
  256.     if(graphconnexe(g))
  257.     {            
  258.     if(num_edges(g)!=0)
  259.     {          
  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.             edge_t edg = edge(from, to, g);                                    
  266.  
  267.             if (
  268.                 (g[from].id==fromid)
  269.                 &&
  270.                 (g[to].id==toid)
  271.                 &&
  272.                 (g[edg.first].label==elabel)
  273.                 )
  274.             {
  275.                 bn++;
  276.             }            
  277.         }
  278.     }
  279.     }
  280.  
  281.     return(bn!=0);
  282.   }
  283.  
  284. // =========================================
  285.  
  286.   bool vareneighbors(Graph g,int a,int b)
  287.   {
  288.     int bn=0;
  289.     if(graphconnexe(g))
  290.     {        
  291.    
  292.     if(num_edges(g)!=0)
  293.     {          
  294.         edge_pair ep;
  295.         for (ep = edges(g); ep.first != ep.second; ++ep.first) //ep edge number
  296.         {
  297.             vertex_t from = source(*ep.first, g);
  298.             vertex_t to = target(*ep.first, g);                                
  299.  
  300.             if (((g[from].id==a)||(g[to].id==a))&&((g[from].id==b)||(g[to].id==b)))
  301.             {
  302.                 bn++;
  303.             }            
  304.         }
  305.     }
  306.     }
  307.  
  308.     return(bn!=0);
  309.   }  
  310. //==========================================
  311.   bool edgesneighbors(Graph g, edge_iter ep1,edge_iter ep2)
  312. {
  313.    
  314.     vertex_t fromep1 = source(*ep1, g);
  315.     vertex_t toep1 = target(*ep1, g);
  316.     vertex_t fromep2 = source(*ep2, g);
  317.     vertex_t toep2 = target(*ep2, g);
  318.  
  319.     cout<<g[fromep1].id<<"--"<<g[toep1].id<<endl;
  320.     cout<<g[fromep2].id<<"--"<<g[toep2].id<<endl;
  321.     if(graphconnexe(g))
  322.     {return((fromep1==fromep2)||(fromep1==toep2)||(toep1==toep2)||(fromep2==toep1));}
  323.     else {return false;}
  324.            
  325. }
  326. //==========================================
  327. void emptygraphaddedge(Graph& g,int fromId,int toId,int eLabel)
  328. {
  329.     if (num_edges(g)==0)
  330.     {
  331.         boost::add_edge(fromId, toId, EdgeProperties(num_edges(g)+1, eLabel),g);
  332.     }
  333. }
  334.  
  335.  
  336. //==========================================
  337. int main()
  338. {
  339.     clock_t start=std::clock();
  340.     size_t length;
  341.  
  342.     std::vector<Graph> dataG =creategraphs(splitstringtolines(readfromfile2("testgUe.txt", length)));    
  343.        
  344.     typedef std::pair<edge_iter, edge_iter> edge_pair;    
  345.  
  346.        
  347.    
  348.     if (!dataG.empty())
  349.     {                                      
  350.         edge_pair ep;
  351.         edge_iter e1,e2;
  352.         for (ep = edges(dataG[0]); ep.first != ep.second; ++ep.first) //ep edge number
  353.             {
  354.               e1=ep.first;
  355.               e2=ep.second;
  356.             }
  357.        
  358.      
  359.         cout<<"edgesneighbors"<<edgesneighbors(dataG[0],e1,e2)<<endl;
  360.                        
  361.     }
  362.  
  363.  //end and time  
  364.     cout<<"FILE Contains "<<dataG.size()<<" graphs.\nTIME: "<<( std::clock() - start ) / (double) CLOCKS_PER_SEC<<"s"<<endl; // fin du programme.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement