boost graph library - minimal example of vertex co

2019-02-21 20:29发布

Being new to the boost graph library, I find it's often difficult to tease out what pieces of the examples are tied to the particular example and which parts are universal to usage.

As an exercise, I'm trying to make a simple graph, assign a color property to the vertices, and output the result to graphviz so colors appear as color attributes that get rendered. Any help would be appreciated! Here's what I have so far (more specific usage questions are in the comments here):

#include "fstream"
#include "boost/graph/graphviz.hpp"
#include "boost/graph/adjacency_list.hpp"

struct vertex_info { 
    int color; 
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, vertex_info> Graph;
typedef std::pair<int, int> Edge;

int main(void) {
  Graph g;
  add_edge(0, 1, g);
  add_edge(1, 2, g);

  // replace this with some traversing and assigning of colors to the 3 vertices  ...
  // should I use bundled properties for this?
  // it's unclear how I would get write_graphviz to recognize a bundled property as the color attribute
  g[0].color = 1; 

  std::ofstream outf("min.gv");
  write_graphviz(outf, g); // how can I make write_graphviz output the vertex colors?
}

1条回答
够拽才男人
2楼-- · 2019-02-21 20:52

Try:

boost::dynamic_properties dp;
dp.property("color", get(&vertex_info::color, g));
dp.property("node_id", get(boost::vertex_index, g));
write_graphviz_dp(outf, g, dp);

instead of your write_graphviz call. See http://www.boost.org/doc/libs/1_47_0/libs/graph/example/graphviz.cpp for an example of this. Note that the code I posted will write out the colors as integers, not color names like Dot requires.

查看更多
登录 后发表回答