C++ BGL-BFS/DFS访问者,访问顶点颜色
在BGL中,我不太清楚如何访问图形中顶点的固有颜色(白色表示未触及,灰色表示已访问,黑色表示已完成),因为在bfs/dfs搜索期间访问了这些顶点C++ BGL-BFS/DFS访问者,访问顶点颜色,c++,c++11,boost,graph-traversal,boost-property-map,C++,C++11,Boost,Graph Traversal,Boost Property Map,在BGL中,我不太清楚如何访问图形中顶点的固有颜色(白色表示未触及,灰色表示已访问,黑色表示已完成),因为在bfs/dfs搜索期间访问了这些顶点 有人能举例说明如何从dfs/bfs访问者中访问顶点的颜色吗?例如,在编写自定义的检查_edge?时,您忘了包含SSCCE,因此我将以散文形式给出答案: 应该使用顶点颜色贴图 这是一张财产地图。对于内部属性映射,可以使用get访问属性映射: property_map<boost::vertex_color_t, Graph>::const_
有人能举例说明如何从dfs/bfs访问者中访问顶点的颜色吗?例如,在编写自定义的
检查_edge
?时,您忘了包含SSCCE,因此我将以散文形式给出答案:
应该使用顶点颜色贴图
这是一张财产地图。对于内部属性映射,可以使用get访问属性映射:
property_map<boost::vertex_color_t, Graph>::const_type pmap =
boost::get(boost::vertex_color, g);
但是,大多数情况下,彩色地图都是外部的,因此您可以让访问者访问它:
直接访问外部属性映射
在本例中,我选择将colormap本身作为访问者的属性。我没有选择最有效的表示(地图),但它
- 允许在没有显式初始化的情况下使用
- 更通用,因为它适用于非整数顶点描述符(例如,当使用
而不是列表
时)向量
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_utility.hpp>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS>;
struct my_vis : default_dfs_visitor {
using colormap = std::map<Graph::vertex_descriptor, default_color_type>;
colormap vertex_coloring;
template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, Graph const& g) {
default_color_type color = vertex_coloring[v];
default_dfs_visitor::discover_vertex(v,g);
}
};
int main() {
Graph const g = make();
my_vis vis;
depth_first_search(g, vis, make_assoc_property_map(vis.vertex_coloring));
for(auto& vc : vis.vertex_coloring)
std::cout << "vertex " << vc.first << " color " << vc.second << "\n";
print_graph(g);
}
using Graph = adjacency_list<vecS, vecS, directedS, property<vertex_color_t, default_color_type> >;
struct my_vis : default_dfs_visitor {
using colormap = property_map<Graph, vertex_color_t>::type;
colormap vertex_coloring;
template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, Graph const& g) {
default_color_type color = vertex_coloring[v];
(void) color; // suppress unused warning
default_dfs_visitor::discover_vertex(v,g);
}
};
int main() {
Graph g = make();
my_vis::colormap map = get(vertex_color, g);
depth_first_search(g, my_vis{}, map);
for(auto u : make_iterator_range(vertices(g)))
std::cout << "vertex " << u << " color " << get(map, u) << "\n";
print_graph(g);
}
使用内部属性
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_utility.hpp>
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS>;
struct my_vis : default_dfs_visitor {
using colormap = std::map<Graph::vertex_descriptor, default_color_type>;
colormap vertex_coloring;
template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, Graph const& g) {
default_color_type color = vertex_coloring[v];
default_dfs_visitor::discover_vertex(v,g);
}
};
int main() {
Graph const g = make();
my_vis vis;
depth_first_search(g, vis, make_assoc_property_map(vis.vertex_coloring));
for(auto& vc : vis.vertex_coloring)
std::cout << "vertex " << vc.first << " color " << vc.second << "\n";
print_graph(g);
}
using Graph = adjacency_list<vecS, vecS, directedS, property<vertex_color_t, default_color_type> >;
struct my_vis : default_dfs_visitor {
using colormap = property_map<Graph, vertex_color_t>::type;
colormap vertex_coloring;
template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, Graph const& g) {
default_color_type color = vertex_coloring[v];
(void) color; // suppress unused warning
default_dfs_visitor::discover_vertex(v,g);
}
};
int main() {
Graph g = make();
my_vis::colormap map = get(vertex_color, g);
depth_first_search(g, my_vis{}, map);
for(auto u : make_iterator_range(vertices(g)))
std::cout << "vertex " << u << " color " << get(map, u) << "\n";
print_graph(g);
}
共享代码
#包括
图make(){
图g;
添加_顶点(g);
添加_顶点(g);
添加_顶点(g);
添加_顶点(g);
添加_顶点(g);
添加_边(0,1,g);
添加_边(0,2,g);
添加_边(1,0,g);
添加_边(2,4,g);
添加_边(4,3,g);
添加_边(3,1,g);
返回g;
}
vertex 0 color 4
vertex 1 color 4
vertex 2 color 4
vertex 3 color 4
vertex 4 color 4
0 --> 1 2
1 --> 0
2 --> 4
3 --> 1
4 --> 3
#include <boost/graph/graph_utility.hpp>
Graph make() {
Graph g;
add_vertex(g);
add_vertex(g);
add_vertex(g);
add_vertex(g);
add_vertex(g);
add_edge(0,1,g);
add_edge(0,2,g);
add_edge(1,0,g);
add_edge(2,4,g);
add_edge(4,3,g);
add_edge(3,1,g);
return g;
}