boost图形库 - 顶点颜色和graphviz输出的最小示例

时间:2011-04-24 19:05:41

标签: c++ boost boost-graph

作为对图表库的新手,我发现通常很难弄清楚哪些示例与特定示例相关联,以及哪些部分对于使用是通用的。

作为练习,我正在尝试制作一个简单的图形,为顶点指定颜色属性,并将结果输出到graphviz,因此颜色显示为渲染的颜色属性。任何帮助,将不胜感激!这是我到目前为止(更具体的使用问题在这里的评论):

#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 个答案:

答案 0 :(得分:5)

尝试:

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

而不是write_graphviz来电。有关此示例,请参阅http://www.boost.org/doc/libs/1_47_0/libs/graph/example/graphviz.cpp。请注意,我发布的代码会将颜色写为整数,而不是像Dot要求的颜色名称。