48#ifndef TPL_BIPARTITE_H
49#define TPL_BIPARTITE_H
70template <AlephGraph GT>
76template <AlephGraph GT>
102template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
115 typename GT::Node *p = nit.get_curr();
129 typename GT::Arc *a = it.get_curr();
135 typename GT::Node *q = it.get_tgt_node();
150 typename GT::Arc *a = it.get_curr();
152 if (color<GT>(a) ==
Bp_Red)
157 typename GT::Node *q = it.get_tgt_node();
159 if (color<GT>(q) ==
Bp_Red)
180template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
203 compute_bipartite<GT, SA>(g,
l,
r,
sa);
242 compute_bipartite<GT, SA>(g,
l,
r, sa);
260 bool has_arcs =
false;
263 typename GT::Node *p = it.get_curr();
273 typename AN::Node *source = net.insert_node();
278 typename GT::Node *p = i.get_curr();
281 typename AN::Node *src = mapped_node<GT, AN>(p);
282 net.insert_arc(source, src, 1);
286 typename GT::Arc *arc = j.get_current_arc_ne();
289 typename AN::Arc *a = net.insert_arc(src, tgt, 1);
295 typename AN::Node *sink = net.insert_node();
301 typename GT::Node *p = it.get_curr();
302 net.insert_arc(mapped_node<GT, AN>(p), sink, 1);
309 typename AN::Arc *a = it.get_curr();
314 auto *pair_ptr = arc_map.
search(a);
315 if (pair_ptr ==
nullptr)
318 matching.
append(pair_ptr->second);
343 :
sa(
std::move(__sa)) {}
361 compute_maximum_cardinality_bipartite_matching<GT, Max_Flow, SA>(g, matching,
sa);
379template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
392 Node *start = nit.get_curr();
396 color<GT>(start) =
Bp_Red;
405 const long p_color = color<GT>(p);
409 if (
Node *q = it.get_tgt_node(); color<GT>(q) ==
Bp_White)
411 color<GT>(q) = neighbor_color;
412 if (neighbor_color ==
Bp_Red)
418 else if (color<GT>(q) == p_color)
428template <AlephGraph GT, ArcFilter<GT> SA>
444template <AlephGraph GT, ArcFilter<GT> SA>
447 noexcept(
noexcept(f.it.next_ne()))
452 for (; f.it.has_curr(); f.it.next_ne())
454 Arc *arc = f.it.get_current_arc_ne();
459 if (match_arc ==
nullptr)
474template <AlephGraph GT, ArcFilter<GT> SA>
475[[gnu::always_inline]]
inline void
478 auto a = f.it.get_current_arc_ne();
494template <AlephGraph GT, ArcFilter<GT> SA>
502 constexpr long INF = std::numeric_limits<long>::max();
505 auto match_frames = [&g, &frames]()
507 while (not frames.is_empty())
514 auto here = dfs_here<Frame>(u, sa);
535 if (frames.is_empty())
538 Frame &f = frames.top();
574template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
581 constexpr long INF = std::numeric_limits<long>::max();
589 const bool is_bipartite = compute_bipartite_all_components<GT, SA>(g, left_nodes, right_nodes, sa);
599 for (
auto it = left_nodes.
get_it(); it.has_curr(); it.next_ne())
601 for (
auto it = right_nodes.
get_it(); it.has_curr(); it.next_ne())
606 auto bfs = [&]() ->
bool
609 long dist_to_free = INF;
612 for (
auto it = left_nodes.
get_it(); it.has_curr(); it.next_ne())
626 if (u_dist >= dist_to_free)
631 Arc *arc = it.get_current_arc_ne();
637 if (pair_v ==
nullptr)
638 dist_to_free = u_dist + 1;
647 return dist_to_free != INF;
654 for (
auto it = left_nodes.
get_it(); it.has_curr(); it.next_ne())
656 hopcroft_karp_dfs<GT, SA>(g, u, frames, sa);
661 for (
auto it = left_nodes.
get_it(); it.has_curr(); it.next_ne())
678template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
698 hopcroft_karp_matching<GT, SA>(g, matching,
sa);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
WeightedDigraph::Node Node
Class that takes a bipartite graph and computes the partition sets.
void operator()(const GT &g, DynDlist< typename GT::Node * > &l, DynDlist< typename GT::Node * > &r)
Computes the partition sets of a bipartite graph.
Compute_Bipartite(SA __sa=SA())
Builds the computation with the filter of the arcs.
Class for computing the maximum cardinality matching of a bipartite graph.
void operator()(const GT &g, DynDlist< typename GT::Arc * > &matching)
Computes the maximum bipartite matching of a graph.
Compute_Maximum_Cardinality_Bipartite_Matching(SA __sa=SA())
Builds the computation with the filter of the arcs.
RAII guard that clears graph cookies on destruction.
Stack of the frames of a depth-first search that does not recurse.
bool has_curr() const noexcept
Return true if the iterator has current item.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
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.
T & put(const T &item)
See append(const T & item).
T get()
See remove_first().
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.
bool is_empty() const noexcept
Return true if this is empty.
T & append(const T &item)
T & put(const T &item)
See insert(const T & item).
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Functor wrapper for hopcroft_karp_matching.
Hopcroft_Karp_Matching(SA __sa=SA())
Builds the computation with the filter of the arcs.
void operator()(const GT &g, DynDlist< typename GT::Arc * > &matching)
Computes the maximum matching using Hopcroft-Karp.
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.
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
RAII guards for graph node/arc cookies.
void compute_bipartite(const GT &g, DynDlist< typename GT::Node * > &l, DynDlist< typename GT::Node * > &r, SA sa=SA())
Computes the partition sets of a bipartite graph.
void compute_maximum_cardinality_bipartite_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Computes the maximum cardinality matching of a bipartite graph.
void dfs_replace(T &here, Args &&...args)
Replaces the frame that a Dfs_Here holds with one built with args, by assignment.
#define NODE_COUNTER(p)
Get the counter of a node.
#define ARC_COUNTER(p)
Return the counter of arc p.
bool compute_bipartite_all_components(const GT &g, DynDlist< typename GT::Node * > &l, DynDlist< typename GT::Node * > &r, SA sa=SA())
Computes the partition sets of a bipartite graph handling all connected components.
void hopcroft_karp_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Computes a maximum cardinality matching on a bipartite graph using the Hopcroft-Karp algorithm.
#define NODE_COOKIE(p)
Return the node cookie
T & dfs_frame(T &here) noexcept
The frame that a Dfs_Here holds.
#define ALEPH_FLATTEN
[[gnu::flatten]], or nothing where it would only make the compilation slower.
Main namespace for Aleph-w library functions.
GT::Node * hopcroft_karp_next(const GT &g, Hopcroft_Karp_Frame< GT, SA > &f, bool &free) noexcept(noexcept(f.it.next_ne()))
static bool hopcroft_karp_dfs(const GT &g, typename GT::Node *u, Dfs_Stack< Hopcroft_Karp_Frame< GT, SA > > &frames, const SA &sa)
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
void next()
Advance all underlying iterators (bounds-checked).
void hopcroft_karp_match(const GT &g, Hopcroft_Karp_Frame< GT, SA > &f) noexcept
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
static long & color(typename GT::Node *p)
Filtered iterator on all the arcs of a graph.
Default filter for filtered iterators on arcs.
Functor wrapper for ford_fulkerson_maximum_flow().
A left node that the search for an augmenting path goes through, and the next of its arcs to look at,...
Hopcroft_Karp_Frame(typename GT::Node *n, const SA &sa)
Node_Arc_Iterator< GT, SA > it
void next_arc() noexcept(noexcept(it.next_ne()))
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Filtered iterator of adjacent arcs of a node.
Stack of the frames of a depth-first search that does not recurse.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic key-value map based on balanced binary search trees.
Network flow graph structures.