32#include <gtest/gtest.h>
92 vector<IntGraph::Node*> left(
k), right(
k);
94 for (
int i = 0; i <
k; ++i)
101 for (
int i = 0; i <
k; ++i)
102 for (
int j = i + 1; j <
k; ++j)
106 for (
int i = 0; i <
k; ++i)
107 for (
int j = i + 1; j <
k; ++j)
121 vector<IntGraph::Node*>
nodes(n);
123 for (
int i = 0; i < n; ++i)
126 for (
int i = 0; i < n - 1; ++i)
137 vector<IntGraph::Node*>
nodes(n);
139 for (
int i = 0; i < n; ++i)
142 for (
int i = 0; i < n; ++i)
150IntGraph create_complete_graph(
int n)
153 vector<IntGraph::Node*>
nodes(n);
155 for (
int i = 0; i < n; ++i)
158 for (
int i = 0; i < n; ++i)
159 for (
int j = i + 1; j < n; ++j)
172 for (
int i = 1; i < n; ++i)
183IntGraph create_two_clusters(
int cluster_size,
int bridge_count,
int weight = 1)
186 vector<IntGraph::Node*> left(cluster_size), right(cluster_size);
188 for (
int i = 0; i < cluster_size; ++i)
195 for (
int i = 0; i < cluster_size; ++i)
196 for (
int j = i + 1; j < cluster_size; ++j)
200 for (
int i = 0; i < cluster_size; ++i)
201 for (
int j = i + 1; j < cluster_size; ++j)
205 for (
int i = 0; i < bridge_count; ++i)
206 g.
insert_arc(left[i % cluster_size], right[i % cluster_size], weight);
233TEST(StoerWagnerConstruction, DefaultConstruction)
238TEST(StoerWagnerConstruction, WithCustomDistance)
242 using Distance_Type = int;
253TEST(StoerWagnerErrors, ThrowsOnSingleNode)
262 EXPECT_THROW(sw(g, vs, vt, cut), std::domain_error);
265TEST(StoerWagnerErrors, HandlesDisconnectedGraph)
282 int min_cut = sw(g, vs, vt, cut);
287TEST(StoerWagnerErrors, HandlesTwoNodesNoEdges)
297 int min_cut = sw(g, vs, vt, cut);
305TEST(StoerWagnerMinCut, TwoNodesOneEdge)
316 int min_cut = sw(g, vs, vt, cut);
319 EXPECT_EQ(cut.
size(), 1u);
320 EXPECT_EQ(vs.
size(), 1u);
321 EXPECT_EQ(vt.
size(), 1u);
332 int min_cut = sw(g, vs, vt, cut);
336 EXPECT_EQ(vs.
size() + vt.
size(), g.get_num_nodes());
341 auto g = create_square();
347 int min_cut = sw(g, vs, vt, cut);
353TEST(StoerWagnerMinCut, Barbell)
355 auto g = create_barbell(4);
361 int min_cut = sw(g, vs, vt, cut);
365 EXPECT_EQ(cut.
size(), 1u);
370 auto g = create_path(6);
376 int min_cut = sw(g, vs, vt, cut);
380 EXPECT_EQ(cut.
size(), 1u);
391 int min_cut = sw(g, vs, vt, cut);
395 EXPECT_EQ(cut.
size(), 2u);
400 auto g = create_star(6);
406 int min_cut = sw(g, vs, vt, cut);
416TEST(StoerWagnerMinCut, CompleteK3)
418 auto g = create_complete_graph(3);
424 int min_cut = sw(g, vs, vt, cut);
430TEST(StoerWagnerMinCut, CompleteK4)
432 auto g = create_complete_graph(4);
438 int min_cut = sw(g, vs, vt, cut);
444TEST(StoerWagnerMinCut, CompleteK5)
446 auto g = create_complete_graph(5);
452 int min_cut = sw(g, vs, vt, cut);
458TEST(StoerWagnerMinCut, CompleteK6)
460 auto g = create_complete_graph(6);
466 int min_cut = sw(g, vs, vt, cut);
476TEST(StoerWagnerWeighted, WeakMiddleEdge)
478 auto g = create_weighted_chain(10, 1, 10);
484 int min_cut = sw(g, vs, vt, cut);
490TEST(StoerWagnerWeighted, WeakFirstEdge)
492 auto g = create_weighted_chain(1, 10, 10);
498 int min_cut = sw(g, vs, vt, cut);
503TEST(StoerWagnerWeighted, WeakLastEdge)
505 auto g = create_weighted_chain(10, 10, 1);
511 int min_cut = sw(g, vs, vt, cut);
516TEST(StoerWagnerWeighted, TwoClustersWeakBridge)
518 auto g = create_two_clusters(4, 2, 1);
524 int min_cut = sw(g, vs, vt, cut);
528 EXPECT_EQ(cut.
size(), 2u);
531TEST(StoerWagnerWeighted, TwoClustersHeavyBridge)
533 auto g = create_two_clusters(4, 1, 50);
539 int min_cut = sw(g, vs, vt, cut);
543 EXPECT_EQ(cut.
size(), 1u);
546TEST(StoerWagnerWeighted, WeightedTriangle)
562 int min_cut = sw(g, vs, vt, cut);
574TEST(StoerWagnerPartitions, PartitionsCoverAllNodes)
576 auto g = create_barbell(5);
584 EXPECT_EQ(vs.
size() + vt.
size(), g.get_num_nodes());
587TEST(StoerWagnerPartitions, PartitionsNonEmpty)
589 auto g = create_complete_graph(5);
601TEST(StoerWagnerPartitions, NoOverlap)
612 for (
auto it = vs.
get_it(); it.has_curr(); it.next())
613 vs_set.
insert(it.get_curr());
615 for (
auto it = vt.
get_it(); it.has_curr(); it.next())
616 EXPECT_FALSE(vs_set.
has(it.get_curr()));
619TEST(StoerWagnerPartitions, CutEdgesCrossPartition)
621 auto g = create_barbell(4);
630 for (
auto it = vs.
get_it(); it.has_curr(); it.next())
631 vs_set.
insert(it.get_curr());
632 for (
auto it = vt.
get_it(); it.has_curr(); it.next())
633 vt_set.
insert(it.get_curr());
635 for (
auto it = cut.
get_it(); it.has_curr(); it.next())
637 auto arc = it.get_curr();
638 auto src = g.get_src_node(arc);
639 auto tgt = g.get_tgt_node(arc);
641 bool crosses = (vs_set.
has(src) && vt_set.
has(tgt)) ||
642 (vt_set.
has(src) && vs_set.
has(tgt));
643 EXPECT_TRUE(crosses);
651TEST(StoerWagnerWeightOnly, BasicUsage)
653 auto g = create_barbell(4);
658 EXPECT_EQ(weight, 1);
663 auto g = create_weighted_chain(5, 2, 8);
668 EXPECT_EQ(weight, 2);
671TEST(StoerWagnerWeightOnly, MatchesFullComputation)
673 auto g = create_complete_graph(6);
679 int full_result = sw(g, vs, vt, cut);
682 EXPECT_EQ(full_result, weight_only);
685TEST(StoerWagnerWeightOnly, SmallGraph)
695 EXPECT_EQ(weight, 0);
702TEST(StoerWagnerUnitWeight, IgnoresArcWeights)
717 size_t min_cut = sw(g, vs, vt, cut);
723TEST(StoerWagnerUnitWeight, CountsEdges)
725 auto g = create_complete_graph(5);
731 size_t min_cut = sw(g, vs, vt, cut);
741TEST(StoerWagnerCustomDistance, DoubleWeight)
745 using Distance_Type = int;
749 auto g = create_path(3);
755 int min_cut = sw(g, vs, vt, cut);
761TEST(StoerWagnerCustomDistance, ConstantWeight)
765 using Distance_Type = int;
775 int min_cut = sw(g, vs, vt, cut);
798TEST(StoerWagnerArcFilter, FiltersByWeight)
820 int min_cut = sw(g, vs, vt, cut);
831TEST(StoerWagnerGraphTypes, ListSGraph)
846 int min_cut = sw(g, vs, vt, cut);
851TEST(StoerWagnerGraphTypes, DoubleWeights)
872 double min_cut = sw(g, vs, vt, cut);
877 EXPECT_DOUBLE_EQ(
min_cut, 2.0);
884TEST(StoerWagnerEdgeCases, ZeroWeightEdge)
898 int min_cut = sw(g, vs, vt, cut);
904TEST(StoerWagnerEdgeCases, AllSameWeight)
906 auto g = create_complete_graph(4);
912 int min_cut = sw(g, vs, vt, cut);
918TEST(StoerWagnerEdgeCases, LargeWeights)
932 int min_cut = sw(g, vs, vt, cut);
941TEST(StoerWagnerPerformance, MediumGraph50Nodes)
945 vector<IntGraph::Node*>
nodes(
N);
947 for (
int i = 0; i <
N; ++i)
951 for (
int i = 0; i <
N - 1; ++i)
955 for (
int i = 0; i <
N; i += 5)
956 for (
int j = i + 10; j <
N; j += 10)
963 EXPECT_NO_THROW(sw(g, vs, vt, cut));
964 EXPECT_GT(vs.
size(), 0u);
965 EXPECT_GT(vt.
size(), 0u);
968TEST(StoerWagnerPerformance, DenseGraph20Nodes)
970 auto g = create_complete_graph(20);
976 int min_cut = sw(g, vs, vt, cut);
986TEST(StoerWagnerDeterminism, SameResultOnMultipleCalls)
988 auto g = create_complete_graph(8);
994 int result1 = sw(g, vs1, vt1, cut1);
995 int result2 = sw(g, vs2, vt2, cut2);
998 EXPECT_EQ(result1, result2);
999 EXPECT_EQ(cut1.
size(), cut2.
size());
Stoer-Wagner deterministic min-cut algorithm.
Graph create_triangle()
Creates a triangle (K_3) - the simplest non-bipartite graph.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool has(const Key &key) const
Arc for graphs implemented with simple adjacency lists.
constexpr bool is_empty() const noexcept
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Arc * insert_arc(Node *src, Node *tgt, void *a)
virtual Node * insert_node(Node *p)
Insertion of a node whose memory has already been allocated.
Number Theoretic Transform over Z / MOD Z.
Stoer-Wagner deterministic minimum cut algorithm.
Weight min_cut_weight(GT &g)
Find only the minimum cut weight (faster, no partition info).
A non-degenerate triangle defined by three points.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
@ Cycle
Graph has an Eulerian cycle.
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.
Arc of graph implemented with double-linked adjacency lists.
Distance_Type operator()(SimpleGraph::Arc *arc) const
bool operator()(typename GT::Arc *a) const
GT create_cycle(size_t n)
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.