1
0
Fork 0

Require signed type for edge weight

This commit is contained in:
Joris van Rantwijk 2024-11-08 20:30:53 +01:00
parent 67ca294840
commit b17ca1a364
2 changed files with 9 additions and 18 deletions

View File

@ -116,7 +116,7 @@ void check_input_graph(const std::vector<Edge<WeightType>>& edges)
}
// Check that edge weight will not cause overflow.
if (edge.weight > std::numeric_limits<WeightType>::max() / 4) {
if (edge.weight > std::numeric_limits<WeightType>::max() / 6) {
throw std::invalid_argument(
"Edge weight exceeds maximum supported value");
}
@ -2125,7 +2125,9 @@ private:
* This function uses O(n + m) memory, where "m" is the number of edges.
*
* @tparam WeightType Type used to represent edge weights.
* This must be a signed type.
* For example "long" or "double".
*
* @param edges Graph defined as a vector of weighted edges.
*
* @return Vector of pairs of matched vertex indices.
@ -2137,6 +2139,10 @@ template <typename WeightType>
std::vector<VertexPair> maximum_weight_matching(
const std::vector<Edge<WeightType>>& edges)
{
// Edge weight type must be signed.
static_assert(std::numeric_limits<WeightType>::is_signed,
"WeightType must be signed");
// Check that the input meets all constraints.
impl::check_input_graph(edges);
@ -2200,8 +2206,8 @@ template <typename WeightType>
std::vector<Edge<WeightType>> adjust_weights_for_maximum_cardinality_matching(
const std::vector<Edge<WeightType>>& edges_in)
{
const WeightType min_safe_weight = std::numeric_limits<WeightType>::min() / 4;
const WeightType max_safe_weight = std::numeric_limits<WeightType>::max() / 4;
const WeightType min_safe_weight = std::numeric_limits<WeightType>::min() / 6;
const WeightType max_safe_weight = std::numeric_limits<WeightType>::max() / 6;
// Copy edges.
std::vector<Edge<WeightType>> edges(edges_in);

View File

@ -327,21 +327,6 @@ BOOST_AUTO_TEST_CASE(test51_augment_blossom_nested)
BOOST_TEST(mwmatching::maximum_weight_matching(edges) == expect);
}
BOOST_AUTO_TEST_CASE(test61_unsigned)
{
std::vector<mwmatching::Edge<unsigned int>> edges = {{1, 2, 5}, {2, 3, 11}, {3, 4, 5}};
Matching expect = {{2, 3}};
BOOST_TEST(mwmatching::maximum_weight_matching(edges) == expect);
}
BOOST_AUTO_TEST_CASE(test62_unsigned2)
{
std::vector<mwmatching::Edge<unsigned int>> edges = {
{1, 2, 8}, {1, 3, 9}, {2, 3, 10}, {3, 4, 7}, {1, 6, 5}, {4, 5, 6}};
Matching expect = {{2, 3}, {1, 6}, {4, 5}};
BOOST_TEST(mwmatching::maximum_weight_matching(edges) == expect);
}
BOOST_AUTO_TEST_CASE(test_fail_bad_input)
{
EdgeVectorDouble inf_weight = {{1, 2, std::numeric_limits<double>::infinity()}};