71 using Cost_Edge = tuple<size_t, size_t, long long>;
77 vector<Cost_Edge> edges;
98 for (
size_t i = 0; i < s.num_nodes; ++i)
101 for (
const auto & [u, v, cost] : s.edges)
125 for (
auto it = matching.
get_it(); it.has_curr(); it.next_ne())
127 auto * arc = it.get_curr();
128 size_t u =
static_cast<size_t>(g.
get_src_node(arc)->get_info());
129 size_t v =
static_cast<size_t>(g.
get_tgt_node(arc)->get_info());
132 sorted_pairs.
insert({u, v});
136 for (
auto it = sorted_pairs.
get_it(); it.has_curr(); it.next_ne())
137 pairs.
append(it.get_curr());
159 for (
size_t i = 0; i < pairs.
size(); ++i)
161 const auto & p = pairs(i);
163 if (i + 1 < pairs.
size())
194 void print_backend(
const string & backend_name,
196 bool max_cardinality,
197 long long & canonical_cost,
198 size_t & canonical_cardinality,
199 bool & first_backend)
201 GT g = build_graph<GT>(s);
204 const auto result = blossom_minimum_cost_matching<GT>(
211 const auto pairs = extract_pairs(g, matching);
215 canonical_cost = result.total_cost;
216 canonical_cardinality = result.cardinality;
217 first_backend =
false;
220 cout <<
" " << backend_name
221 <<
" -> card " << result.cardinality
222 <<
", cost " << result.total_cost
223 <<
" | " << format_pairs(pairs) <<
'\n';
225 if (result.total_cost != canonical_cost
226 or result.cardinality != canonical_cardinality)
227 cerr <<
" Warning: objective mismatch across backends\n";
241 void run_scenario(
const Scenario & s)
243 cout <<
'\n' << s.title <<
'\n';
244 cout << string(s.title.size(),
'-') <<
'\n';
246 for (
const bool max_cardinality : {
false,
true})
248 cout << (max_cardinality
249 ?
"\nMode: maximum-cardinality then minimum-cost\n"
250 :
"\nMode: pure minimum-cost\n");
252 long long canonical_cost = 0;
253 size_t canonical_cardinality = 0;
254 bool first_backend =
true;
257 "List_Graph", s, max_cardinality, canonical_cost, canonical_cardinality, first_backend);
259 "List_SGraph", s, max_cardinality, canonical_cost, canonical_cardinality, first_backend);
261 "Array_Graph", s, max_cardinality, canonical_cost, canonical_cardinality, first_backend);
278 void print_backend_perfect(
const string & backend_name,
281 GT g = build_graph<GT>(s);
283 const auto result = blossom_minimum_cost_perfect_matching<GT>(g, matching);
285 cout <<
" " << backend_name <<
" -> feasible "
286 << (result.feasible ?
"yes" :
"no");
289 const auto pairs = extract_pairs(g, matching);
290 cout <<
", card " << result.cardinality
291 <<
", cost " << result.total_cost
292 <<
" | " << format_pairs(pairs);
306 void run_perfect_demo(
const Scenario & s)
308 cout <<
'\n' << s.title <<
" (perfect matching)\n";
309 cout << string(s.title.size() + 19,
'-') <<
'\n';
323 const Scenario scenario{
324 .title =
"General min-cost matching (objective-mode demo)",
340 const Scenario perfect_feasible{
341 .title =
"Perfect min-cost matching demo (feasible)",
351 const Scenario perfect_infeasible{
352 .title =
"Perfect min-cost matching demo (infeasible)",
361 cout <<
"Dedicated API: blossom_minimum_cost_matching()\n";
362 cout <<
"Dedicated API: blossom_minimum_cost_perfect_matching()\n";
363 cout <<
"All costs are edge costs in a non-bipartite graph.\n";
364 run_scenario(scenario);
365 run_perfect_demo(perfect_feasible);
366 run_perfect_demo(perfect_infeasible);
Minimum-cost matching in general undirected graphs.
size_t size_t int32_t * out
Default distance accessor for arc weights.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
Dynamic doubly linked list with O(1) size and bidirectional access.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
Array-based graph implementation.
Lazy and scalable dynamic array implementation.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.