80#ifndef BLOSSOM_WEIGHTED_H
81#define BLOSSOM_WEIGHTED_H
111template <
typename Weight_Type =
long long>
118namespace blossom_weighted_detail {
125 static_assert(std::is_integral_v<T>,
"Blossom_Weighted requires integral arc weights");
127 if constexpr (std::is_signed_v<T>)
129 using Common = std::common_type_t<T, long long>;
130 constexpr auto ll_max = std::numeric_limits<long long>::max();
131 constexpr auto ll_min = std::numeric_limits<long long>::min();
135 <<
"Weight cannot be represented as long long";
139 using Common = std::common_type_t<T, long long>;
140 constexpr auto ll_max = std::numeric_limits<long long>::max();
144 <<
"Weight cannot be represented as long long";
147 return static_cast<long long>(
value);
177template <AlephGraph GT,
class Weight = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
181 Weight weight = Weight(),
187 using Raw_Weight = std::decay_t<decltype(weight(static_cast<Arc *>(
nullptr)))>;
192 static_assert(std::is_integral_v<Raw_Weight>,
"Blossom_Weighted requires integral arc weights");
201 constexpr auto max_vertex =
static_cast<size_t>(std::numeric_limits<MwVertex>::max());
203 <<
"Graph has too many vertices for weighted blossom implementation";
210 Node *p = it.get_curr();
220 long long weight = 0;
229 Arc *arc = it.get_curr_ne();
237 <<
"Weighted blossom internal node index mapping is invalid";
262 return a.weight > b.weight;
269 for (
size_t i = 0; i <
records.size();)
293 long long total_weight = 0;
294 size_t cardinality = 0;
303 const size_t mid = lo + (hi - lo) / 2;
320 for (
const auto &[
fst,
snd] : matched_pairs)
322 auto u =
static_cast<size_t>(
fst);
323 auto v =
static_cast<size_t>(
snd);
329 <<
"Weighted blossom returned an unknown matched pair";
332#if defined(_MSC_VER) && !defined(__clang__)
335 const long long w = arc_info->weight;
336 const long long lo = std::numeric_limits<long long>::min();
337 const long long hi = std::numeric_limits<long long>::max();
339 <<
"Matching total weight overflows long long";
344 static_cast<__int128>(total_weight) +
static_cast<__int128>(arc_info->weight);
346 sum <
static_cast<__int128>(std::numeric_limits<long long>::min()))
347 <<
"Matching total weight overflows long long";
348 total_weight =
static_cast<long long>(
sum);
373template <AlephGraph GT,
class Weight = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
376 Weight weight = Weight(),
388template <AlephGraph GT,
class Weight = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
High-level sorting functions for Aleph containers.
WeightedDigraph::Node Node
Internal weighted matching core used by Blossom_Weighted.H.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Functor wrapper for weighted blossom matching.
Compute_Maximum_Weight_General_Matching(Weight weight=Weight(), SA sa=SA(), const bool max_cardinality=false)
Construct the solver with specific options.
Blossom_Weighted_Result< long long > operator()(const GT &g, DynDlist< typename GT::Arc * > &matching)
Compute maximum-weight matching.
RAII guard that saves and restores graph cookies.
Dynamic doubly linked list with O(1) size and bidirectional access.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Filtered iterator on the nodes of a graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
bool is_digraph() const noexcept
Return true if the graph this is directed.
constexpr size_t get_num_arcs() const noexcept
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
RAII guards for graph node/arc cookies.
Blossom_Weighted_Result< long long > compute_maximum_weight_general_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, Weight weight=Weight(), SA sa=SA(), const bool max_cardinality=false)
Compute maximum-weight matching in a general undirected graph.
#define NODE_COOKIE(p)
Return the node cookie
Blossom_Weighted_Result< long long > blossom_maximum_weight_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, Weight weight=Weight(), SA sa=SA(), const bool max_cardinality=false)
Alias for compute_maximum_weight_general_matching().
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Array< Edge< WeightType > > adjust_weights_for_maximum_cardinality_matching(const Array< Edge< WeightType > > &edges_in)
Adjust edge weights to prioritize maximum cardinality matching.
unsigned int VertexId
Type representing the unique ID of a vertex.
std::pair< VertexId, VertexId > VertexPair
Type representing a pair of vertices.
Array< VertexPair > maximum_weight_matching(const Array< Edge< WeightType > > &edges)
Compute a maximum-weighted matching in a general undirected graph.
long long to_ll_checked(T value)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Filtered iterator on all the arcs of a graph.
Result of weighted matching.
Weight_Type total_weight
Sum of matched arc weights.
size_t cardinality
Number of arcs in the matching.
Type representing a weighted edge.
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.