c ++ free():路径压缩时无效指针错误(按等级联合)

时间:2016-04-14 07:29:03

标签: c++ union-find invalid-pointer

我查看了其他类似的问题,但无法找出导致此错误的原因。我正在编写一个C ++程序来实现Kruskal的最小生成树算法,使用Union by rank with path compression。 它正确打印MST的边缘,但包括路径压缩部分导致此错误:

*`./kruskal':free()错误:无效指针:0x0000000001650d00 *

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, pair<int,int> > pip;
typedef pair<int,int> pii;
class UnionFind {
    vector <int> parent,rank;
    public:
        UnionFind(int n){
            parent.resize(n);
            rank.resize(n);
            for (int i=0;i<n;i++) { //initialize all parents and ranks
                parent[i]=i;
                rank[i]=0;
            }
        }
        int find (int x){
            int root=x,y;
            while (parent[root]!=root) {
                root=parent[root];
            }
//uncommenting the following loop gives the error
            /*while (parent[x]!=root){ //path compression
                y=parent[x];
                parent[x]=root;
                x=y;
            }*/
            return root;
        }
        void unite (int x,int y) {
            x=find(x);
            y=find(y);
            if (rank[x]>=rank[y]){
                parent[y]=x;
                if (rank[x]==rank[y])
                    rank[x]++;
            }
            else parent[x]=y;
        }
};
int main() {
    int v1,v2,w,n,m;
    cout<<"Enter number of vertices, edges: ";
    cin>>n>>m;
    vector <pip> edges(m);
    UnionFind uf(n);
    cout<<"Enter edges in the form v1,v2,w: ";//w=length of edge
    for (int i=0;i<m;i++) {
        cin>>v1>>v2>>w;
        edges[i]=pip(w,pii(v1,v2));
    }
    sort (edges.begin(),edges.end()); //sort edges in increasing order or lengths
    cout<<"MST: edges \n";
    for (int i=0;i<m;i++){
        if (uf.find(edges[i].second.first)!=uf.find(edges[i].second.second)) {
            uf.unite (edges[i].second.first,edges[i].second.second);
            cout<<edges[i].second.first<<"--"<<edges[i].second.second<<endl;
        }
    }
    return 0;
}

通过查看gdb的回溯,它似乎是vector / class析构函数的一个问题:

(gdb) backtrace
#0  0x00007ffff74ab267 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:55
#1  0x00007ffff74aceca in __GI_abort () at abort.c:89
#2  0x00007ffff74eebf3 in __libc_message (do_abort=do_abort@entry=1, 
    fmt=fmt@entry=0x7ffff7607168 "*** Error in `%s': %s: 0x%s ***\n")
    at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff74f6c09 in malloc_printerr (ptr=<optimized out>, 
    str=0x7ffff76032ba "free(): invalid pointer", action=1) at malloc.c:4965
#4  _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:3834
#5  0x00007ffff74fa83c in __GI___libc_free (mem=<optimized out>) at malloc.c:2950
#6  0x0000000000402cfc in __gnu_cxx::new_allocator<int>::deallocate (this=0x7fffffffdf58, 
    __p=0x618d00) at /usr/include/c++/5/ext/new_allocator.h:110
#7  0x0000000000402588 in __gnu_cxx::__alloc_traits<std::allocator<int> >::deallocate (__a=..., 
    __p=0x618d00, __n=10) at /usr/include/c++/5/ext/alloc_traits.h:185
#8  0x0000000000401ce6 in std::_Vector_base<int, std::allocator<int> >::_M_deallocate (
    this=0x7fffffffdf58, __p=0x618d00, __n=10) at /usr/include/c++/5/bits/stl_vector.h:178
#9  0x00000000004018a8 in std::_Vector_base<int, std::allocator<int> >::~_Vector_base (
    this=0x7fffffffdf58, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/stl_vector.h:160
#10 0x0000000000401498 in std::vector<int, std::allocator<int> >::~vector (this=0x7fffffffdf58, 
    __in_chrg=<optimized out>) at /usr/include/c++/5/bits/stl_vector.h:425
#11 0x000000000040140b in UnionFind::~UnionFind (this=0x7fffffffdf40, __in_chrg=<optimized out>)
    at kruskal.cpp:7
#12 0x0000000000401023 in main () at kruskal.cpp:47

有人可以解释当我在循环中包含路径压缩时会发生什么中断吗?

编辑:提供错误的示例输入:

10 14
1 5 1
1 8 10
2 3 2
4 1 4
4 8 1
5 6 3
6 2 1
6 3 3
6 7 7
6 9 1
8 5 5
8 9 9
9 10 2
10 7 1

1 个答案:

答案 0 :(得分:5)

只有10个节点,但是有9-10个边缘和10-7个边缘,因此您访问了向量的超出范围。尝试在存储之前将1原点输入转换为0原点。

尝试

for (int i=0;i<m;i++) {
    cin>>v1>>v2>>w;
    edges[i]=pip(w,pii(v1-1,v2-1));
}

而不是

for (int i=0;i<m;i++) {
    cin>>v1>>v2>>w;
    edges[i]=pip(w,pii(v1,v2));
}

cout<<(edges[i].second.first+1)<<"--"<<(edges[i].second.second+1)<<endl;

而不是

cout<<edges[i].second.first<<"--"<<edges[i].second.second<<endl;