|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Karger's randomized minimum cut algorithm. More...
#include <Karger.H>
Classes | |
| class | ArcIndex |
| Arc index with O(1) operations and deterministic ordering. More... | |
Public Member Functions | |
| Karger_Min_Cut (const unsigned long _seed=time(nullptr), SA _sa=SA()) | |
| Construct a Karger minimum cut solver. | |
| Karger_Min_Cut (const Karger_Min_Cut &)=delete | |
| Disable copy constructor (class owns its generator) | |
| Karger_Min_Cut & | operator= (const Karger_Min_Cut &)=delete |
| Disable copy assignment (class owns its generator) | |
| 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. | |
| unsigned long | get_seed () const noexcept |
| Get the random seed used by this solver (useful for reproducibility). | |
| void | reseed (const unsigned long new_seed) noexcept |
| Change the random seed. | |
| size_t | compute_min_cut_size (GT &g, size_t num_iter=0) |
| Compute only the minimum cut size (without partition info). | |
| 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. | |
| size_t | operator() (GT &g, DynList< typename GT::Node * > &vs, DynList< typename GT::Node * > &vt, DynList< typename GT::Arc * > &cut) |
| Compute minimum cut with default number of iterations. | |
| 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. | |
Private Types | |
| using | Knode = Graph_Node< DynList< typename GT::Node * > > |
| using | Karc = Graph_Arc< typename GT::Arc * > |
| using | Kgraph = List_Graph< Knode, Karc > |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
Private Member Functions | |
| bool | has_self_loop (const GT &g) const |
| bool | is_connected_filtered (const GT &g) const |
| void | validate_graph (GT &g) const |
| void | build_kgraph (GT &g, Kgraph &kg, ArcIndex &arcs) |
| Build the contracted graph representation from the original graph. | |
| void | update_arcs (Kgraph &kg, Knode *p, Knode *t, Knode *cp, ArcIndex &arcs) |
| Update arcs when contracting nodes p and t into cp. | |
| void | rebuild_arc_index (Kgraph &kg, ArcIndex &arcs) |
| Rebuild the arc index from scratch for the given graph. | |
| 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, and with the draw inline GCC stops inlining the contraction and the removal of its nodes, which cost more than the call. | |
| void | contract (Kgraph &kg, size_t left_num_nodes, ArcIndex &arcs) |
| Contract the graph by randomly merging edges until left_num_nodes remain. | |
| 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. | |
| size_t | __fast_karger_min_cut (Kgraph &kg, ArcIndex &arcs) |
| Karger-Stein fast minimum cut algorithm (recursive version). | |
Private Attributes | |
| unsigned long | seed |
| Gsl_Compatible_Rng_Owner | r |
| SA | sa |
Karger's randomized minimum cut algorithm.
This class implements Karger's algorithm for finding a minimum cut in an undirected graph. A minimum cut is a partition of the graph's vertices into two non-empty sets such that the number of edges crossing the partition is minimized.
The algorithm works by repeatedly contracting randomly selected edges until only two "super-nodes" remain. The edges between these super-nodes form a cut. By running multiple iterations, the algorithm finds the minimum cut with high probability.
Template parameters:
bool operator()(Arc*) returning true if the arc should be included. Defaults to Dft_Show_Arc<GT> which includes all arcs.Time complexity:
Features:
gsl_rng_mt19937 of GSL, so that a seed gives the cuts that it gave when they came from GSL
|
private |
|
private |
|
private |
|
private |
|
private |
|
inline |
Construct a Karger minimum cut solver.
| [in] | _seed | Random seed for the algorithm. Defaults to current time. Using the same seed produces reproducible results. |
| [in] | _sa | Arc filter to determine which arcs to consider. Defaults to Dft_Show_Arc<GT> which includes all arcs. |
| std::bad_alloc | if there is not enough memory for the generator. |
|
delete |
Disable copy constructor (class owns its generator)
|
inlinenoexcept |
Move constructor.
Transfers ownership of the random number generator, once the filter is moved: if its move throws, other keeps its generator.
Definition at line 502 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::r, and Aleph::Gsl_Compatible_Rng_Owner::swap().
|
inlineprivate |
Karger-Stein fast minimum cut algorithm (recursive version).
Time complexity: O(n^2 log^3 n)
Definition at line 435 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut(), arcs, Aleph::Karger_Min_Cut< GT, SA >::contract(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Karger_Min_Cut< GT, SA >::rebuild_arc_index(), and Aleph::List_Graph< _Graph_Node, _Graph_Arc >::swap().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut(), and Aleph::Karger_Min_Cut< GT, SA >::fast().
|
inlineprivate |
Build the contracted graph representation from the original graph.
Each node in kg contains a list of original nodes it represents.
| [in] | g | The original graph. |
| [out] | kg | The contracted graph (cleared and rebuilt). |
| [out] | arcs | Index of arcs for O(1) random selection. |
Definition at line 254 of file Karger.H.
References arcs, Aleph::clear_graph(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), GraphCommon< GT, Node, Arc >::map_nodes(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), GraphCommon< GT, Node, Arc >::reset_arcs(), GraphCommon< GT, Node, Arc >::reset_nodes(), and Aleph::Karger_Min_Cut< GT, SA >::sa.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::compute_min_cut_size(), Aleph::Karger_Min_Cut< GT, SA >::fast(), and Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut().
|
inline |
Compute only the minimum cut size (without partition info).
This is more efficient when you only need the cut size, not the actual partition or cut edges.
| [in] | g | The undirected graph. |
| [in] | num_iter | Number of iterations to run. |
| std::domain_error | if graph is disconnected, contains self-loops, has fewer than 2 nodes, or has no arcs. |
Definition at line 547 of file Karger.H.
References arcs, Aleph::Karger_Min_Cut< GT, SA >::build_kgraph(), Aleph::Karger_Min_Cut< GT, SA >::contract(), Aleph::Gsl_Compatible_Rng_Owner::get(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::min_cut(), Aleph::Karger_Min_Cut< GT, SA >::r, and Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().
|
inlineprivate |
Contract the graph by randomly merging edges until left_num_nodes remain.
This is the core of Karger's algorithm. Each iteration:
| [in,out] | kg | The graph to contract. |
| [in] | left_num_nodes | Stop when this many nodes remain. |
| [in,out] | arcs | Index of arcs for O(1) random selection. |
Definition at line 331 of file Karger.H.
References arcs, Aleph::Karger_Min_Cut< GT, SA >::draw_arc(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::remove_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::remove_node(), and Aleph::Karger_Min_Cut< GT, SA >::update_arcs().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut(), Aleph::Karger_Min_Cut< GT, SA >::compute_min_cut_size(), and Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut().
|
inlineprivate |
The position of an arc among num_arcs, drawn out of line: a contraction takes much longer than a draw, and with the draw inline GCC stops inlining the contraction and the removal of its nodes, which cost more than the call.
Definition at line 318 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::r, and Aleph::Gsl_Compatible_Rng::uniform_int().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::contract().
|
inline |
Compute minimum cut using Karger-Stein fast algorithm.
This version uses the recursive Karger-Stein algorithm which has better time complexity: O(n^2 log^3 n) vs O(n^4) for the standard.
The algorithm is run O(log^2 n) times to achieve high probability of finding the true minimum cut.
| [in] | g | The undirected graph. |
| [out] | vs | Nodes in the first partition (S). |
| [out] | vt | Nodes in the second partition (T). |
| [out] | cut | Arcs crossing the minimum cut. |
| [in] | num_iter | Number of iterations (default: ceil(log^2 n)). |
| std::domain_error | if graph is disconnected, contains self-loops, has fewer than 2 nodes, or has no arcs. |
Definition at line 639 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut(), Aleph::DynList< T >::append(), arcs, Aleph::Karger_Min_Cut< GT, SA >::build_kgraph(), Aleph::DynList< T >::empty(), Aleph::Gsl_Compatible_Rng_Owner::get(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::get_first_arc(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Dlink::Iterator::has_curr(), Aleph::Karger_Min_Cut< GT, SA >::r, Aleph::DynList< T >::swap(), and Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().
|
inlinenoexcept |
Get the random seed used by this solver (useful for reproducibility).
Definition at line 526 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::seed.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().
|
inlineprivate |
Definition at line 220 of file Karger.H.
References GraphCommon< GT, Node, Arc >::get_src_node(), and GraphCommon< GT, Node, Arc >::get_tgt_node().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
|
inlineprivate |
Definition at line 228 of file Karger.H.
References GraphCommon< GT, Node, Arc >::get_num_nodes(), and Aleph::Karger_Min_Cut< GT, SA >::sa.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
|
inlineprivate |
Internal implementation of Karger's minimum cut algorithm.
Runs num_iter independent contractions and returns the best cut found.
| [in] | g | The original undirected graph. |
| [out] | vs | Nodes in partition S of the minimum cut. |
| [out] | vt | Nodes in partition T of the minimum cut. |
| [out] | cut | Arcs crossing the minimum cut. |
| [in] | num_iter | Number of independent iterations to run. |
Definition at line 367 of file Karger.H.
References Aleph::DynList< T >::append(), arcs, Aleph::Karger_Min_Cut< GT, SA >::build_kgraph(), Aleph::Karger_Min_Cut< GT, SA >::contract(), Aleph::DynList< T >::empty(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::get_first_arc(), GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Dlink::Iterator::has_curr(), Aleph::min_cut(), Aleph::DynList< T >::swap(), and Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::operator()(), and Aleph::Karger_Min_Cut< GT, SA >::operator()().
|
inline |
Compute minimum cut with default number of iterations.
Uses approximately n^2 iterations where n is the number of nodes, which gives a high probability of finding the true minimum cut.
| [in] | g | The undirected graph. |
| [out] | vs | Nodes in the first partition (S). |
| [out] | vt | Nodes in the second partition (T). |
| [out] | cut | Arcs crossing the minimum cut. |
| std::domain_error | if graph is disconnected, contains self-loops, has fewer than 2 nodes, or has no arcs. |
Definition at line 611 of file Karger.H.
References Aleph::Gsl_Compatible_Rng_Owner::get(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut(), and Aleph::Karger_Min_Cut< GT, SA >::r.
|
inline |
Compute a minimum cut with a specified number of iterations.
| [in] | g | The undirected graph. |
| [out] | vs | Nodes in the first partition (S). |
| [out] | vt | Nodes in the second partition (T). |
| [out] | cut | Arcs crossing the minimum cut. |
| [in] | num_iter | Number of iterations to run. |
| std::domain_error | if the graph is disconnected, contains self-loops, has fewer than 2 nodes, or has no arcs. |
Definition at line 589 of file Karger.H.
References Aleph::Gsl_Compatible_Rng_Owner::get(), Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut(), and Aleph::Karger_Min_Cut< GT, SA >::r.
|
delete |
Disable copy assignment (class owns its generator)
|
inlinenoexcept |
Move assignment operator.
Transfers ownership of the random number generator. Only for a filter that can be assigned; if its move throws, nothing changes.
Definition at line 512 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::r, Aleph::Karger_Min_Cut< GT, SA >::sa, and Aleph::Karger_Min_Cut< GT, SA >::seed.
|
inlineprivate |
Rebuild the arc index from scratch for the given graph.
Used after copying a graph to ensure pointers point to the copy's arcs.
Definition at line 307 of file Karger.H.
References arcs, and Aleph::Dlink::Iterator::has_curr().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut().
|
inlinenoexcept |
Change the random seed.
Useful for running multiple independent trials.
| [in] | new_seed | The new seed value. |
Definition at line 530 of file Karger.H.
References Aleph::Gsl_Compatible_Rng_Owner::get(), Aleph::Gsl_Compatible_Rng::max(), Aleph::Karger_Min_Cut< GT, SA >::r, Aleph::Gsl_Compatible_Rng::seed(), and Aleph::Karger_Min_Cut< GT, SA >::seed.
Referenced by TEST(), TEST(), and TYPED_TEST().
|
inlineprivate |
Update arcs when contracting nodes p and t into cp.
Moves all arcs from node p to the new contracted node cp, except arcs that connect to t (which become self-loops and are removed).
| [in,out] | kg | The contracted graph. |
| [in] | p | Source node being contracted. |
| [in] | t | Target node being contracted (arcs to t are skipped). |
| [in] | cp | New contracted node that replaces p and t. |
| [in,out] | arcs | Index of arcs (updated to reflect changes). |
Definition at line 288 of file Karger.H.
References arcs, GTNodeCommon< NodeInfo >::get_info(), Aleph::Dlink::Iterator::has_curr(), and Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::contract().
|
inlineprivate |
Definition at line 237 of file Karger.H.
References ah_domain_error_if, GraphCommon< GT, Node, Arc >::get_num_arcs(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Karger_Min_Cut< GT, SA >::has_self_loop(), and Aleph::Karger_Min_Cut< GT, SA >::is_connected_filtered().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::compute_min_cut_size(), Aleph::Karger_Min_Cut< GT, SA >::fast(), and Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut().
|
private |
Definition at line 246 of file Karger.H.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::Karger_Min_Cut(), Aleph::Karger_Min_Cut< GT, SA >::compute_min_cut_size(), Aleph::Karger_Min_Cut< GT, SA >::draw_arc(), Aleph::Karger_Min_Cut< GT, SA >::fast(), Aleph::Karger_Min_Cut< GT, SA >::operator()(), Aleph::Karger_Min_Cut< GT, SA >::operator()(), Aleph::Karger_Min_Cut< GT, SA >::operator=(), and Aleph::Karger_Min_Cut< GT, SA >::reseed().
|
private |
Definition at line 247 of file Karger.H.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::build_kgraph(), Aleph::Karger_Min_Cut< GT, SA >::is_connected_filtered(), and Aleph::Karger_Min_Cut< GT, SA >::operator=().
|
private |
Definition at line 245 of file Karger.H.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::get_seed(), Aleph::Karger_Min_Cut< GT, SA >::operator=(), and Aleph::Karger_Min_Cut< GT, SA >::reseed().