summaryrefslogtreecommitdiff
path: root/content/graph/kruskal.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2025-06-07 21:20:34 +0200
committerGloria Mundi <gloria@gloria-mundi.eu>2025-06-07 21:20:34 +0200
commit88d04413ebaab961f849ac6ef3d6ff2179253d41 (patch)
tree075e5f245f160cf3d8a03f728a4ebe41e010c5df /content/graph/kruskal.cpp
parentf8f53c2f9e63f0ac89b67dc4d413ec9a76415a73 (diff)
make union find a struct, remove kruskal
Diffstat (limited to 'content/graph/kruskal.cpp')
-rw-r--r--content/graph/kruskal.cpp20
1 files changed, 11 insertions, 9 deletions
diff --git a/content/graph/kruskal.cpp b/content/graph/kruskal.cpp
index d42800d..98a2682 100644
--- a/content/graph/kruskal.cpp
+++ b/content/graph/kruskal.cpp
@@ -1,9 +1,11 @@
-ranges::sort(edges, less{});
-vector<Edge> mst;
-ll cost = 0;
-for (Edge& e : edges) {
- if (findSet(e.from) != findSet(e.to)) {
- unionSets(e.from, e.to);
- mst.push_back(e);
- cost += e.cost;
-}}
+ll kruskal(int n, vector<Edge> edges, vector<Edge> &mst) {
+ ranges::sort(edges, less{});
+ ll cost = 0;
+ UnionFind uf(n); // union find @\sourceref{datastructures/unionFind.cpp}@
+ for (Edge &e: edges) {
+ if (uf.link(e.from, e.to)) {
+ mst.push_back(e);
+ cost += e.cost;
+ }}
+ return cost;
+}