【问题标题】:boost graph library - minimal example of vertex colors and graphviz outputboost graph library - 顶点颜色和graphviz输出的最小示例
【发布时间】:2011-08-11 23:16:52
【问题描述】:

作为 boost 图形库的新手,我发现通常很难梳理出示例的哪些部分与特定示例相关联,哪些部分是通用的。

作为练习,我正在尝试制作一个简单的图形,为顶点分配一个颜色属性,并将结果输出到 graphviz,以便颜色显示为可以渲染的颜色属性。任何帮助,将不胜感激!这是我到目前为止所拥有的(更具体的使用问题在这里的 cmets 中):

#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?
}

【问题讨论】:

  • 哎呀,他们没有出现...不应该使用
  • 为您解决了这个问题(选择代码,按 Ctrl-K 自动执行此操作)

标签: c++ boost boost-graph


【解决方案1】:

试试:

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 要求的颜色名称。

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2016-09-19
    • 2019-09-21
    相关资源
    最近更新 更多