87namespace blossom_detail {
102template <AlephGraph GT, ArcFilter<GT> SA>
133 return std::make_pair(u, v);
145 Node *p = it.get_curr();
147 NODE_COOKIE(p) =
reinterpret_cast<void *
>(idx + 1);
158 for (
size_t i = 0; i < n; ++i)
165 Arc *a = it.get_curr_ne();
169 const auto src_idx_plus_one =
reinterpret_cast<uintptr_t
>(
NODE_COOKIE(src));
170 const auto tgt_idx_plus_one =
reinterpret_cast<uintptr_t
>(
NODE_COOKIE(tgt));
171 if (src_idx_plus_one == 0 or tgt_idx_plus_one == 0)
174 const size_t u = src_idx_plus_one - 1;
175 const size_t v = tgt_idx_plus_one - 1;
185 const auto &pair_item = it.get_curr();
186 const size_t u = pair_item.first.first;
187 const size_t v = pair_item.first.second;
197 [[nodiscard]]
size_t lca(
size_t a,
size_t b)
const
206 a =
static_cast<size_t>(
base_[a]);
210 a =
static_cast<size_t>(
parent_[
static_cast<size_t>(
match_[a])]);
215 b =
static_cast<size_t>(
base_[b]);
220 b =
static_cast<size_t>(
parent_[
static_cast<size_t>(
match_[b])]);
223 assert(
false and "Blossom::lca fallback reached");
228 void mark_path(
size_t v,
const size_t blossom_base,
size_t child)
230 while (
static_cast<size_t>(
base_[v]) != blossom_base)
232 const auto matched_v =
static_cast<size_t>(
match_[v]);
235 parent_[v] =
static_cast<long>(child);
237 v =
static_cast<size_t>(
parent_[matched_v]);
256 for (
size_t i = 0; i < n; ++i)
261 for (
size_t i = 0; i < n; ++i)
262 base_[i] =
static_cast<long>(i);
276 const bool creates_blossom = u ==
root or
281 const size_t cur_base =
lca(v, u);
282 for (
size_t i = 0; i < n; ++i)
287 for (
size_t i = 0; i < n; ++i)
290 base_[i] =
static_cast<long>(cur_base);
300 parent_[u] =
static_cast<long>(v);
302 return static_cast<long>(u);
322 const long pv =
parent_[
static_cast<size_t>(v)];
326 const long next =
match_[
static_cast<size_t>(pv)];
327 match_[
static_cast<size_t>(v)] = pv;
328 match_[
static_cast<size_t>(pv)] = v;
352 for (
size_t i = 0; i < n; ++i)
382 size_t cardinality = 0;
432template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
438 <<
"compute_maximum_cardinality_general_matching(): g is a digraph";
443 const size_t cardinality = matcher.
solve();
446 for (
size_t i = 0; i < mate.size(); ++i)
447 if (mate[i] != -1
and i <
static_cast<size_t>(mate[i]))
449 auto *arc = matcher.
get_pair_arc(i,
static_cast<size_t>(mate[i]));
451 <<
"Blossom internal error: missing arc for matched pair";
465template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
470 return compute_maximum_cardinality_general_matching<GT, SA>(g, matching, sa);
480template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
499 return compute_maximum_cardinality_general_matching<GT, SA>(g, matching,
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_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void empty() noexcept
Empties the container.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Functor wrapper for maximum cardinality general matching.
size_t operator()(const GT &g, DynDlist< typename GT::Arc * > &matching)
Computes a maximum matching.
Compute_Maximum_Cardinality_General_Matching(SA __sa=SA())
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.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
void clear() noexcept
Empties the container.
bool is_empty() const noexcept
Return true if this is empty.
Generic key-value map implemented on top of a binary search tree.
typename Base::Iterator Iterator
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
void empty()
Remove all the elements of the set.
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.
static constexpr long No_Vertex
void augment_matching(const long endpoint)
Augment the matching along the path ending at endpoint.
void build_adjacency()
Build a simplified adjacency list for faster traversal.
Arc * get_pair_arc(size_t u, size_t v) const noexcept
Returns the arc connecting node indices u and v.
DynListQueue< size_t > bfs_queue_
DynMapTree< Pair_Key, Arc * > pair_to_arc_
void build_node_index()
Index nodes from 0 to n-1 and set up the cookie-based mapping.
size_t solve()
Execute the matching algorithm.
size_t lca(size_t a, size_t b) const
Find the lowest common ancestor of two nodes in the alternating tree.
Edmonds_Blossom_Matcher(const GT &graph, SA __sa=SA())
Initialize the matcher with a graph and an optional filter.
const Array< long > & get_match_vector() const noexcept
Returns the match vector (mate of each node index).
void mark_path(size_t v, const size_t blossom_base, size_t child)
Mark the path from v to the blossom base and set up parents.
Cookie_Saver< GT > cookie_saver_
long find_augmenting_path(const size_t root)
Search for an augmenting path starting from root.
std::pair< size_t, size_t > Pair_Key
static Pair_Key normalized_pair(size_t u, size_t v) noexcept
Array< Array< size_t > > adjacency_
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.
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.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
size_t compute_maximum_cardinality_general_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Computes a maximum cardinality matching in a general graph.
#define NODE_COOKIE(p)
Return the node cookie
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Filtered iterator on all the arcs of a graph.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.