diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2022-10-19 17:27:59 +0200 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2022-10-19 17:27:59 +0200 |
commit | e4d86723227dc3694bf502dfd492e8c2b1887c2c (patch) | |
tree | fa114525f8a24caad2425c3753c440e7520085e4 /hungarian.cpp | |
parent | 6308773c0b6b745d49d20dc2afd6ab7ec63cb996 (diff) | |
download | code-e4d86723227dc3694bf502dfd492e8c2b1887c2c.tar.gz code-e4d86723227dc3694bf502dfd492e8c2b1887c2c.tar.bz2 code-e4d86723227dc3694bf502dfd492e8c2b1887c2c.zip |
Diffstat (limited to 'hungarian.cpp')
-rw-r--r-- | hungarian.cpp | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/hungarian.cpp b/hungarian.cpp index b8cb7ab..8c01a78 100644 --- a/hungarian.cpp +++ b/hungarian.cpp @@ -1,14 +1,14 @@ -#include <iostream> #include <cmath> #include <functional> -#include <random> -#include <vector> +#include <iostream> #include <limits> #include <queue> +#include <random> #include <stack> +#include <vector> -#include "randutils/randutils.hpp" #include "pcg-cpp/include/pcg_random.hpp" +#include "randutils/randutils.hpp" using Rng = randutils::random_generator<pcg32>; @@ -22,11 +22,9 @@ public: std::vector<std::reference_wrapper<Edge>> neighbors; std::array<unsigned, 2> coordinate; - Vertex() : y(0), inZ(false) {}; + Vertex() : y(0), inZ(false){}; - void addEdge(Edge& e) { - neighbors.push_back(e); - } + void addEdge(Edge& e) { neighbors.push_back(e); } bool inMatching() const; }; @@ -39,14 +37,14 @@ public: double weight; bool inPath; - Edge() : orientation(false) {}; + Edge() : orientation(false){}; void setVertices(Vertex& red, Vertex& blue) { s = &red; t = &blue; red.addEdge(*this); blue.addEdge(*this); } - Vertex& tail() { + Vertex& tail() { if (orientation) { return *t; } else { @@ -74,9 +72,7 @@ public: return *t; } } - bool isTight() const { - return std::abs(s->y + t->y - weight) < 1e-13; - } + bool isTight() const { return std::abs(s->y + t->y - weight) < 1e-13; } }; bool Vertex::inMatching() const { @@ -112,11 +108,11 @@ public: void hungarian() { std::queue<std::reference_wrapper<Vertex>> q; - for (Vertex &v : T) { + for (Vertex& v : T) { v.inZ = false; } - for (Vertex &v : S) { + for (Vertex& v : S) { v.inZ = false; if (!v.inMatching()) { @@ -141,7 +137,9 @@ public: for (Vertex& v : T) { if (v.inZ && !v.inMatching()) { - std::stack<std::tuple<std::vector<std::reference_wrapper<Edge>>::iterator, std::vector<std::reference_wrapper<Edge>>::iterator, unsigned>> path; + std::stack<std::tuple<std::vector<std::reference_wrapper<Edge>>::iterator, + std::vector<std::reference_wrapper<Edge>>::iterator, unsigned>> + path; for (Edge& e : E) { e.inPath = false; @@ -167,7 +165,7 @@ public: if (!e.tail().inMatching()) { break; } else { - e.inPath= true; + e.inPath = true; path.push({e.tail().neighbors.begin(), e.tail().neighbors.end(), e.tail().index}); } } else { @@ -209,25 +207,23 @@ public: v.y -= Δ; } } - } } bool isPerfect() const { for (const Vertex& v : S) { if (!v.inMatching()) { - return false; + return false; } } for (const Vertex& v : T) { if (!v.inMatching()) { - return false; + return false; } } return true; } - }; int main(int argc, char* argv[]) { @@ -265,11 +261,8 @@ int main(int argc, char* argv[]) { for (const Edge& e : G.E) { if (e.orientation) { - std::cout - << e.tail().coordinate[0] << " " - << e.tail().coordinate[1] << " " - << e.head().coordinate[0] << " " - << e.head().coordinate[1] << std::endl; + std::cout << e.tail().coordinate[0] << " " << e.tail().coordinate[1] << " " + << e.head().coordinate[0] << " " << e.head().coordinate[1] << std::endl; } } |