46# include <type_traits>
163 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
190 assert(i <
arcs.size());
200 auto idx =
static_cast<size_t>(raw_idx);
201 assert(idx <
arcs.size());
202 assert(
arcs[idx] == a);
203 if (idx <
arcs.size() - 1)
214 [[nodiscard]]
size_t size()
const {
return arcs.size(); }
222 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
261 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
263 auto p = it.get_curr();
265 q->get_info().append(p);
272 auto a = it.get_curr();
293 auto pa = it.get_curr();
294 auto tgt = it.get_tgt_node_ne();
311 arcs.insert(it.get_curr());
318 [[gnu::noinline]]
size_t draw_arc(
const size_t num_arcs)
337 "Arc index and graph arc count must be in sync");
339 auto a =
arcs.select(num_arc);
351 cp->get_info().swap(s->get_info());
352 cp->get_info().append(t->get_info());
371 const size_t num_iter)
375 auto min_cut = std::numeric_limits<size_t>::max();
380 for (
size_t i = 0; i < num_iter; ++i)
398 cut.
append(ka->get_info());
412 auto ka = it.get_curr();
415 cut.
append(ka->get_info());
422 assert(S->get_info().size() +
T->get_info().size() ==
427 vs.
swap(S->get_info());
428 vt.
swap(
T->get_info());
447 const auto t =
static_cast<size_t>(std::ceil(1.0 + n / std::sqrt(2.0)));
503 noexcept(std::is_nothrow_move_constructible_v<SA>)
513 noexcept(std::is_nothrow_move_assignable_v<SA>)
514 requires std::is_move_assignable_v<SA>
518 sa = std::move(other.sa);
520 r = std::move(other.r);
530 void reseed(
const unsigned long new_seed)
noexcept
533 if (
r.
get() !=
nullptr)
549 assert(
r.
get() !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
555 num_iter =
static_cast<size_t>(1.05 * n * n);
558 size_t min_cut = std::numeric_limits<size_t>::max();
563 for (
size_t i = 0; i < num_iter; ++i)
593 const size_t num_iter)
595 assert(
r.
get() !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
616 assert(
r.
get() !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
619 const auto num_iter =
static_cast<size_t>(1.05 * n * n);
645 assert(
r.
get() !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
651 const double log_n = std::log2(
static_cast<double>(g.
get_num_nodes()));
652 num_iter =
static_cast<size_t>(std::ceil(log_n * log_n));
657 size_t best_cut = std::numeric_limits<size_t>::max();
659 for (
size_t i = 0; i < num_iter; ++i)
667 if (current_cut >= best_cut)
670 best_cut = current_cut;
674 cut.
append(it.get_curr()->get_info());
682 vs.
swap(S->get_info());
683 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.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
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(shows_ne)
Advances the iterator to the next filtered element, as next() does.
A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer,...
void swap(Gsl_Compatible_Rng_Owner &o) noexcept
Swaps the generators, with their gsl_rng.
Gsl_Compatible_Rng * get() const noexcept
The generator, or nullptr after a move.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
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(const Karger_Min_Cut &)=delete
Disable copy constructor (class owns its generator)
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).
void rebuild_arc_index(Kgraph &kg, ArcIndex &arcs)
Rebuild the arc index from scratch for the given graph.
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 its generator)
void build_kgraph(GT &g, Kgraph &kg, ArcIndex &arcs)
Build the contracted graph representation from the original graph.
Gsl_Compatible_Rng_Owner r
void reseed(const unsigned long new_seed) noexcept
Change the random seed.
size_t draw_arc(const size_t num_arcs)
The position of an arc among num_arcs, drawn out of line: a contraction takes much longer than a draw...
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.
Karger_Min_Cut(Karger_Min_Cut &&other) noexcept(std::is_nothrow_move_constructible_v< SA >)
Move constructor.
Karger_Min_Cut & operator=(Karger_Min_Cut &&other) noexcept(std::is_nothrow_move_assignable_v< SA >)
Move assignment operator.
Iterator on the arcs of a graph.
Graph implemented with double-linked adjacency lists.
Arc * get_first_arc(Node *node) const
Return any arc adjacent to a node.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
void swap(List_Graph &g) noexcept
Swap in constant time this with g
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
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_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
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.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynArray< Graph::Arc * > arcs
#define ARC_COUNTER(p)
Return the counter of arc p.
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
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.