105# ifndef STOER_WAGNER_H
106# define STOER_WAGNER_H
113# include <functional>
202 template <AlephGraph
GT,
237 std::vector<std::vector<Weight>>
adj;
245 adj.assign(n, std::vector<Weight>(n,
Weight(0)));
251 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
253 auto p = it.get_curr();
255 nodes[id].members.append(p);
257 nodes[id].merged =
false;
264 auto a = it.get_curr();
277 const size_t n =
nodes.size();
281 while (start < n &&
nodes[start].merged)
288 std::vector<Weight> key(n,
Weight(0));
289 std::vector<bool>
in_A(n,
false);
292 std::priority_queue<PQEntry>
pq;
300 for (
size_t j = 0; j < n; ++j)
304 key[j] =
adj[start][j];
305 pq.push({j, key[j]});
319 if (!
in_A[top.node_id] && !
nodes[top.node_id].merged &&
320 top.priority == key[top.node_id])
330 for (
size_t j = 0; j < n; ++j)
349 for (
size_t j = 0; j < n; ++j)
354 pq.push({j, key[j]});
368 const size_t n =
nodes.size();
372 nodes[t].merged =
true;
375 for (
size_t j = 0; j < n; ++j)
377 if (j != s && j != t && !
nodes[j].merged)
385 for (
size_t j = 0; j < n; ++j)
468 for (
auto p :
nodes[t].members)
479 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
481 auto p = it.get_curr();
489 auto a = it.get_curr();
543 template <AlephGraph GT>
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.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Default distance accessor for arc weights.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
void empty() noexcept
empty the list
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Stoer-Wagner deterministic minimum cut algorithm.
std::vector< std::vector< Weight > > adj
Weight operator()(GT &g, DynList< Node * > &vs, DynList< Node * > &vt, DynList< Arc * > &cut)
Find minimum cut in graph.
void merge_nodes(size_t s, size_t t)
std::tuple< size_t, size_t, Weight > minimum_cut_phase()
std::vector< SWNode > nodes
Stoer_Wagner_Min_Cut()=default
Default constructor.
Stoer_Wagner_Min_Cut(Distance _distance)
Constructor with custom distance functor.
typename Distance::Distance_Type Weight
Weight min_cut_weight(GT &g)
Find only the minimum cut weight (faster, no partition info).
void init_from_graph(GT &g)
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
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.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
#define NODE_COUNTER(p)
Get the counter of a node.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Filtered iterator on all the arcs of a graph.
Default filter for filtered iterators on arcs.
bool operator<(const PQEntry &other) const
DynList< Node * > members
Unit weight functor for unweighted graphs.
size_t operator()(typename GT::Arc *) const
Lazy and scalable dynamic array implementation.
Utility algorithms and operations for graphs.
Simple graph implementation with adjacency lists.