/*--------------------------kruskal algo implementation to find Minimum Spanning Tree (MST) of a weighted undirected graph------------------------*/ /*------------------------------kruskal.h this is header file-------------------------*/ /* Author: Laxman Kasyap 24/oct/2009 */ #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace tbb; #ifndef _kruskal #define _kruskal class kruskal{ private: struct Edge{ int vi,vj,w; }; // each struct Edge stores the 2 vertexes and weight of each edge in the graph typedef Edge* argument_type; vector edges; //stores the graph vector mst; //stores the MST vector circles; //used to prevent any circles in the MST int n; //number of nodes public: kruskal(int a); //constructor initialises number of nodes n=a static bool lequal(Edge const* t1, Edge const* t2); //function used as 3rd argument in parallel_sort void init_graph(map >m); //initialises the edges with given graph as map void kruskalApply(); //applies kruskal algo on edges and finds MST void showGraph(); //shows the MST }; kruskal::kruskal(int a){ n=a; } //constructor static bool kruskal::lequal(Edge const* t1, Edge const* t2){ return (t1->ww); } //comparision funtion void kruskal::init_graph(map >m){ //initialising the vector edges with the graph given Edge *e; map >::iterator it1; map::iterator it2; for(it1=m.begin();it1!=m.end();it1++) for(it2=(it1->second).begin();it2!=(it1->second).end();it2++){ e = new Edge; e->vi=it1->first; e->vj=it2->first; e->w=it2->second; ed.push_back(e); } } void kruskal::kruskalApply(){ parallel_sort(edges.begin(),edges.end(),kruskal::lequal); //applying parallel sort so the edges sort in increasing order of weights //here is where iam getting the problem after calling this function my nodes //values and weights change to 1 int number; for(int i=1;i<=n;i++) circles.push_back(i); while(mst.size()vi]!=circles[edges[0]->vj]) { //prevents any cycles in MST number = circles[edges[0]->vj]; for(int i=1;ivi]; } mst.push_back(edges[0]); } edges.erase(edges.begin(),edges.begin()+1); } } void kruskal::showGraph(){ //outputs the mst obtained into a file kruskal.out int W=0; ofstream fout("kruskal.out"); fout<w; fout<vi<<" "<vj<<" "<w<