Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 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("1000.data"); // 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.  
  101.     Graph testg;
  102.     if (!dataG.empty())
  103.     {
  104.         auto const& gr = dataG.front(); // firslt graph in G_list
  105.  
  106.  
  107.         auto ep = edges(gr).first;  //first edge in gr
  108.  
  109.         vertex_t from = source(*ep, gr);
  110.         vertex_t to   = target(*ep, gr);
  111.         boost::property_map<Graph, int VertexProperties::*>::type idmap    = boost::get(&VertexProperties::id, testg);
  112.         boost::property_map<Graph, int VertexProperties::*>::type labelmap = boost::get(&VertexProperties::label, testg);
  113.         idmap[from]    = gr[from].id;
  114.         labelmap[from] = gr[from].label;
  115.         boost::add_vertex(VertexProperties(idmap[from], labelmap[from]), testg);
  116.         idmap[to]    = gr[to].id;
  117.         labelmap[to] = gr[to].label;
  118.         boost::add_vertex(VertexProperties(idmap[to], labelmap[to]), testg);
  119.  
  120.         boost::add_edge(from, to, gr[*ep], testg);    
  121.     }
  122.    
  123.        typedef std::pair<edge_iter, edge_iter> edge_pair;
  124.      
  125.       //int c = 0; //compteur de dataG
  126.      
  127.      
  128.         //++c;
  129.         std::cout << "Graph  contains " << num_vertices(testg) << " vertices, and " << num_edges(testg) << " edges" << std::endl;
  130.         //Vertex list
  131.         std::cout << "  Vertex list: " << std::endl;
  132.         for (size_t i = 0 ; i < num_vertices(testg) ; ++i) //size_t vertice number in the graph
  133.         {
  134.           std::cout << "   v[" << i << "]   ID: " << testg[i].id << ", Label: " << testg[i].label << std::endl;
  135.         }
  136.         //Edge list
  137.         std::cout << "  Edge list: " << std::endl;
  138.         edge_pair ep;
  139.         for (ep = edges(testg); ep.first != ep.second; ++ep.first) //ep edge number
  140.         {
  141.           vertex_t from = source(*ep.first, testg);
  142.           vertex_t to = target(*ep.first, testg);
  143.           edge_t edg = edge(from, to, testg);
  144.           std::cout << "   e[" << testg[from].id << "," << testg[to].id << "]   ID: " << testg[edg.first].id << " ,  Label: " <<  testg[edg.first].label << std::endl;
  145.         }
  146.         std::cout << std::endl;
  147.  
  148.  
  149.     cout<<"TIME: "<<( std::clock() - start ) / (double) CLOCKS_PER_SEC<<"s"<<endl; // fin du programme.
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement