Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <ctime>
  7. using namespace std;
  8. using namespace boost;
  9.  
  10. /*********************************************/
  11. // vertex
  12. struct VertexProperties {
  13.     int id;
  14.     int label;
  15.     VertexProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
  16. };
  17.  
  18. // edge
  19. struct EdgeProperties {
  20.     unsigned id;
  21.     unsigned label;
  22.     EdgeProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
  23. };
  24.  
  25. // Graph
  26. struct GraphProperties {
  27.     unsigned id;
  28.     unsigned label;
  29.     GraphProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
  30. };
  31.  
  32. // adjency list
  33. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
  34.         VertexProperties,
  35.         EdgeProperties,
  36.         GraphProperties> Graph;
  37.  
  38. // descriptors
  39.  
  40. typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
  41. typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
  42. // iterators
  43. typedef graph_traits<Graph>::vertex_iterator vertex_iter;
  44. typedef graph_traits<Graph>::edge_iterator edge_iter;
  45.  
  46. int main()
  47. {
  48.     clock_t start=std::clock();
  49.     std::vector<Graph> dataG;
  50.  
  51.     std::ifstream file_reader("5.txt"); // flux d'entrée pour opérer sur les fichiers.
  52.     //ifstream * file_reader= new ifstream("60.txt" ); //flux d'entrée pour opérer sur les fichiers.
  53.  
  54.     std::string line;
  55.     while (std::getline(file_reader, line)) { // getline reads characters from an input stream and places them into a string
  56.  
  57.         char lineType;
  58.  
  59.         std::stringstream ss(line);  // use a string buffer that contains a sequence of characters.
  60.         if (ss >> lineType) switch (lineType) {
  61.             case 't':
  62.                 {
  63.                     char diez;
  64.                     unsigned gid;
  65.                     if (ss >> diez >> gid) {
  66.                         dataG.emplace_back(GraphProperties (gid, gid));
  67.                     }
  68.                     else throw std::runtime_error("Error parsing '" + line + "'");
  69.                 }
  70.                 break;
  71.             case 'v':
  72.                 {
  73.                     assert(!dataG.empty());
  74.  
  75.                     int vId, vLabel;
  76.                     if (ss >> vId >> vLabel) {
  77.                         boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
  78.                     }
  79.                     else throw std::runtime_error("Error parsing '" + line + "'");
  80.                 }
  81.                 break;
  82.             case 'e':
  83.                 {
  84.                     assert(!dataG.empty());
  85.  
  86.                     int fromId, toId, vLabel;
  87.                     if (ss >> fromId >> toId >> vLabel) {
  88.                         // Note that the EdgeProperty.id doesn't make sense with your input data
  89.                         // as it only contains [vertexFrom vertexTo edgeData]
  90.                         boost::add_edge(fromId, toId, EdgeProperties(vLabel, vLabel), dataG.back());
  91.                     }
  92.                     else throw std::runtime_error("Error parsing '" + line + "'");
  93.                 }
  94.                 break;
  95.         } else
  96.         {
  97.             // ignoring empty line
  98.         }
  99.     }
  100.     typedef std::pair<edge_iter, edge_iter> edge_pair;
  101.     Graph testg;
  102.     if (!dataG.empty())
  103.     {
  104.         auto const& gr = dataG.front(); // no copy
  105.         //++c;
  106.         std::cout << "Graph gr  contains " << num_vertices(gr) << " vertices, and " << num_edges(gr) << " edges" << std::endl;
  107.         //Vertex list
  108.         std::cout << "  Vertex list: " << std::endl;
  109.         for (size_t i = 0 ; i < num_vertices(gr) ; ++i) //size_t vertice number in the graph
  110.         {
  111.           std::cout << "   v[" << i << "]   ID: " << gr[i].id << ", Label: " << gr[i].label << std::endl;
  112.         }
  113.         //Edge list
  114.         std::cout << "  Edge list: " << std::endl;
  115.         edge_pair ep;
  116.         for (ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number
  117.         {
  118.           vertex_t from = source(*ep.first, gr);
  119.           vertex_t to = target(*ep.first, gr);
  120.           edge_t edg = edge(from, to, gr);
  121.           std::cout << "   e[" << gr[from].id << "," << gr[to].id << "]   ID: " << gr[edg.first].id << " ,  Label: " <<  gr[edg.first].label << std::endl;
  122.         }
  123.         std::cout << std::endl;
  124.  
  125.  
  126.  
  127.        for (edge_pair ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number
  128.         {
  129.             //auto ep = edges(gr).first;  // ep edge number
  130.         xxxx
  131.             vertex_t from = source(*ep.first, gr);
  132.             vertex_t to   = target(*ep.first, gr);
  133.  
  134.             boost::add_vertex(gr[from], testg);
  135.             boost::add_vertex(gr[to], testg);
  136.             boost::add_edge(from, to, gr[*ep.first], testg);
  137.         xxxx
  138.        
  139.         }
  140.                                          
  141.     }
  142.    
  143.        
  144.      
  145.      
  146.      
  147.      
  148.         //++c;
  149.         std::cout << "Graph testg  contains " << num_vertices(testg) << " vertices, and " << num_edges(testg) << " edges" << std::endl;
  150.         //Vertex list
  151.         std::cout << "  Vertex list: " << std::endl;
  152.         for (size_t i = 0 ; i < num_vertices(testg) ; ++i) //size_t vertice number in the graph
  153.         {
  154.           std::cout << "   v[" << i << "]   ID: " << testg[i].id << ", Label: " << testg[i].label << std::endl;
  155.         }
  156.         //Edge list
  157.         std::cout << "  Edge list: " << std::endl;
  158.         edge_pair ep;
  159.         for (ep = edges(testg); ep.first != ep.second; ++ep.first) //ep edge number
  160.         {
  161.           vertex_t from = source(*ep.first, testg);
  162.           vertex_t to = target(*ep.first, testg);
  163.           edge_t edg = edge(from, to, testg);
  164.           std::cout << "   e[" << testg[from].id << "," << testg[to].id << "]   ID: " << testg[edg.first].id << " ,  Label: " <<  testg[edg.first].label << std::endl;
  165.         }
  166.         std::cout << std::endl;
  167.      
  168.  
  169.     cout<<"TIME: "<<( std::clock() - start ) / (double) CLOCKS_PER_SEC<<"s"<<endl; // fin du programme.
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement