37# include <gtest/gtest.h>
45# include <type_traits>
63 template <
class GT,
typename Cost>
67 return r.length == 0
and r.cycle_nodes.is_empty()
and r.cycle_arcs.is_empty();
69 if (
r.length == 0
or r.cycle_nodes.size() !=
r.length + 1
or r.cycle_arcs.size() !=
r.length)
72 std::set<typename GT::Node *> seen;
73 auto node_it =
r.cycle_nodes.get_it();
94 if constexpr (std::is_floating_point_v<Cost>)
97 return std::fabs(
static_cast<long double>(sum) -
static_cast<long double>(
r.total_cost))
98 <= (
r.length + 1) *
static_cast<long double>(std::numeric_limits<Cost>::epsilon()) *
abs_sum;
100 return sum ==
r.total_cost;
106 long long min_cost = 0;
113 const size_t n = built.
nodes.size();
114 std::vector<bool> visited(n,
false);
117 std::function<
void(
size_t,
size_t,
long long,
size_t)>
dfs =
118 [&](
const size_t start,
const size_t u,
const long long cost,
const size_t len)
124 Arc * arc = it.get_current_arc_ne();
125 const size_t v =
static_cast<size_t>(it.get_tgt_node()->get_info());
129 const long long total = cost +
w;
139 if (v < start
or visited[v])
142 dfs(start, v, cost +
w, len + 1);
147 for (
size_t s = 0; s < n; ++s)
165 std::vector<Edge_Def> edges;
166 for (
size_t i = 0; i < 4; ++i)
168 const size_t h = 4 + 2 * i;
169 const size_t j =
h + 1;
170 edges.emplace_back(i,
h, 0);
171 edges.emplace_back(
h, i, -2);
172 edges.emplace_back(
h, j, -2);
173 edges.emplace_back(j,
h, -1);
175 edges.emplace_back(0, 1, -2);
176 edges.emplace_back(1, 2, -1);
177 edges.emplace_back(2, 3, -1);
178 edges.emplace_back(3, 0, -1);
179 for (
size_t i = 12; i < 17; ++i)
180 edges.emplace_back(i, i == 16 ? 12 : i + 1, -1);
185 template <
typename W>
190 3, {{0, 1, W(1)}, {1, 2, W(-3)}, {2, 0, W(1)}});
199 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
209 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}});
230 const std::vector<Edge_Def> edges = {
231 {0, 1, -1}, {1, 2, -1}, {2, 0, -1},
232 {0, 3, -1}, {3, 4, -1}, {4, 5, -1}, {5, 6, -1}, {6, 0, 0}
238 EXPECT_NEAR(
static_cast<double>(karp.minimum_mean), -1.0, 1e-12);
258 auto built =
build_graph(4, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {2, 3, -2}, {3, 2, 1}});
275 const std::vector<Edge_Def> edges = {
276 {0, 1, 0}, {1, 0, 0},
277 {1, 2, -1}, {2, 1, -1},
278 {1, 3, -1}, {3, 1, -1},
279 {0, 4, -1}, {4, 5, -1}, {5, 0, -1}
300 auto built =
build_graph(3, {{0, 1, 2}, {1, 0, 3}, {1, 2, 1}, {2, 1, 1}});
317 auto dag =
build_graph(3, {{0, 1, -4}, {1, 2, -4}, {0, 2, -9}});
337 auto built =
build_graph(2, {{0, 1, -1}, {1, 0, -1}});
350 auto built =
build_graph(2, {{0, 0, -2}, {0, 1, -5}, {1, 0, 1}});
367 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}});
378 auto built =
build_graph(4, {{0, 1, -4}, {1, 0, 1}, {2, 3, -2}, {3, 2, 1}});
395 auto built =
build_graph(2, {{0, 1, 5}, {0, 1, -4}, {1, 0, 1}});
408 3, {{0, 1, -0.75}, {1, 2, 0.25}, {2, 0, 0.25}, {2, 1, 0.5}});
416 EXPECT_EQ(
fr.numeric_quality, Cycle_Numeric_Quality::Exact);
422 3, {{0, 1, -4}, {1, 0, 1}, {1, 2, 3}, {2, 1, 3}});
425 EXPECT_EQ(
ar.numeric_quality, Cycle_Numeric_Quality::Exact);
442 inf_graph.insert_arc(
i0,
i1, std::numeric_limits<double>::infinity());
447 auto overflow =
build_graph(2, {{0, 1, std::numeric_limits<long long>::max()}, {1, 0, 1}});
456 const long long M = std::numeric_limits<long long>::max();
488 std::vector<double> weights = {-1.0, -1
e16, 1.0, 1
e16};
489 std::sort(weights.begin(), weights.end());
496 4, {{0, 1, weights[0]}, {1, 2, weights[1]}, {2, 3, weights[2]}, {3, 0, weights[3]}});
499 for (
const double w : weights)
517 while (std::next_permutation(weights.begin(), weights.end()));
540 for (
const int e : {54, 100})
542 const double big = std::ldexp(1.0, e);
544 5, {{0, 1, big}, {1, 2, 1.0}, {2, 3, -big}, {3, 0, -2.0}, {4, 4, -1.5}});
553 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Rounded) <<
"e=" << e;
554 EXPECT_GE(
r.optimality_gap, 0.5) <<
"e=" << e;
560 const long long big = 1LL << 54;
561 auto built =
build_graph(5, {{0, 1, big}, {1, 2, 2}, {2, 3, -big}, {3, 0, -4}, {4, 4, -3}});
564 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
579 const double big = std::ldexp(1.0, 54);
580 const double loop = std::nextafter(-2.0, -3.0);
581 ASSERT_EQ(
static_cast<double>(big + 2.0) + -(big + 4.0), -4.0);
583 4, {{0, 1, big}, {1, 2, 2.0}, {2, 0, -(big + 4.0)}, {3, 3, loop}});
598 2, {{0, 1, 0x1p51 + 1}, {1, 0, -(0x1p51 + 3)}});
602 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
607 2, {{0, 1, 0x1p52 + 1}, {1, 0, -(0x1p52 + 3)}});
611 EXPECT_EQ(s.numeric_quality, Cycle_Numeric_Quality::Rounded);
625 const double big = std::ldexp(1.0, 100);
626 const double tiny = std::ldexp(1.0, -100);
627 const std::vector<double>
ring = {big, 1.0,
tiny, -1.0, -big, -
tiny};
631 std::vector<std::tuple<size_t, size_t, double>> edges;
632 for (
size_t i = 0; i <
ring.size(); ++i)
642 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Rounded) <<
"rotation=" <<
rotation;
701 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {0, 2, 7}, {2, 2, 4}});
718 auto * a = g.insert_node(0);
719 auto * b = g.insert_node(1);
720 auto * c = g.insert_node(2);
721 g.insert_arc(a, b, -2.0);
722 g.insert_arc(b, a, 1.0);
723 g.insert_arc(b, c, std::numeric_limits<double>::infinity());
724 g.insert_arc(c, a, std::numeric_limits<double>::quiet_NaN());
738 auto built =
build_graph(4, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {2, 3, -2}, {3, 2, 1}});
751 for (
const long long w : {-3LL, 0
LL, 3LL})
754 for (
const size_t bound : {1u, 2u, 100u})
775 auto built =
build_graph(7, {{0, 1, 2}, {1, 0, 2}, {3, 4, -2}, {4, 3, 1}, {5, 6, -9}});
803 const int int_min = std::numeric_limits<int>::min();
804 const int int_max = std::numeric_limits<int>::max();
805 const long double ld_lowest = std::numeric_limits<long double>::lowest();
806 const long double ld_max = std::numeric_limits<long double>::max();
842 std::mt19937_64
rng(0xB0DEDC1CULL);
843 std::uniform_int_distribution<int>
n_dist(2, 7);
844 std::uniform_int_distribution<int>
len_dist(1, 6);
845 std::bernoulli_distribution
has_edge(0.4);
846 std::bernoulli_distribution has_loop(0.1);
847 std::uniform_int_distribution<int>
weight_dist(-9, 13);
853 const size_t n =
static_cast<size_t>(
n_dist(
rng));
854 const size_t L =
static_cast<size_t>(
len_dist(
rng));
855 std::vector<Edge_Def> edges;
856 for (
size_t u = 0; u < n; ++u)
857 for (
size_t v = 0; v < n; ++v)
861 edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng)));
882 else if (
oracle.min_cost < 0)
894 std::mt19937_64
rng(0x3C1C1E5ULL);
895 std::uniform_int_distribution<int>
n_dist(2, 7);
896 std::bernoulli_distribution
has_edge(0.45);
897 std::uniform_int_distribution<int>
weight_dist(-9, 13);
901 const size_t n =
static_cast<size_t>(
n_dist(
rng));
902 std::vector<Edge_Def> edges;
903 for (
size_t u = 0; u < n; ++u)
904 for (
size_t v = 0; v < n; ++v)
906 edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng)));
929 std::vector<Edge_Def> edges;
930 for (
size_t u = 0; u < 12; ++u)
931 for (
size_t v = u + 1; v < 12; ++v)
932 if ((u + v) % 3 != 0)
933 edges.emplace_back(u, v, -100 +
static_cast<long long>(u * v));
936 for (
const size_t L : {1u, 2u, 5u, 12u, 40u})
948 constexpr size_t n = 500;
951 for (
const size_t L : {1u, 2u, 7u, 500u, 509u})
968 auto built =
build_graph(6, {{0, 1, 1}, {1, 2, 1}, {2, 0, -3},
970 {3, 4, 1}, {4, 5, 1}, {5, 3, -4}});
977 for (
auto it =
r.cycle_arcs.get_it(); it.has_curr(); it.next_ne())
978 EXPECT_NE(it.get_curr(), built.
arcs[3]) <<
"the bridge is on no cycle";
986 auto built =
build_graph(4, {{0, 1, 2}, {1, 0, 2}, {1, 2, 1}, {2, 2, -4}, {2, 3, 1}});
1002 const long long big = std::numeric_limits<long long>::max();
1003 auto built =
build_graph(5, {{0, 1, big}, {1, 2, big}, {3, 4, -1}, {4, 3, -1}});
1017 for (
const bool padded : {
false,
true})
1022 std::vector<Edge_Def> edges = {{0, 1, 1}, {0, 2, 1}, {2, 3, 1}, {1, 3, 1}, {3, 0, -5}};
1024 for (
size_t u = 4; u < 12; ++u)
1025 for (
size_t v = 4; v < 12; ++v)
1027 edges.emplace_back(u, v, 5);
1036 std::vector<int>
ids;
1037 for (
auto it =
r.cycle_nodes.get_it(); it.has_curr(); it.next_ne())
1038 ids.push_back(it.get_curr()->get_info());
1046 for (
const bool padded : {
false,
true})
1049 std::vector<Edge_Def> edges = {{1, 0, -5}, {0, 1, 1}, {0, 1, 1}};
1051 for (
size_t u = 2; u < 10; ++u)
1052 for (
size_t v = 2; v < 10; ++v)
1054 edges.emplace_back(u, v, 5);
1065 for (
auto it =
r.cycle_arcs.get_it(); it.has_curr(); it.next_ne())
1081 std::mt19937_64
rng(0xC0DEC0DEULL);
1082 std::uniform_int_distribution<int>
core_dist(2, 5);
1083 std::uniform_int_distribution<int>
fringe_dist(0, 3);
1084 std::uniform_int_distribution<int>
len_dist(1, 6);
1085 std::bernoulli_distribution
has_edge(0.45);
1086 std::bernoulli_distribution has_loop(0.1);
1087 std::bernoulli_distribution
coin(0.5);
1088 std::uniform_int_distribution<int>
weight_dist(-9, 13);
1098 const size_t n = c + tin + tout +
iso;
1099 const size_t L =
static_cast<size_t>(
len_dist(
rng));
1101 std::vector<Edge_Def> edges;
1102 auto add = [&](
const size_t u,
const size_t v)
1103 { edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng))); };
1105 for (
size_t u = 0; u < c; ++u)
1106 for (
size_t v = 0; v < c; ++v)
1109 for (
size_t i = 0; i < tin; ++i)
1111 for (
size_t u = 0; u < c; ++u)
1115 add(c + i - 1, c + i);
1117 for (
size_t i = 0; i < tout; ++i)
1119 for (
size_t u = 0; u < c; ++u)
1121 add(u, c + tin + i);
1123 add(c + tin + i - 1, c + tin + i);
1127 if (n > c
and has_loop(
rng))
1128 edges.emplace_back(c, c,
static_cast<long long>(
weight_dist(
rng)));
1136 if (
not r.has_cycle)
1147 else if (
oracle.min_cost < 0)
1163 const std::vector<Edge_Def> edges = {{0, 1, -1}, {1, 0, -1}, {1, 1, -5}};
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
Generic directed graph (digraph) wrapper template.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynList< Negative_Cycle_Item< GT, typename Distance::Distance_Type > > find_disjoint_negative_cycles(const GT &g, const size_t max_cycles, const Negative_Cycle_Exclusion exclusion=Negative_Cycle_Exclusion::Min_Weight_Arc, Distance distance=Distance(), SA sa=SA())
Find up to max_cycles negative cycles, excluding arcs of each cycle found before searching for the ne...
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > karp_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute minimum mean cycle by Karp's algorithm.
DFSResult< typename Domain::Distance > dfs(Domain &domain, typename Domain::State &state, SearchPath< typename Domain::Move > &path, const typename Domain::Distance g, const typename Domain::Distance threshold, const size_t depth, IDAStarResult< Solution, typename Domain::Distance > &result, OnSolution &on_solution)
Core recursive DFS used by IDA* for a single threshold pass.
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
Graph types and probe helpers shared by negative_cycles_test.cc and bounded_negative_cycle_test....
Result of most_negative_cycle_bounded().
Filtered iterator of adjacent arcs of a node.
std::vector< typename GT::Arc * > arcs
std::vector< typename GT::Node * > nodes
void plant(Built_Graph_T< GT > &built)
Array-based graph implementation.
Generic graph and digraph implementations.