From 9c669f2d038012e356202c0a7f16f065f4b684c7 Mon Sep 17 00:00:00 2001 From: Joris van Rantwijk Date: Thu, 18 May 2023 09:55:55 +0200 Subject: [PATCH] Remove dependence on Boost.Hash --- cpp/mwmatching.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/cpp/mwmatching.h b/cpp/mwmatching.h index 8d05e18..8bc5d45 100644 --- a/cpp/mwmatching.h +++ b/cpp/mwmatching.h @@ -1,8 +1,5 @@ /** * Algorithm for finding a maximum weight matching in general graphs. - * - * Depends on Boost.Hash. - * Tested with Boost v1.74, available from https://www.boost.org/ */ #ifndef MWMATCHING_H_ @@ -18,12 +15,9 @@ #include #include #include -#include #include #include -#include - namespace mwmatching { @@ -95,7 +89,7 @@ inline VertexPair flip_vertex_pair(const VertexPair& vt) * The graph may not have self-edges or multiple edges between * the same pair of vertices. Edge weights must be within a valid range. * - * This function takes time O(m). + * This function takes time O(m * log(m)). * * @param edges Vector of weighted edges defining the graph. * @throw std::invalid_argument If the input graph is not valid. @@ -136,16 +130,20 @@ void check_input_graph(const std::vector>& edges) } // Check that the graph does not contain duplicate edges. - std::unordered_set> edges_seen; + std::vector edge_endpoints; + edge_endpoints.reserve(edges.size()); for (const Edge& edge : edges) { VertexPair vt = edge.vt; if (vt.first > vt.second) { - vt = flip_vertex_pair(vt); - } - auto status = edges_seen.insert(vt); - if (!status.second) { - throw std::invalid_argument("Duplicate edges are not supported"); + std::swap(vt.first, vt.second); } + edge_endpoints.push_back(vt); + } + + std::sort(edge_endpoints.begin(), edge_endpoints.end()); + if (std::unique(edge_endpoints.begin(), edge_endpoints.end()) + != edge_endpoints.end()) { + throw std::invalid_argument("Duplicate edges are not supported"); } }