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