使用捆绑属性增强BGL read_graphviz

时间:2014-08-08 04:34:23

标签: c++ boost graph graphviz

我有一个如此定义的图表:

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;

我使用write_graphviz将边缘+分数写入文件(我正在分别编写Trans,所以我只想在读取点文件时读取分数)。我是这样写的:

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));

我的点文件类似于:

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"];
}

所以,我基本上想要加载G,以便在{-1}}和G[edge].score = -3之间存在0-1等的边缘。

我现在拥有的内容给了我大量的编译错误,我正在认真考虑只制作一个纯文本版本的图形,而不是尝试读取点文件,然后从那里重新创建图形...... / p>

这就是我所拥有的:

G[edge].trans = //some default value or whatever

我很确定只有分数是个问题,但我已经尝试过获取EdgeInfoProperty地图,只是我尝试过的所有内容都给出了错误...

1 个答案:

答案 0 :(得分:1)

<强>问题

您的代码中存在多个错误:

boost::get(boost::vertex_name,G)假设您的顶点中有一个名为 name 的属性,但情况并非如此。此外,read_graphviz()将节点标识符存储到名为node_id的属性映射中。但在你的情况下,你的顶点没有属性。它无法运作。

正在运行的示例

#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;
}