【问题标题】:Segmentation fault when using prim_minimum_spanning_tree in boost在 boost 中使用 prim_minimum_spanning_tree 时出现分段错误
【发布时间】:2021-04-12 12:44:31
【问题描述】:

我想在文件中的图形上运行 prim,但是对于某些输入,prim_minimum_spanning_tree 给了我一个分段错误。 当我使用 valgrind 运行它时,不会发生段错误,但 valgrind 指示存在错误(“无效读取大小为 8”),但它只在某些图表上显示它们。

我对使用 boost 很陌生,我怀疑我初始化图表错误。

std::pair<int,int> *edges = new std::pair<int,int>[num_edges];
double *weights = new double[num_edges];

// read edges and weights into arrays
readin(argc, argv, edges, weights);

typedef adjacency_list<vecS, vecS, undirectedS, no_property, property<edge_weight_t, mat_t>> Graph;
Graph G(num_nodes);

for(int i = 0; i < num_edges; i++){
// check input
  assert(edges[i].first >= 0 && "zero first");
  assert(edges[i].first < num_nodes && "numvertices first");
  assert(edges[i].second >= 0 && "zero second");
  assert(edges[i].second < num_nodes && "numvertices second");
// add edge
  auto edge_iterator = add_edge( edges[i].first,edges[i].second, weights[i], G);
}
//create parent vector to store solution
    std::vector<graph_traits<Graph>::vertex_descriptor> *parents = 
         new std::vector<graph_traits<Graph>::vertex_descriptor>(num_vertices(G));

//run prim
    prim_minimum_spanning_tree(G, &(parents->at(0)));

delete parents;
delete[] edges;
delete[] weights;

我尝试用 gdb 调试它,但我找不到错误...


更新: 感谢您的帮助!

我终于找到了错误:它与 readin 函数无关。 问题是当图形包含自循环时,提升中的 prim 不起作用...... (或至少只有在那个错误发生时)

来自 boost 文档: " Boost.Graph 中实现的算法不会在具有平行边的图上产生正确的结果。"

我不认为segfault被认为是“不正确的结果”......

【问题讨论】:

  • 修复Valgrind错误,您还可能会修复分段错误。 span>

标签: c++ boost graph segmentation-fault minimum-spanning-tree


【解决方案1】:

就像评论者所说,修复你的 valgrind 错误。很可能您在 readin 函数中做的事情越界。

您操作的原始指针太多。这不是C,为什么要假装它是?

这是我的看法,没有使用手动分配,也没有可能出现错误的界限。请注意,我确保输入是无符号的,因此我们不会得到混合符号整数比较。这也使得一半的输入检查变得多余:

//assert(s >= 0            && "zero source");
assert(s < num_vertices(G) && "numvertices source");
//assert(t >= 0            && "zero target");
assert(t < num_vertices(G) && "numvertices target");

Live On Wandbox

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
#include <boost/graph/graph_utility.hpp>
#include <iostream>
#include <fstream>

using mat_t  = double;
struct input { unsigned source, target; mat_t weight; };

static auto readin(std::string filename) {
    std::vector<input> lines;
    input line;

    std::ifstream ifs(filename);
    while (ifs >> line.source >> line.target >> line.weight) {
        lines.push_back(line);
    }

    return lines;
}

int main() {
    // read edges and weights into arrays
    auto input = readin("input.txt");

    using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, 
          boost::property<boost::edge_weight_t, mat_t>>;

    unsigned highest_vertex = 0;
    for (auto [s,t,w]: input)
        highest_vertex = std::max({highest_vertex, s, t});

    Graph G(highest_vertex + 1);

    for (auto [s,t,w]: input) {
        //assert(s >= 0            && "zero source");
        assert(s < num_vertices(G) && "numvertices source");
        //assert(t >= 0            && "zero target");
        assert(t < num_vertices(G) && "numvertices target");

        // add edge
        add_edge(s, t, w, G);
    }

    //create parent vector to store solution
    std::vector<Graph::vertex_descriptor> parents(num_vertices(G));

    //run prim
    prim_minimum_spanning_tree(G, parents.data());

    print_graph(G);
}

documentation example的基础上加上input.txt

0 2 0.1
1 3 0.1
1 4 0.2
2 1 0.7
2 3 0.3
3 4 0.1
4 0 0.1

打印:

0 <--> 2 4
1 <--> 3 4 2
2 <--> 0 1 3
3 <--> 1 2 4
4 <--> 1 3 0

-fsanitize=undefined,address 和 valgrind 下运行完全干净。也没有内存泄漏:

==9025== Memcheck, a memory error detector
==9025== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9025== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==9025== Command: ./sotest
==9025== 
0 <--> 2 4 
1 <--> 3 4 2 
2 <--> 0 1 3 
3 <--> 1 2 4 
4 <--> 1 3 0 
==9025== 
==9025== HEAP SUMMARY:
==9025==     in use at exit: 0 bytes in 0 blocks
==9025==   total heap usage: 46 allocs, 46 frees, 84,314 bytes allocated
==9025== 
==9025== All heap blocks were freed -- no leaks are possible
==9025== 
==9025== For counts of detected and suppressed errors, rerun with: -v
==9025== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-09-03
  • 2021-06-13
  • 2013-04-03
  • 2016-09-15
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多