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();
133 const auto v =
static_cast<Common
>(
value);
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();
142 const auto v =
static_cast<Common
>(
value);
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(),
183 const bool max_cardinality =
false)
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();
213 NODE_COOKIE(p) =
reinterpret_cast<void *
>(encoded);
220 long long weight = 0;
229 Arc *arc = it.get_curr_ne();
233 const auto src_idx_plus_one =
reinterpret_cast<uintptr_t
>(
NODE_COOKIE(src));
234 const auto tgt_idx_plus_one =
reinterpret_cast<uintptr_t
>(
NODE_COOKIE(tgt));
237 <<
"Weighted blossom internal node index mapping is invalid";
239 auto u = src_idx_plus_one - 1;
240 auto v = tgt_idx_plus_one - 1;
248 records.
append(Arc_Record{u, v,
w, arc});
254 struct Arc_Record_Less
256 bool operator()(
const Arc_Record &a,
const Arc_Record &b)
const noexcept
262 return a.weight > b.weight;
269 for (
size_t i = 0; i < records.
size();)
271 const Arc_Record &best = records[i];
274 while (i < records.
size()
and records[i].u == best.u
and records[i].v == best.v)
280 for (
const Arc_Record &e : best_edges)
281 edges.
append(MwEdge(
static_cast<MwVertex
>(e.u),
static_cast<MwVertex
>(e.v), e.weight));
288 solver_edges = edges;
293 long long total_weight = 0;
294 size_t cardinality = 0;
296 auto find_edge = [&best_edges](
size_t u,
size_t v) ->
const Arc_Record *
299 size_t hi = best_edges.
size();
303 const size_t mid = lo + (hi - lo) / 2;
304 if (
const Arc_Record &cur = best_edges[mid]; cur.u < u or (cur.u == u
and cur.v < v))
310 if (lo < best_edges.
size())
312 const Arc_Record &cand = best_edges[lo];
313 if (cand.u == u
and cand.v == v)
320 for (
const auto &[fst, snd] : matched_pairs)
322 auto u =
static_cast<size_t>(fst);
323 auto v =
static_cast<size_t>(snd);
327 const Arc_Record *arc_info = find_edge(u, v);
329 <<
"Weighted blossom returned an unknown matched pair";
331 matching.
append(arc_info->arc);
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(),
378 const bool max_cardinality =
false)
380 return compute_maximum_weight_general_matching<GT, Weight, SA>(g, matching, std::move(weight),
381 std::move(sa), max_cardinality);
388template <AlephGraph GT,
class Weight = Dft_Dist<GT>, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
405 const bool max_cardinality =
false)
424 return compute_maximum_weight_general_matching<GT, Weight, SA>(g, matching,
weight_,
sa_,
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.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
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.
T & append(const T &item)
Append a copied item at the end of the list.
void empty() noexcept
@brief Empties the container.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
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.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
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().
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.
std::decay_t< typename HeadC::Item_Type > T
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
and
Check uniqueness with explicit hash + equality functors.
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.