boost graph library - minimal example of vertex colors and graphviz output

时光毁灭记忆、已成空白 提交于 2020-01-21 11:00:05

问题


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:


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.



来源:https://stackoverflow.com/questions/5772589/boost-graph-library-minimal-example-of-vertex-colors-and-graphviz-output

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