From aa6fdd64908847b7da75c62f7fe17a4aca8895e8 Mon Sep 17 00:00:00 2001 From: ADHIRAJ-12 Date: Mon, 2 Oct 2023 10:49:20 +0530 Subject: [PATCH] Kruskals algo code and explanation --- Graphs/09 Kruskal'salgo (MST).cpp | 113 ++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Graphs/09 Kruskal'salgo (MST).cpp diff --git a/Graphs/09 Kruskal'salgo (MST).cpp b/Graphs/09 Kruskal'salgo (MST).cpp new file mode 100644 index 0000000..3b0690c --- /dev/null +++ b/Graphs/09 Kruskal'salgo (MST).cpp @@ -0,0 +1,113 @@ + + +// Problem Statement: +// Given a weighted, undirected, and connected graph of V vertices and E edges. + + +// The task : +// to find the sum of weights of the edges of the Minimum Spanning Tree. + + + +// Definition: +//A minimum spanning tree consists of N nodes and N-1 edges connecting all the nodes which have the minimum cost(sum of edge weights). + + + +// Time Complexity: + +// O(ElogE)+O(E*4*alpha), +// ElogE for sorting and E*4*alpha for findParent operation ‘E’ times + + + +// Space Complexity: +// O(N). Parent array+Rank Array + + + // Approach: +// Sort all the edges according to their weight +// Greedily pick minimum edge and make sure the two nodes belong to different components(using disjoint set data structure findParent operation). Remember, if they belong to same component it would indicate we would be having cycles which is not possible in a MST. +// Also, once we a node to be a part of the MST, we must join the two components using the union operation of DSU. + + +#include +using namespace std; +struct node { + int u; + int v; + int wt; + node(int first, int second, int weight) { + u = first; + v = second; + wt = weight; + } +}; + +bool comp(node a, node b) { + return a.wt < b.wt; +} + +int findPar(int u, vector &parent) { + if(u == parent[u]) return u; + return parent[u] = findPar(parent[u], parent); +} + +void unionn(int u, int v, vector &parent, vector &rank) { + u = findPar(u, parent); + v = findPar(v, parent); + if(rank[u] < rank[v]) { + parent[u] = v; + } + else if(rank[v] < rank[u]) { + parent[v] = u; + } + else { + parent[v] = u; + rank[u]++; + } +} +int main(){ + int N=5,m=6; + vector edges; + edges.push_back(node(0,1,2)); + edges.push_back(node(0,3,6)); + edges.push_back(node(1,0,2)); + edges.push_back(node(1,2,3)); + edges.push_back(node(1,3,8)); + edges.push_back(node(1,4,5)); + edges.push_back(node(2,1,3)); + edges.push_back(node(2,4,7)); + edges.push_back(node(3,0,6)); + edges.push_back(node(3,1,8)); + edges.push_back(node(4,1,5)); + edges.push_back(node(4,2,7)); + sort(edges.begin(), edges.end(), comp); + + vector parent(N); + for(int i = 0;i rank(N, 0); + + int cost = 0; + vector> mst; + for(auto it : edges) { + if(findPar(it.v, parent) != findPar(it.u, parent)) { + cost += it.wt; + mst.push_back({it.u, it.v}); + unionn(it.u, it.v, parent, rank); + } + } + cout << cost << endl; + for(auto it : mst) cout << it.first << " - " << it.second << endl; + return 0; +} + + +Output: + +16 +0 – 1 +1 – 2 +1 – 4 +0 – 3