diff options
| author | Gloria Mundi <gloria@gloria-mundi.eu> | 2025-06-07 21:20:34 +0200 |
|---|---|---|
| committer | Gloria Mundi <gloria@gloria-mundi.eu> | 2025-06-07 21:20:34 +0200 |
| commit | 88d04413ebaab961f849ac6ef3d6ff2179253d41 (patch) | |
| tree | 075e5f245f160cf3d8a03f728a4ebe41e010c5df /content/graph | |
| parent | f8f53c2f9e63f0ac89b67dc4d413ec9a76415a73 (diff) | |
make union find a struct, remove kruskal
Diffstat (limited to 'content/graph')
| -rw-r--r-- | content/graph/cycleCounting.cpp | 6 | ||||
| -rw-r--r-- | content/graph/graph.tex | 12 | ||||
| -rw-r--r-- | content/graph/kruskal.cpp | 20 |
3 files changed, 20 insertions, 18 deletions
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<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; +} |
