手动着色助推图 [英] Manually colouring of boost's graphs
问题描述
我正在努力尝试使用boost来为图的顶点着色。我写了下面的代码,但我无法弄清楚为什么生成的文件没有任何颜色。
I'm struggling trying to manually colouring graph's vertices using boost. I wrote the code below but I can't figure it out why the generated file does not have any colour.
int main(int,char*[]) {
typedef property<edge_name_t, string> EdgeProperties;
typedef property<vertex_name_t, string, property<vertex_color_t, default_color_type>> VertexProperties;
typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;
Graph g;
property_map<Graph, vertex_name_t>::type vertex_label = get(vertex_name, g);
property_map<Graph, vertex_color_t>::type color_map = get(vertex_color, g);
property_map<Graph, edge_name_t>::type edge_label = get(edge_name, g);
Vertex v1 = add_vertex(g);
vertex_label[v1] = "v1";
put(color_map, v1, boost::red_color);
std::ofstream outf("example.gv");
write_graphviz(outf, g,
make_label_writer(vertex_label),
make_label_writer(edge_label)
);
return 0;
}
推荐答案
顶点着色实际上是一种算法详情。据我所知,没有自动条款将其转换为graphviz。
Vertex colourings are really an algorithmic detail. There is no "automatic" provision to translate it to graphviz, as far as I know.
您可以添加自定义属性。
You can add custom attributes though.
我通常会改变它以使用动态属性:
I'd usually change it around to use dynamic properties:
std::ofstream outf("example.gv");
dynamic_properties dp;
dp.property("node_id", get(vertex_index, g));
dp.property("label", vertex_label);
dp.property("label", edge_label);
write_graphviz_dp(outf, g, dp);
现在就像向动态集添加一个新的顶点属性一样简单:
Now it's as simple as adding a new vertex attribute to the dynamic set:
dp.property("color", make_transform_value_property_map(&color_to_string, color_map));
唯一的问题是您需要提供 default_color_type
到文本。这是颜色图通常不用于表示目的的另一个征兆。这是一个完整的工作示例:
The only complication is that you need to supply the conversion from default_color_type
to text. This is another symptom of the fact that the color maps aren't usually used for representational purposes. Here's a fully working sample:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>
using namespace boost;
inline char const* color_to_string(default_color_type c) {
switch(c) {
case default_color_type::red_color: return "red";
case default_color_type::green_color: return "green";
case default_color_type::gray_color: return "gray";
case default_color_type::white_color: return "white";
case default_color_type::black_color: return "black";
}
return ""; // not known
}
int main() {
typedef property<edge_name_t, std::string> EdgeProperties;
typedef property<vertex_name_t, std::string, property<vertex_color_t, default_color_type>> VertexProperties;
typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
Graph g;
property_map<Graph, vertex_name_t>::type vertex_label = get(vertex_name, g);
property_map<Graph, vertex_color_t>::type color_map = get(vertex_color, g);
property_map<Graph, edge_name_t>::type edge_label = get(edge_name, g);
Vertex v1 = add_vertex(g);
vertex_label[v1] = "v1";
put(color_map, v1, boost::red_color);
dynamic_properties dp;
dp.property("node_id", get(vertex_index, g));
dp.property("label", vertex_label);
dp.property("label", edge_label);
dp.property("color", make_transform_value_property_map(&color_to_string, color_map));
write_graphviz_dp(std::cout, g, dp);
}
打印:
Prints:
digraph G {
0 [color=red, label=v1];
}
这篇关于手动着色助推图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!