diff --git a/Algorithms/Graph Algorithms/kruskal_algorithm.cpp b/Algorithms/Graph Algorithms/kruskal_algorithm.cpp new file mode 100644 index 00000000..12e7f57f --- /dev/null +++ b/Algorithms/Graph Algorithms/kruskal_algorithm.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; +#define ll long long +#define MAX 1000005 +ll id[MAX]; + +// We use disjoint sets to detect whether adding a edge to the growing tree creates a cycle or not. + +/** + This function set's up the array to implement disjoint sets +*/ +void setDisjoint(){ + for(ll i=0; i< MAX; i++) + id[i] = i; +} + +/** + This function takes the number and returns its root in disjoint set +*/ +ll root(ll n){ + while(id[n] != n){ + id[n] = id[id[n]]; + n = id[n]; + } + return n; +} + +/** + This function takes two number and puts them in same set i.e. performs the union operation +*/ +void disjointUnion(ll from, ll to){ + ll p = root(from); + ll q = root(to); + id[p] = id[q]; +} + +/** + This function implements the kruskal's algorithm +*/ +ll kruskal(pair< ll, pair > p[], ll nodes, ll edges) +{ + ll minCost = 0; + for(ll i=0; i> nodes; + + cout << "Enter the number of edges: "; + cin >> num_of_edges; + + pair< ll, pair > edges[num_of_edges]; + + cout << "Enter the edges in order of : from to weight\n"; + for(ll i=0; i> from >> to >> weight; + edges[i] = make_pair(weight, make_pair(from, to)); + } + + sort(edges, edges + num_of_edges); + ll MST = kruskal(edges, nodes, num_of_edges); + + cout << "Minimum cost spanning tree is : " << MST << endl; + return 0; +}