-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskals_Minimum_Spanning_Tree_Union_Find.cpp
More file actions
71 lines (59 loc) · 1.84 KB
/
Copy pathKruskals_Minimum_Spanning_Tree_Union_Find.cpp
File metadata and controls
71 lines (59 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include "set"
#include "unordered_map"
#include "algorithm"
using namespace std;
struct Edge{
int source,destination,weight;
};
struct Compare{
inline bool operator ()( Edge a, Edge b){
return (a.weight > b.weight);
}
};
class DisjointSet{
unordered_map <int ,int> parent;
public:
void makeSet(int V){
for(int i = 0;i < V;i++) parent[i] = i;
}
int Find(int k ){
if(parent[k] == k) return k ;
return Find(parent[k]);
}
void Union(int a, int b){
parent[Find(a)] = Find(b);
}
};
vector<Edge> Kruskals_Minimum_Spanning_Tree(int V,vector<Edge> edges){
vector<Edge> MST;
DisjointSet ds;
ds.makeSet(V);
while (MST.size() != V - 1){
Edge next_edge = edges.back();
edges.pop_back();
if(ds.Find(next_edge.source) != ds.Find(next_edge.destination)){
ds.Union(next_edge.source,next_edge.destination);
MST.push_back(next_edge);
}
}
return MST;
}
int main(){
vector<Edge> edges =
{
// (u, v, w) tiplet represent undirected edge from
// vertex u to vertex v having weight w
{ 0, 1, 7 }, { 1, 2, 8 }, { 0, 3, 5 }, { 1, 3, 9 },
{ 1, 4, 7 }, { 2, 4, 5 }, { 3, 4, 15 }, { 3, 5, 6 },
{ 4, 5, 8 }, { 4, 6, 9 }, { 5, 6, 11 }
};
sort(edges.begin(),edges.end(),Compare());
//for(auto i : edges) cout<<i.source <<"->"<<i.destination<<" "<<i.weight<<"\n";
set <int > setsize;
for(auto i : edges){ setsize.insert(i.source);setsize.insert(i.destination);}
int V = setsize.size();
vector<Edge>MST = Kruskals_Minimum_Spanning_Tree( V,edges);
for(auto i : MST) cout << "(" << i.source << ", " << i.destination << ", "<< i.weight << ")" << endl;
}