156 template <
class GT,
class SA = Dft_Show_Arc<GT>>
193 auto idx =
static_cast<size_t>(
raw_idx);
196 if (idx <
arcs.size() - 1)
215 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
254 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
256 auto p = it.get_curr();
257 auto q =
kg.insert_node();
265 auto a = it.get_curr();
268 auto ka =
kg.insert_arc(s, t, a);
286 auto pa = it.get_curr();
287 auto tgt = it.get_tgt_node_ne();
293 auto ka =
kg.insert_arc(
cp, tgt,
pa->get_info());
304 arcs.insert(it.get_curr());
321 "Arc index and graph arc count must be in sync");
324 auto s =
kg.get_src_node(a);
325 auto t =
kg.get_tgt_node(a);
330 auto cp =
kg.insert_node();
335 cp->get_info().
swap(s->get_info());
336 cp->get_info().
append(t->get_info());
359 auto min_cut = std::numeric_limits<size_t>::max();
364 for (
size_t i = 0; i <
num_iter; ++i)
380 auto ka =
kg.get_first_arc();
385 vs.
swap(
kg.get_src_node(
ka)->get_info());
396 auto ka = it.get_curr();
402 auto ka =
kg.get_first_arc();
403 auto S =
kg.get_src_node(
ka);
404 auto T =
kg.get_tgt_node(
ka);
406 assert(S->get_info().size() +
T->get_info().size() ==
411 vs.
swap(S->get_info());
421 const size_t n =
kg.get_num_nodes();
427 return kg.get_num_arcs();
431 const auto t =
static_cast<size_t>(std::ceil(1.0 + n / std::sqrt(2.0)));
534 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
540 num_iter =
static_cast<size_t>(1.05 * n * n);
543 size_t min_cut = std::numeric_limits<size_t>::max();
548 for (
size_t i = 0; i <
num_iter; ++i)
580 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
601 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
604 const auto num_iter =
static_cast<size_t>(1.05 * n * n);
630 assert(
r !=
nullptr &&
"Cannot use moved-from Karger_Min_Cut object");
642 size_t best_cut = std::numeric_limits<size_t>::max();
644 for (
size_t i = 0; i <
num_iter; ++i)
659 cut.
append(it.get_curr()->get_info());
661 auto ka =
kg.get_first_arc();
662 auto S =
kg.get_src_node(
ka);
663 auto T =
kg.get_tgt_node(
ka);
667 vs.
swap(S->get_info());
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Stateful depth-first traversal functor.
bool has_curr() const noexcept
Return true if the iterator has current item.
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
T & append(const T &item)
Append a new item by copy.
T remove()
Remove the first item of the list.
void empty() noexcept
empty the list
DynList & swap(DynList &l) noexcept
Swap this with l.
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< int > Node
The graph type.
Graph_Arc< int > 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.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.