Boost图库-崩溃topological_sort与remove_vertex
Boost Graph Library - crash in topological_sort with remove_vertex
下面这段代码在topological_sort中崩溃,似乎是损坏了。有人能在代码中发现可能导致这种情况的东西吗?还是触发了BGL的bug ?
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
using namespace boost;
typedef boost::adjacency_list <listS, listS, bidirectionalS,
boost::property<boost::vertex_index_t, int > > Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef boost::graph_traits<Graph>::edge_descriptor Edge;
int main(int,char*[])
{
Graph graph;
std::list<Vertex> vertex_list;
typedef property_map<Graph, vertex_index_t>::type index_map_t;
index_map_t index_map = get(vertex_index, graph);
Vertex a, b, c, d, e, f, g;
graph_traits<Graph>::vertices_size_type current_index = 0;
a = add_vertex(graph);
index_map[a] = current_index++;
b = add_vertex(graph);
index_map[b] = current_index++;
c = add_vertex(graph);
index_map[c] = current_index++;
d = add_vertex(graph);
index_map[d] = current_index++;
e = add_vertex(graph);
index_map[e] = current_index++;
clear_vertex(a, graph);
remove_vertex(a, graph);
f = add_vertex(graph);
index_map[f] = current_index++;
g = add_vertex(graph);
index_map[g] = current_index++;
topological_sort(graph, std::front_inserter(vertex_list));
}
从崩溃回溯如下:
#0 0x00007ffff7538425 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff753bb8b in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff757639e in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#3 0x00007ffff7580b96 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#4 0x0000000000407e58 in boost::checked_array_delete<boost::default_color_type> (x=0x610290) at /usr/include/boost/checked_delete.hpp:41
#5 0x0000000000407d54 in boost::checked_array_deleter<boost::default_color_type>::operator() (this=0x6102c8, x=0x610290)
at /usr/include/boost/checked_delete.hpp:63
#6 0x0000000000407f45 in boost::detail::sp_counted_impl_pd<boost::default_color_type*, boost::checked_array_deleter<boost::default_color_type> >::dispose (
this=0x6102b0)
at /usr/include/boost/smart_ptr/detail/sp_counted_impl.hpp:148
#7 0x0000000000401ab6 in boost::detail::sp_counted_base::release (
this=0x6102b0)
at /usr/include/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp:145
#8 0x0000000000401b2f in boost::detail::shared_count::~shared_count (
this=0x7fffffffe418, __in_chrg=<optimized out>)
at /usr/include/boost/smart_ptr/detail/shared_count.hpp:305
#9 0x0000000000403786 in boost::shared_array<boost::default_color_type>::~shared_array (this=0x7fffffffe410, __in_chrg=<optimized out>)
at /usr/include/boost/smart_ptr/shared_array.hpp:46
#10 0x00000000004037a0 in boost::shared_array_property_map<boost::default_color_type, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, boost::property<boost::vertex_index_t, int, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, int, int const&, boost::vertex_index_t> >::~shared_array_property_map (
this=0x7fffffffe410, __in_chrg=<optimized out>)
at /usr/include/boost/property_map/shared_array_property_map.hpp:19
#11 0x0000000000403997 in boost::depth_first_search<boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, boost::property<boost::vertex_index_t, int, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, boost::topo_sort_visitor<std::front_insert_iterator<std::list<void*, std::allocator<void*> > > >, boost::graph_visitor_t, boost::bgl_named_params<int, boost::buffer_param_t, boost::no_property> > (g=..., params=...)
at /usr/include/boost/graph/depth_first_search.hpp:298
#12 0x0000000000402e6f in boost::topological_sort<boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, boost::property<boost::vertex_index_t, int, boost::no_property>, boost::no_property, boost::no_property, boost::listS---Type <return> to continue, or q <return> to quit---
>, std::front_insert_iterator<std::list<void*, std::allocator<void*> > >, int, boost::buffer_param_t, boost::no_property> (g=..., result=..., params=...)
at /usr/include/boost/graph/topological_sort.hpp:65
#13 0x00000000004024d6 in boost::topological_sort<boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, boost::property<boost::vertex_index_t, int, boost::no_property>, boost::no_property, boost::no_property, boost::listS>, std::front_insert_iterator<std::list<void*, std::allocator<void*> > > > (
g=..., result=...) at /usr/include/boost/graph/topological_sort.hpp:71
#14 0x00000000004015c5 in main () at bgl_try.cc:40
我认为是index_map导致了您的问题。根据这个,它应该将每个顶点映射到[0,num_vertices(g))范围内的整数(从0到num_vertices - 1),而你的是从1到num_vertices。
这样做很好(忽略关于未使用变量的警告):
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/iteration_macros.hpp>
using namespace boost;
typedef boost::adjacency_list <listS, listS, bidirectionalS,
boost::property<boost::vertex_index_t, int > > Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef boost::graph_traits<Graph>::edge_descriptor Edge;
int main(int,char*[])
{
Graph graph;
std::list<Vertex> vertex_list;
typedef property_map<Graph, vertex_index_t>::type index_map_t;
index_map_t index_map = get(vertex_index, graph);
Vertex a, b, c, d, e, f, g;
a = add_vertex(graph);
b = add_vertex(graph);
c = add_vertex(graph);
d = add_vertex(graph);
e = add_vertex(graph);
clear_vertex(a, graph);
remove_vertex(a, graph);
f = add_vertex(graph);
g = add_vertex(graph);
graph_traits<Graph>::vertices_size_type current_index = 0;
BGL_FORALL_VERTICES(v,graph,Graph)
index_map[v]=current_index++;
topological_sort(graph, std::front_inserter(vertex_list));
}
相关文章:
- 为什么 std::unique 不调用 std::sort?
- 使用函数"remove"删除重复元素
- 对字符串进行排序时,在c++中处理sort()
- 链表的泛型函数remove()与成员函数remove)
- std::sort()函数无法对向量的一部分进行排序
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- Qt:remove() 和 rmdir() 有什么区别
- C++中"std::sort"比较器的不同类型
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- 按字母顺序对字符串中的字母进行排序,而无需使用内置的 sort()
- c++无值sort()的问题是什么?
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- remove(str.begin(), str.end(), );无法正常工作(我正在使用视觉工作室 2012)
- 使用 std::sort 对向量进行稳定排序
- 如何使用 QSortFilterProxyModel::sort 对 Qlist 中的数据进行排序
- std::sort 如何处理重复的数字?
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- C++ <algorithm> 使用对象作为比较定义的 sort()
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- unqualified sort() -- 为什么它在 std::vector 上使用而不是在 std::array 上