Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA > Class Template Reference

Stoer-Wagner deterministic minimum cut algorithm. More...

#include <Stoer_Wagner.H>

Collaboration diagram for Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >:
[legend]

Classes

struct  PQEntry
 
struct  SWNode
 

Public Types

using Node = typename GT::Node
 
using Arc = typename GT::Arc
 
using Weight = typename Distance::Distance_Type
 

Public Member Functions

 Stoer_Wagner_Min_Cut ()=default
 Default constructor.
 
 Stoer_Wagner_Min_Cut (Distance _distance)
 Constructor with custom distance functor.
 
Weight operator() (GT &g, DynList< Node * > &vs, DynList< Node * > &vt, DynList< Arc * > &cut)
 Find minimum cut in graph.
 
Weight min_cut_weight (GT &g)
 Find only the minimum cut weight (faster, no partition info).
 

Private Member Functions

void init_from_graph (GT &g)
 
std::tuple< size_t, size_t, Weight > minimum_cut_phase ()
 
void merge_nodes (size_t s, size_t t)
 

Private Attributes

Distance distance
 
SA sa
 
std::vector< std::vector< Weight > > adj
 
std::vector< SWNode > nodes
 
size_t active_count
 

Detailed Description

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >

Stoer-Wagner deterministic minimum cut algorithm.

This class implements the Stoer-Wagner algorithm for finding the global minimum cut in an undirected weighted graph. Unlike randomized algorithms (Karger, Karger-Stein), Stoer-Wagner is deterministic and always finds the exact minimum cut.

Algorithm Overview

The algorithm is based on the observation that for any two vertices s and t, the global minimum cut is either:

  • The minimum s-t cut, OR
  • The minimum cut of the graph with s and t merged

It uses Maximum Adjacency Search to efficiently find minimum s-t cuts.

Template Parameters

  • GT: Graph type (must be based on List_Graph)
  • Distance: Functor to extract edge weight. Must provide Weight operator()(Arc*). Defaults to extracting arc->get_info() as the weight.
  • SA: Arc filter functor. Defaults to showing all arcs.

Weight Type

The algorithm works with any numeric weight type that supports:

  • Addition (+)
  • Comparison (<)
  • Assignment (=)
  • Zero initialization

Example Usage

#include <Stoer_Wagner.H>
#include <tpl_graph.H>
GT g;
// Build weighted graph
auto a = g.insert_node("A");
auto b = g.insert_node("B");
auto c = g.insert_node("C");
auto d = g.insert_node("D");
g.insert_arc(a, b, 2); // Weight 2
g.insert_arc(b, c, 3);
g.insert_arc(c, d, 4);
g.insert_arc(d, a, 1);
g.insert_arc(a, c, 5);
// Find minimum cut
int cut_weight = solver(g, S, T, cut_arcs);
cout << "Min-cut weight: " << cut_weight << endl;
Stoer-Wagner deterministic min-cut algorithm.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:525
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:605
Stoer-Wagner deterministic minimum cut algorithm.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:223
Generic graph and digraph implementations.
Example: Unweighted graph (all edges have weight 1)
// For unweighted graphs, use a constant distance functor
struct UnitWeight {
int operator()(GT::Arc*) const { return 1; }
};
int cut_size = solver(g, S, T, cut_arcs);
See also
Karger_Min_Cut Randomized algorithm (simpler, slower)
Karger_Stein_Min_Cut Faster randomized algorithm
https://en.wikipedia.org/wiki/Stoer%E2%80%93Wagner_algorithm

Definition at line 205 of file Stoer_Wagner.H.

Member Typedef Documentation

◆ Arc

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::Arc = typename GT::Arc

Definition at line 209 of file Stoer_Wagner.H.

◆ Node

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::Node = typename GT::Node

Definition at line 208 of file Stoer_Wagner.H.

◆ Weight

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::Weight = typename Distance::Distance_Type

Definition at line 210 of file Stoer_Wagner.H.

Constructor & Destructor Documentation

◆ Stoer_Wagner_Min_Cut() [1/2]

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::Stoer_Wagner_Min_Cut ( )
default

Default constructor.

◆ Stoer_Wagner_Min_Cut() [2/2]

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::Stoer_Wagner_Min_Cut ( Distance  _distance)
inlineexplicit

Constructor with custom distance functor.

Parameters
_distanceFunctor to extract edge weights

Definition at line 401 of file Stoer_Wagner.H.

Member Function Documentation

◆ init_from_graph()

◆ merge_nodes()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::merge_nodes ( size_t  s,
size_t  t 
)
inlineprivate

◆ min_cut_weight()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Weight Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::min_cut_weight ( GT &  g)
inline

Find only the minimum cut weight (faster, no partition info).

If you only need the cut weight and not the actual partition, this method is slightly more efficient.

Parameters
[in]gInput graph
Returns
Weight of the minimum cut

Definition at line 511 of file Stoer_Wagner.H.

References Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::active_count, Aleph::blossom_maximum_cardinality_matching(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::init_from_graph(), Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::merge_nodes(), and Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::minimum_cut_phase().

Referenced by example_api_patterns(), TEST(), TEST(), TEST(), TEST(), and test_sw_min_cut_weight_only().

◆ minimum_cut_phase()

◆ operator()()

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Weight Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::operator() ( GT &  g,
DynList< Node * > &  vs,
DynList< Node * > &  vt,
DynList< Arc * > &  cut 
)
inline

Find minimum cut in graph.

Executes the Stoer-Wagner algorithm to find the exact minimum cut. For weighted graphs, returns the minimum total weight of cut edges. For unweighted graphs (weight=1), returns the minimum number of edges.

Parameters
[in]gInput graph (must have at least 2 nodes)
[out]vsFirst partition of vertices
[out]vtSecond partition of vertices
[out]cutArcs crossing the cut
Returns
Weight of the minimum cut
Exceptions
std::domain_errorif graph has fewer than 2 nodes
Note
Time complexity: O(nm + n² log n) with binary heap
Space complexity: O(n²) for adjacency matrix

Definition at line 422 of file Stoer_Wagner.H.

References Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::active_count, ah_domain_error_if, Aleph::DynList< T >::append(), Aleph::blossom_maximum_cardinality_matching(), Aleph::DynList< T >::empty(), FunctionalMethods< Container, T >::exists(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::init_from_graph(), Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::merge_nodes(), Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::minimum_cut_phase(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::nodes, and Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::sa.

Member Data Documentation

◆ active_count

◆ adj

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
std::vector<std::vector<Weight> > Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::adj
private

◆ distance

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Distance Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::distance
private

◆ nodes

◆ sa

template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
SA Aleph::Stoer_Wagner_Min_Cut< GT, Distance, SA >::sa
private

The documentation for this class was generated from the following file: