82 namespace blossom_detail
98 template <
class GT,
class SA>
129 return std::make_pair(u, v);
141 Node *p = it.get_curr();
143 NODE_COOKIE(p) =
reinterpret_cast<void *
>(idx + 1);
154 for (
size_t i = 0; i < n; ++i)
161 Arc *a = it.get_curr_ne();
199 for (
size_t i = 0; i <
nodes.
size(); ++i)
204 a =
static_cast<size_t>(
base[a]);
208 a =
static_cast<size_t>(
parent[
static_cast<size_t>(
match[a])]);
213 b =
static_cast<size_t>(
base[b]);
218 b =
static_cast<size_t>(
parent[
static_cast<size_t>(
match[b])]);
221 assert(
false and "Blossom::lca fallback reached");
233 parent[v] =
static_cast<long>(child);
254 for (
size_t i = 0; i < n; ++i)
259 for (
size_t i = 0; i < n; ++i)
260 base[i] =
static_cast<long>(i);
281 for (
size_t i = 0; i < n; ++i)
286 for (
size_t i = 0; i < n; ++i)
299 parent[u] =
static_cast<long>(v);
301 return static_cast<long>(u);
321 const long pv =
parent[
static_cast<size_t>(v)];
326 match[
static_cast<size_t>(v)] =
pv;
327 match[
static_cast<size_t>(
pv)] = v;
351 for (
size_t i = 0; i < n; ++i)
369 for (
long & i :
match)
372 for (
size_t i = 0; i <
nodes.
size(); ++i)
381 size_t cardinality = 0;
382 for (
size_t i = 0; i <
match.
size(); ++i)
430 template <
class GT,
class SA = Dft_Show_Arc<GT>>
435 <<
"compute_maximum_cardinality_general_matching(): g is a digraph";
440 const size_t cardinality =
matcher.solve();
443 for (
size_t i = 0; i <
mate.size(); ++i)
444 if (
mate[i] != -1
and i <
static_cast<size_t>(
mate[i]))
446 auto *arc =
matcher.get_pair_arc(i,
static_cast<size_t>(
mate[i]));
448 <<
"Blossom internal error: missing arc for matched pair";
463 template <
class GT,
class SA = Dft_Show_Arc<GT>>
478 template <
class GT,
class 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_domain_error_if(C)
Throws std::domain_error if condition holds.
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.
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) const noexcept
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
void empty()
remove all elements from the set
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.
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.
Array< Array< size_t > > adjacency
DynListQueue< size_t > bfs_queue
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.
DynMapTree< Pair_Key, Arc * > pair_to_arc
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
Cookie_Saver< GT > cookie_saver
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.
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.