Search code examples
c++boostgraphvizboost-graph

Boost graphviz custom vertex labels


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?


Solution

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