Reputation: 1608
I have a graph defined like so:
struct EdgeInfoProperty{
int score;
//is the trans from v to u, where u<v
Trans trans;
};
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeInfoProperty > Graph;
typedef Graph::edge_descriptor Edge;
typedef Graph::vertex_descriptor Vertex;
And I used write_graphviz
to write the edges + scores to a file (I'm writing the Trans separately, so I'd only want to read the score when reading the dot file). I wrote it like so:
auto w_map = boost::get(&EdgeInfoProperty::score, G); // <=== THIS IS THE TRICK!!!
boost::write_graphviz(myfile, G, boost::default_writer(), make_edge_writer(w_map));
My dot file looks something like:
graph G {
0;
1;
2;
3;
4;
5;
6;
7;
8;
9;
10;
11;
12;
0--1 [label="-3"];
0--5 [label="-2"];
2--3 [label="-8"];
3--8 [label="-4"];
4--5 [label="-1"];
4--6 [label="-6"];
4--7 [label="-5"];
4--8 [label="-10"];
8--9 [label="-9"];
}
So, I basically want to load G so that there's an edge between 0--1 etc. with G[edge].score = -3
etc. and G[edge].trans = //some default value or whatever
.
What I have right now gives me tons of compilation errors, and I'm seriously considering just making a text-only copy of my graph instead of trying to read the dot file and then recreating the graph from there...
Here's what I have:
std::string gn = loc + "MST.dot";
Graph G(0);
boost::dynamic_properties dp;
boost::property_map<Graph, boost::vertex_name_t>::type name = boost::get(boost::vertex_name, G);
dp.property("node_id",name);
auto score = boost::get(&EdgeInfoProperty::score, G);
dp.property("score",score);
std::filebuf fb;
fb.open (gn, std::ios::in);
std::istream isg(&fb);
bool status = boost::read_graphviz(isg,G,dp,"node_id");
I'm pretty sure having only the score is a problem, but I've tried getting the EdgeInfoProperty map, it's just that everything I've tried gave an error...
Upvotes: 3
Views: 750
Reputation: 1213
Issues
There are several mistakes in your code:
boost::get(boost::vertex_name,G)
assumes there is a property named name in your vertex, which is not the case. Moreover, read_graphviz()
stores the node identifiers into a property map called node_id
. But in your case, you have no property for your vertex. It cannot work.
A running example
#include <iostream>
#include <sstream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
using namespace std;
struct Trans {
int t;
};
struct EdgeInfoProperty {
int score;
//is the trans from v to u, where u<v
Trans trans;
};
struct NodeInfoProperty {
int index;
};
/* you cannot read a graph without node property. By default, read_graphviz() assume
* nodes have a "node_id" property map...
*/
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, NodeInfoProperty, EdgeInfoProperty > Graph;
template<class Index> class noeud_writer {
public:
noeud_writer(Index id) : idm(id){}
template<class Noeud> void operator()(std::ostream & out, const Noeud & n) {
out << "[index=" << idm[n] << "]"; }
private:
Index idm;
};
template< class IdMap> inline noeud_writer<IdMap> make_noeud_writer(IdMap idm) {
return noeud_writer<IdMap>(idm); }
template<class Score> class edge_writer {
public:
edge_writer(Score s) : score(s){}
template<class Noeud> void operator()(std::ostream & out, const Noeud & n) {
out << "[score=" << score[n] << "]"; }
private:
Score score;
};
template< class Score> inline edge_writer<Score> make_edge_writer(Score score) {
return edge_writer<Score>(score); }
int main(int argc, char * argv[])
{
Graph G(0);
boost::dynamic_properties dp;
dp.property("index", boost::get(&NodeInfoProperty::index,G));
auto score = boost::get(&EdgeInfoProperty::score, G);
dp.property("score", score);
std::istringstream isg("graph G {0[index=0];1[index=1];0--1[score=-3];}");
bool status = boost::read_graphviz(isg, G, dp,"index");
write_graphviz(std::cout, G, make_noeud_writer(boost::get(&NodeInfoProperty::index, G)),
make_edge_writer(boost::get(&EdgeInfoProperty::score, G)));
return 0;
}
Upvotes: 2