Hi everyone.
I wrote my first useful program in C++ [maybe after hello world]. After hours of effort to make it survive the compilation [and it finally did] I can't make it work right. I get at runtime the message : Segmentation fault. using gdb I found that it is somehow related to the comparison functor I defined... still can't understand how can one create a segmentation fault without arrays.
I have included the parts of the code which I think that are relevant. If it helps , I am trying to implement Kruskal's algorithm to find minimal spanning tree:
bar.Code://this is the functor: struct lvert { bool operator()(const vert &v1,const vert &v2) { //comparing the graph's verticles so they can be ordered. if(v1.x==v2.x) return v1.y<v2.y; return v1.x<v2.x; } }; //gdb says that the problem comes from here: int unite(vert v1,vert v2,map<vert,set<vert,lvert> ,lvert > &dsets,vector<vector<vert> >& verts) { cout << v1.x << ":" << v1.y << " " << v2.x << ":" << v2.y << endl; set<vert,lvert> *S1 = &dsets[verts[v1.x][v1.y]]; //I guess this is where something is wrong set<vert,lvert> *S2 = &dsets[verts[v2.x][v2.y]]; //or maybe here? set<vert,lvert>::iterator i; for(i=S2->begin(); i!=S2->end(); i++) { verts[i->x][i->y] = verts[v1.x][v1.y]; } S1->insert(S2->begin(),S2->end()); dsets.erase(v2); } //and this code is somewhere in main() , it calls unite() for(i=0; i<N; i++) { verts[i].resize(M); for(j=0; j<M; j++) { verts[i][j] = vert(i,j); dsets[vert(i,j)].insert(vert(i,j)); cin >> state; vert v1(i,j); vert v2(i+1,j); vert v3(i,j+1); if(state==3) { Sedges.erase(edge(i,j,i,j+1)); unite(v1,v3,dsets,verts); Sedges.erase(edge(i,j,i+1,j)); unite(v1,v2,dsets,verts); } if(state==2) { Sedges.erase(edge(i,j,i,j+1)); unite(v1,v3,dsets,verts); } if(state==1) { Sedges.erase(edge(i,j,i+1,j)); unite(v1,v2,dsets,verts); } } }



LinkBack URL
About LinkBacks


