From 88d04413ebaab961f849ac6ef3d6ff2179253d41 Mon Sep 17 00:00:00 2001 From: Gloria Mundi Date: Sat, 7 Jun 2025 21:20:34 +0200 Subject: make union find a struct, remove kruskal --- content/graph/cycleCounting.cpp | 6 ++---- content/graph/graph.tex | 12 +++++++----- content/graph/kruskal.cpp | 20 +++++++++++--------- 3 files changed, 20 insertions(+), 18 deletions(-) (limited to 'content/graph') diff --git a/content/graph/cycleCounting.cpp b/content/graph/cycleCounting.cpp index deac71e..b7545d5 100644 --- a/content/graph/cycleCounting.cpp +++ b/content/graph/cycleCounting.cpp @@ -38,13 +38,11 @@ struct cycles { bool isCycle(cycle cur) {// cycle must be constructed from base if (cur.none()) return false; - init(ssize(adj)); // union find @\sourceref{datastructures/unionFind.cpp}@ + UnionFind uf(ssize(adj)); // union find @\sourceref{datastructures/unionFind.cpp}@ for (int i = 0; i < ssize(edges); i++) { if (cur[i]) { cur[i] = false; - if (findSet(edges[i].first) == - findSet(edges[i].second)) break; - unionSets(edges[i].first, edges[i].second); + if (!uf.link(edges[i].first, edges[i].second)) break; }} return cur.none(); } diff --git a/content/graph/graph.tex b/content/graph/graph.tex index e46ad07..bf51d74 100644 --- a/content/graph/graph.tex +++ b/content/graph/graph.tex @@ -10,11 +10,13 @@ Für jeden Kreis $K$ im Graphen gilt: Die schwerste Kante auf dem Kreis ist nicht Teil des minimalen Spannbaums. - \subsection{\textsc{Kruskal}} - \begin{methods}[ll] - berechnet den Minimalen Spannbaum & \runtime{\abs{E}\cdot\log(\abs{E})} \\ - \end{methods} - \sourcecode{graph/kruskal.cpp} + \optional{ + \subsubsection{\textsc{Kruskal}'s Algorithm \opthint} + \begin{methods}[ll] + berechnet den Minimalen Spannbaum & \runtime{\abs{E}\cdot\log(\abs{E})} \\ + \end{methods} + \sourcecode{graph/kruskal.cpp} + } \end{algorithm} \begin{algorithm}{Heavy-Light Decomposition} 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 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 edges, vector &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; +} -- cgit v1.2.3