158 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
195 auto idx =
static_cast<size_t>(
raw_idx);
198 if (idx <
arcs.size() - 1)
217 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
256 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
258 auto p = it.get_curr();
259 auto q =
kg.insert_node();
260 q->get_info().append(p);
267 auto a = it.get_curr();
270 auto ka =
kg.insert_arc(s, t, a);
288 auto pa = it.get_curr();
289 auto tgt = it.get_tgt_node_ne();
295 auto ka =
kg.insert_arc(
cp, tgt,
pa->get_info());
306 arcs.insert(it.get_curr());
323 "Arc index and graph arc count must be in sync");
326 auto s =
kg.get_src_node(a);
327 auto t =
kg.get_tgt_node(a);
332 auto cp =
kg.insert_node();
337 cp->get_info().swap(s->get_info());
338 cp->get_info().append(t->get_info());
361 auto min_cut = std::numeric_limits<size_t>::max();
366 for (
size_t i = 0; i <
num_iter; ++i)
382 auto ka =
kg.get_first_arc();
387 vs.
swap(
kg.get_src_node(
ka)->get_info());
388 vt.
swap(
kg.get_tgt_node(
ka)->get_info());
398 auto ka = it.get_curr();
404 auto ka =
kg.get_first_arc();
405 auto S =
kg.get_src_node(
ka);
406 auto T =
kg.get_tgt_node(
ka);
408 assert(S->get_info().size() +
T->get_info().size() ==
413 vs.
swap(S->get_info());
414 vt.
swap(
T->get_info());
423 const size_t n =
kg.get_num_nodes();
429 return kg.get_num_arcs();
433 const auto t =
static_cast<size_t>(std::ceil(1.0 + n / std::sqrt(2.0)));
536 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
542 num_iter =
static_cast<size_t>(1.05 * n * n);
545 size_t min_cut = std::numeric_limits<size_t>::max();
550 for (
size_t i = 0; i <
num_iter; ++i)
582 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
603 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
606 const auto num_iter =
static_cast<size_t>(1.05 * n * n);
632 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
644 size_t best_cut = std::numeric_limits<size_t>::max();
646 for (
size_t i = 0; i <
num_iter; ++i)
661 cut.
append(it.get_curr()->get_info());
663 auto ka =
kg.get_first_arc();
664 auto S =
kg.get_src_node(
ka);
665 auto T =
kg.get_tgt_node(
ka);
669 vs.
swap(S->get_info());
670 vt.
swap(
T->get_info());
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.
Stateful depth-first traversal functor.
bool has_curr() const noexcept
Return true if the iterator has current item.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
void empty() noexcept
empty the list
DynList & swap(DynList &l) noexcept
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Arc index with O(1) operations and deterministic ordering.
Karc * select(size_t i) const
std::vector< Karc * > arcs
void swap(ArcIndex &other) noexcept
Karger's randomized minimum cut algorithm.
size_t compute_min_cut_size(GT &g, size_t num_iter=0)
Compute only the minimum cut size (without partition info).
unsigned long get_seed() const noexcept
Get the random seed used by this solver (useful for reproducibility).
Karger_Min_Cut(Karger_Min_Cut &&other) noexcept
Move constructor. Transfers ownership of the random number generator.
Karger_Min_Cut(const Karger_Min_Cut &)=delete
Disable copy constructor (class owns gsl_rng pointer)
size_t operator()(GT &g, DynList< typename GT::Node * > &vs, DynList< typename GT::Node * > &vt, DynList< typename GT::Arc * > &cut, const size_t num_iter)
Compute a minimum cut with a specified number of iterations.
void contract(Kgraph &kg, size_t left_num_nodes, ArcIndex &arcs)
Contract the graph by randomly merging edges until left_num_nodes remain.
bool has_self_loop(const GT &g) const
size_t __fast_karger_min_cut(Kgraph &kg, ArcIndex &arcs)
Karger-Stein fast minimum cut algorithm (recursive version).
Karger_Min_Cut & operator=(Karger_Min_Cut &&other) noexcept
Move assignment operator. Transfers ownership of the random number generator.
void rebuild_arc_index(Kgraph &kg, ArcIndex &arcs)
Rebuild the arc index from scratch for the given graph.
~Karger_Min_Cut()
Destructor. Frees the random number generator (if not moved).
bool is_connected_filtered(const GT &g) const
void update_arcs(Kgraph &kg, Knode *p, Knode *t, Knode *cp, ArcIndex &arcs)
Update arcs when contracting nodes p and t into cp.
Karger_Min_Cut(const unsigned long _seed=time(nullptr), SA _sa=SA())
Construct a Karger minimum cut solver.
size_t fast(GT &g, DynList< typename GT::Node * > &vs, DynList< typename GT::Node * > &vt, DynList< typename GT::Arc * > &cut, size_t num_iter=0)
Compute minimum cut using Karger-Stein fast algorithm.
void validate_graph(GT &g) const
Karger_Min_Cut & operator=(const Karger_Min_Cut &)=delete
Disable copy assignment (class owns gsl_rng pointer)
void build_kgraph(GT &g, Kgraph &kg, ArcIndex &arcs)
Build the contracted graph representation from the original graph.
void reseed(const unsigned long new_seed) noexcept
Change the random seed.
size_t karger_min_cut(GT &g, DynList< typename GT::Node * > &vs, DynList< typename GT::Node * > &vt, DynList< typename GT::Arc * > &cut, const size_t num_iter)
Internal implementation of Karger's minimum cut algorithm.
Iterator on the arcs of a graph.
Graph implemented with double-linked adjacency lists.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
void reset_arcs() const
Reset all the arcs of graph (the control bits, the state, the counter and the cookie)
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.
constexpr size_t get_num_arcs() const noexcept
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Graph visualization and output generation utilities.
DynArray< Graph::Arc * > arcs
#define ARC_COUNTER(p)
Return the counter of arc p.
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
Filtered iterator on all the arcs of a graph.
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
Iterator on all arcs of a graph.
Utility algorithms and operations for graphs.
Simple graph implementation with adjacency lists.