55#include <gtest/gtest.h>
74 long long min_cut(
const Spec & s,
const std::vector<long long> &
w)
78 long long best = std::numeric_limits<long long>::max();
80 for (
unsigned long side = 0; side < (1UL << (s.
n - 1)); ++side)
82 if (side == (1UL << (s.
n - 1)) - 1)
84 auto first = [&] (
int x) {
return x == 0 or ((side >> (x - 1)) & 1UL) != 0; };
86 for (
size_t i = 0; i < s.
arcs.size(); ++i)
87 if (first(s.
arcs[i][0]) != first(s.
arcs[i][1]))
88 total +=
w.empty() ? 1 :
w[i];
89 best = std::min(best, total);
98 std::string cut_error(
const Spec & s,
const std::vector<long long> &
w,
103 std::vector<int> side(
size_t(s.
n), -1);
104 for (
int k = 0;
k < 2; ++
k)
105 for (
auto it = (
k == 0 ? vs : vt).get_it(); it.has_curr(); it.next_ne())
107 const int x =
num(it.get_curr());
108 if (x < 0 or x >= s.
n or side[
size_t(x)] != -1)
109 return "a node is in both sides, or twice";
112 if (std::count(side.begin(), side.end(), -1) > 0 or vs.
is_empty() or vt.
is_empty())
113 return "the sides do not split the nodes in two";
114 std::multiset<int> want, got;
116 for (
size_t i = 0; i < s.
arcs.size(); ++i)
117 if (side[
size_t(s.
arcs[i][0])] != side[
size_t(s.
arcs[i][1])])
120 total +=
w.empty() ? 1 :
w[i];
122 for (
auto it = cut.
get_it(); it.has_curr(); it.next_ne())
123 got.insert(
num(it.get_curr()));
125 return "the arcs of the cut are not those between the sides";
127 return "it says " + std::to_string(
value) +
" for a cut of " + std::to_string(total);
134 void for_each_weighting(
Spec s,
long long lo,
long long hi,
F && f)
136 s.
w.assign(s.
arcs.size(), lo);
139 f(
static_cast<const Spec &
>(s));
141 while (i < s.
w.size()
and s.
w[i] == hi)
157 using GT = TypeParam;
159 Check cut(
"Stoer_Wagner_Min_Cut");
160 Check weight(
"Stoer_Wagner_Min_Cut::min_cut_weight()");
161 Check few(
"Stoer_Wagner_Min_Cut on less than two nodes");
164 auto run = [&] (
const Spec & s)
170 const long long want =
min_cut(s, s.
w);
179 (void) sw(b.
g, vs, vt,
arcs);
180 few.
fail(s,
"cuts a graph of less than two nodes");
182 catch (
const std::domain_error &) {}
184 few.
fail(s,
"min_cut_weight() is not 0");
191 const long long got = sw(b.
g, vs, vt,
arcs);
192 if (
auto e = cut_error<GT>(s, s.
w, vs, vt,
arcs, got); not e.empty())
194 else if (got != want)
195 cut.
fail(s,
"a cut of " + std::to_string(got) +
", not " + std::to_string(want));
199 weight.
fail(s, std::to_string(
w) +
", not " + std::to_string(want));
203 for_each_weighting(spec, 0, 2, run);
207 for_each_weighting(spec, 1, 1, run);
219 bool karger_takes(
const Spec & s)
227 Check rejects{
"Karger_Min_Cut, what it does not take"};
228 Check plain{
"Karger_Min_Cut::operator()"};
229 Check many{
"Karger_Min_Cut::operator(), many iterations"};
230 Check size{
"Karger_Min_Cut::compute_min_cut_size()"};
231 Check fast{
"Karger_Min_Cut::fast()"};
235 for (
const Check * c : {&rejects, &plain, &many, &
size, &fast})
241 void check_karger(Karger_Checks & c,
const Spec & s)
247 const std::vector<long long> unit;
249 if (not karger_takes(s))
256 (void) karger(b.g, vs, vt, cut);
257 c.rejects.fail(s,
"cuts a graph that it does not take");
259 catch (
const std::domain_error &) {}
263 const long long want =
min_cut(s, unit);
270 const long long got =
static_cast<long long>(karger(b.g, vs, vt, cut));
271 if (
auto e = cut_error<GT>(s, unit, vs, vt, cut, got); not e.empty())
274 c.plain.fail(s,
"a cut of " + std::to_string(got) +
" under the minimum");
280 const size_t iterations = 12 * size_t(s.
n) * size_t(s.
n);
285 const long long got =
static_cast<long long>(karger(b.g, vs, vt, cut, iterations));
286 if (
auto e = cut_error<GT>(s, unit, vs, vt, cut, got); not e.empty())
288 else if (got != want)
289 c.many.fail(s,
"a cut of " + std::to_string(got) +
", not " + std::to_string(want));
293 if (
const long long got =
static_cast<long long>(karger.compute_min_cut_size(b.g, iterations)); got != want)
294 c.size.fail(s, std::to_string(got) +
", not " + std::to_string(want));
300 const long long got =
static_cast<long long>(karger.fast(b.g, vs, vt, cut, 40));
301 if (
auto e = cut_error<GT>(s, unit, vs, vt, cut, got); not e.empty())
303 else if (got != want)
304 c.fast.fail(s,
"a cut of " + std::to_string(got) +
", not " + std::to_string(want));
320 const int graphs =
heavy() ? 1000 : 60;
321 for (
int k = 0;
k < graphs; ++
k)
323 const int n = 5 + int(
rng() % 4);
325 check_karger<TypeParam>(c, s);
Karger's randomized min-cut algorithm.
Stoer-Wagner deterministic min-cut algorithm.
WeightedDigraph::Node Node
size_t size_t int32_t value
Doubly-linked list (defined in tpl_dynList.H).
constexpr bool is_empty() const noexcept
Karger's randomized minimum cut algorithm.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Stoer-Wagner deterministic minimum cut algorithm.
Weight min_cut_weight(GT &g)
Find only the minimum cut weight (faster, no partition info).
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
TYPED_TEST_SUITE(ExhaustiveCut, Undirected_Kinds)
TYPED_TEST(ExhaustiveCut, StoerWagnerFindsAMinimumCut)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
Answers computed from a Spec of graph_harness.H, without Aleph.
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
void expect(std::ifstream &input_stream, const std::string &expected)
Expect and consume a specific string/keyword.
and
Check uniqueness with explicit hash + equality functors.
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.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
Spec random_spec(std::mt19937 &rng, int n, int m, bool directed, bool loops=true, bool parallel=true)
A random spec of n nodes and m arcs, with loops and parallel arcs if asked; parallel is ignored when ...
int num(Node *p)
The number of a node of a built graph.
Domain domain(int max_nodes, int max_arcs)
The domain of max_nodes nodes and max_arcs arcs, of one more of each in heavy mode,...
bool heavy()
Whether the mode is heavy, which asks for the larger domains.
int components(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of components, without the node skip_node and the arc skip_arc.
The weight of an arc, read by its number from a vector.
A graph made from a spec, with its nodes and arcs by number.
A small multigraph: its nodes are 0 .
std::vector< long long > w
std::vector< std::array< int, 2 > > arcs
bool has_loop() const noexcept
Generic graph and digraph implementations.