|
Aleph-w 3.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 () | |
| Destructor. Frees the random number generator (if not moved). | |
| Karger_Min_Cut (const Karger_Min_Cut &)=delete | |
| Disable copy constructor (class owns gsl_rng pointer) | |
| Karger_Min_Cut & | operator= (const Karger_Min_Cut &)=delete |
| Disable copy assignment (class owns gsl_rng pointer) | |
| Karger_Min_Cut (Karger_Min_Cut &&other) noexcept | |
| Move constructor. Transfers ownership of the random number generator. | |
| Karger_Min_Cut & | operator= (Karger_Min_Cut &&other) noexcept |
| Move assignment operator. Transfers ownership of the random number generator. | |
| 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. | |
| 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_rng * | 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:
|
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. |
Definition at line 469 of file Karger.H.
References Aleph::maps(), Aleph::Karger_Min_Cut< GT, SA >::r, and Aleph::Karger_Min_Cut< GT, SA >::seed.
|
inline |
Destructor. Frees the random number generator (if not moved).
Definition at line 476 of file Karger.H.
References Aleph::maps(), and Aleph::Karger_Min_Cut< GT, SA >::r.
|
delete |
Disable copy constructor (class owns gsl_rng pointer)
|
inlinenoexcept |
Move constructor. Transfers ownership of the random number generator.
Definition at line 489 of file Karger.H.
References Aleph::maps().
|
inlineprivate |
Karger-Stein fast minimum cut algorithm (recursive version).
Time complexity: O(n^2 log^3 n)
Definition at line 419 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut(), arcs, Aleph::Karger_Min_Cut< GT, SA >::contract(), Aleph::maps(), Aleph::Karger_Min_Cut< GT, SA >::rebuild_arc_index(), and Aleph::DynList< T >::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 247 of file Karger.H.
References Aleph::DynList< T >::append(), arcs, Aleph::clear_graph(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::DynList< T >::insert(), GraphCommon< GT, Node, Arc >::map_nodes(), Aleph::maps(), 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().
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 532 of file Karger.H.
References arcs, Aleph::Karger_Min_Cut< GT, SA >::build_kgraph(), Aleph::Karger_Min_Cut< GT, SA >::contract(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::maps(), Aleph::min_cut(), Aleph::Karger_Min_Cut< GT, SA >::r, and Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
|
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 315 of file Karger.H.
References Aleph::DynList< T >::append(), arcs, Aleph::maps(), Aleph::Karger_Min_Cut< GT, SA >::r, Aleph::DynList< T >::remove(), Aleph::DynList< T >::swap(), 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().
|
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 624 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(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Dlink::Iterator::has_curr(), Aleph::maps(), Aleph::Karger_Min_Cut< GT, SA >::r, Aleph::DynList< T >::swap(), and Aleph::Karger_Min_Cut< GT, SA >::validate_graph().
|
inlinenoexcept |
Get the random seed used by this solver (useful for reproducibility).
Definition at line 511 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::seed.
Definition at line 213 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().
Definition at line 221 of file Karger.H.
References GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::maps(), 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 351 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(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Dlink::Iterator::has_curr(), Aleph::maps(), 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 596 of file Karger.H.
References GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut(), Aleph::maps(), 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 574 of file Karger.H.
References Aleph::Karger_Min_Cut< GT, SA >::karger_min_cut(), Aleph::maps(), and Aleph::Karger_Min_Cut< GT, SA >::r.
|
delete |
Disable copy assignment (class owns gsl_rng pointer)
|
inlinenoexcept |
Move assignment operator. Transfers ownership of the random number generator.
Definition at line 496 of file Karger.H.
References Aleph::maps(), 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 300 of file Karger.H.
References arcs, Aleph::Dlink::Iterator::has_curr(), and Aleph::maps().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::__fast_karger_min_cut().
Change the random seed.
Useful for running multiple independent trials.
| [in] | new_seed | The new seed value. |
Definition at line 515 of file Karger.H.
References Aleph::maps(), Aleph::Karger_Min_Cut< GT, SA >::r, and Aleph::Karger_Min_Cut< GT, SA >::seed.
|
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 281 of file Karger.H.
References arcs, Aleph::Dlink::Iterator::has_curr(), Aleph::DynList< T >::insert(), Aleph::maps(), and Aleph::DynList< T >::remove().
Referenced by Aleph::Karger_Min_Cut< GT, SA >::contract().
|
inlineprivate |
Definition at line 230 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(), Aleph::Karger_Min_Cut< GT, SA >::is_connected_filtered(), and Aleph::maps().
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 239 of file Karger.H.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::Karger_Min_Cut(), 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 >::contract(), 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().
Definition at line 240 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 238 of file Karger.H.
Referenced by Aleph::Karger_Min_Cut< GT, SA >::Karger_Min_Cut(), Aleph::Karger_Min_Cut< GT, SA >::get_seed(), Aleph::Karger_Min_Cut< GT, SA >::operator=(), and Aleph::Karger_Min_Cut< GT, SA >::reseed().