Boost graphviz custom vertex labels

久未见 提交于 2019-12-07 18:28:27

问题


Currently I have the following code for a project that represents some probability trees and uses custom structs for the vertex and edge types:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct Vertex{
    std::string name;
    size_t times_visited;
    float value;
    bool terminal_node;
    bool root_node;
};

struct Edge{
    float probability;
};

//Some typedefs for simplicity
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Vertex, Edge> directed_graph_t;

typedef boost::graph_traits<directed_graph_t>::vertex_descriptor vertex_t;
typedef boost::graph_traits<directed_graph_t>::edge_descriptor edge_t;

I currently have a simple visual representation of some simple trees using boost graphviz but with no labels. I would like to have the connections between vertices being labeled with the probability found in the Edge struct and the vertices labeled with the associate name found in the Vertex struct. My first attempt at doing this was to use this code:

std::ofstream outf("test.dot");
boost::dynamic_properties dp;
dp.property("name", get(&Vertex::name, test));
dp.property("node_id", get(boost::vertex_index, test));
write_graphviz_dp(outf, test, dp);

But this seems to not do what I want as it doesn't output the name of the vertex. What should I be changing here?


回答1:


Looks like this was just because I didn't understand graphviz properly. The following code worked as expected:

std::ofstream outf("test.dot");
boost::dynamic_properties dp;
dp.property("label", boost::get(&Vertex::name, test));
dp.property("node_id", boost::get(boost::vertex_index, test));
dp.property("label", boost::get(&Edge::probability, test));
write_graphviz_dp(outf, test, dp);


来源:https://stackoverflow.com/questions/8048009/boost-graphviz-custom-vertex-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!