Skip to content

Instantly share code, notes, and snippets.

@dabasajay
Created January 26, 2021 17:46
Show Gist options
  • Save dabasajay/aab8c0d619e5e70cd8ee07c313f8b6c0 to your computer and use it in GitHub Desktop.
Save dabasajay/aab8c0d619e5e70cd8ee07c313f8b6c0 to your computer and use it in GitHub Desktop.
dsa/graphs/mst | desc: kruskal minimum spanning tree mst
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, pair<int, int>>
const int N = 2e5 + 5;
int arr[N];
int _find(int key);
void _union(int a, int b);
bool myComparator(const pp &a, const pp &b){
return a.second.second < b.second.second;
}
void kruskal(vector<pp> edgeList){
sort(edgeList.begin(), edgeList.end(), myComparator);
vector<pp> edgesInMST;
for(auto edge : edgeList){
auto a = edge.first;
auto b = edge.second.first;
auto weight = edge.second.second;
if (_find(a) != _find(b)) {
_union(a, b);
edgesInMST.push_back({a, {b, weight}});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment