diff --git a/C++/find-minimum-spanning-tree.cpp b/C++/find-minimum-spanning-tree.cpp new file mode 100644 index 0000000..8175795 --- /dev/null +++ b/C++/find-minimum-spanning-tree.cpp @@ -0,0 +1,87 @@ +/* + * This algorithm is called Kruskall's Algorithm which is used to find the + * minimum spanning tree (MST) within a graph. MST is basically a tree within + * a graph with the least weightage in its edges. + * + * Time Complexity : O(M log N) or O(M log M) [variables are as coded] + * Space Complexity : O(M) + */ + +#include +#include +using namespace std; + +#define MAX 10000000 + +int N,M,totalCost; +int rank[MAX],parent[MAX]; + +struct edge {int x,y,c;}; +edge A[MAX]; + +// To find the parent of the node x using Union Disjoint Set Data Structure (UDS). +int f_find(int x) +{ + if(parent[x] != x) + parent[x] = f_find(parent[x]); + return parent[x]; +} + +// To unify and edge with the rest of the tree (coded based on UDS). +void f_union(int x,int y) +{ + if(rank[x] > rank[y])swap(x,y); + + if(rank[x] == rank[y]) + rank[y] ++; + + parent[x] = y; +} + +// A function to help with the sorting algorithm. +bool func(edge a, edge b) +{ + return a.c < b.c; +} + +// Kruskal's algorithm to find the MST within a graph. +void kruskall() +{ + sort(A, A+M, func); + + // Initializing all the varibales. + for(int i=0;i> N; + cout << "Enter the number of edges : "; cin >> M; + cout << "Enter the edges in the format of 'Node1 Node2 Cost'\n"; + for(int i=0;i> A[i].x >> A[i].y >> A[i].c; +} + +int main() +{ + get_input(); + kruskall(); + return 0; +}