在breadth_first_search期间设置顶点的颜色
Setting the color of a vertex during breadth_first_search
我想在BGL图上做区域生长。区域增长的思想是访问顶点,从指定的根顶点开始,收集并返回一个子图或一个顶点列表,这些顶点与它们的父顶点相比通过了一些标准函数。例如,假设我们有一个简单的图形,看起来像这样:
A-B-C-D
,边权为:
AB = 4,BC = 10,CD = 3
现在我们想从a开始生长一个区域,我们想做以下操作:
- 发现A并添加到连接区域
- 发现B,并决定B是否与a"足够相似"。在这个例子中,假设标准是边权值的阈值:如果边权值> 5,那么我们不应该继续遍行到B。所以这里,
AB = 4
,我们应该增长到B,但由于BC=10
,我们应该永远不会到达C。- 如果是,将B添加到连接区域,并继续发现C并检查C是否与B足够相似,等等。
- 如果没有,停止并返回当前连接的区域
我可以在来访者的tree_edge
函数中查看这个准则函数。如果A和B太不相似,我试图通过将传递给tree_edge
的边缘的目标顶点设置为黑色来"阻止"BFS继续(将B添加到队列中,然后稍后处理它等)。然而,这似乎并没有阻止遍历:
#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/breadth_first_search.hpp>
using EdgeWeightProperty = boost::property<boost::edge_weight_t, float>;
using ColorPropertyType = boost::property<boost::vertex_color_t, boost::default_color_type>;
using GraphType = boost::adjacency_list<boost::setS, // out edge container
boost::vecS, // vertex container
boost::undirectedS, // directed or undirected
ColorPropertyType, // vertex properites
EdgeWeightProperty> // edge properties
;
template <typename TGraph>
void printColors(const TGraph& g)
{
const auto& colorMapGraph = get(boost::vertex_color_t(), g);
std::cout << "colors: ";
for(unsigned int i = 0; i < num_vertices(g); ++i) {
std::cout << get(colorMapGraph, vertex(i, g)) << " ";
}
std::cout << std::endl;
}
class BreadthFirstSearchVisitor : public boost::default_bfs_visitor
{
public:
// We must provide a mutable version of the graph to the visitor since we want to change properties
BreadthFirstSearchVisitor(GraphType& graph) : mGraph(graph) {}
template < typename TEdge, typename TGraph>
void tree_edge(TEdge e, const TGraph& g) const
{
std::cout << std::endl << "tree_edge: " << e << std::endl;
printColors(g);
const auto& colors = get(boost::vertex_color_t(), mGraph); // Though this is const&, you can still call put()
const auto& edgeWeights = get(boost::edge_weight_t(), mGraph);
boost::graph_traits<GraphType>::vertex_descriptor targetVertex = boost::target(e, g);
std::cout << "targetVertex: " << targetVertex << std::endl;
float edgeWeight = get(edgeWeights, e);
std::cout << "edgeWeight: " << edgeWeight << std::endl;
if(edgeWeight > 5.f) {
std::cout << "Next vertex does not belong to the region!" << std::endl;
put(colors, vertex(targetVertex, mGraph), boost::color_traits<GraphType>::black());
printColors(g);
}
}
// A very strange pattern, but this is (officially) recommended here: http://stackoverflow.com/a/2608616/284529
GraphType& mGraph;
};
int main(int,char*[])
{
// Create a graph object
GraphType g(4);
EdgeWeightProperty e0 = 4.f;
add_edge(0, 1, e0, g);
EdgeWeightProperty e1 = 10.f;
add_edge(1, 2, e1, g);
EdgeWeightProperty e2 = 3.f;
add_edge(2, 3, e2, g);
BreadthFirstSearchVisitor breadthFirstSearchVisitor(g);
unsigned int startVertex = 0;
// named argument signature
breadth_first_search(g, vertex(startVertex, g), visitor(breadthFirstSearchVisitor).color_map(get(boost::vertex_color_t(), g)));
return 0;
}
输出为:
tree_edge: (0,1)
colors: 1 0 0 0
targetVertex: 1
edgeWeight: 4
tree_edge: (1,2)
colors: 4 1 0 0
targetVertex: 2
edgeWeight: 10
Next vertex does not belong to the region!
colors: 4 1 4 0
tree_edge: (2,3)
colors: 4 4 1 0
targetVertex: 3
edgeWeight: 3
但是我希望它不会调用边缘为(2,3)
的tree_edge
因为我们将顶点2
标记为黑色
谁能解释一下为什么这不能像我期望的那样工作?
答案似乎只是从处理访问者中的tree_edge
改为处理examine_edge
。我猜目标顶点已经被添加到队列一旦tree_edge
被调用,所以它的颜色不再重要(因为颜色是用来确定是否一个顶点应该被添加到队列)。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 如何循环打印顶点结构
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- Vulkan 中的动态顶点缓冲区格式设置
- 无法在顶点着色器中设置输入的位置
- OpenGL:将Z深度设置为统一深度,而不是冗余存储在顶点内
- OpenGL:设置索引/顶点
- 在breadth_first_search期间设置顶点的颜色
- 设置第二个顶点数组对象会导致奇怪的渲染