37# include <gtest/gtest.h>
54 template <
class GT,
typename Cost>
62 std::set<typename GT::Node *> seen;
63 auto ni =
r.cycle_nodes.get_it();
64 auto * first =
ni.get_curr();
66 for (
auto ai =
r.cycle_arcs.get_it();
ai.has_curr();
ai.next_ne())
90 Optimum
exhaustive(
const size_t n,
const std::vector<Edge_Def> & edges,
const size_t bound)
93 std::vector<bool> seen(n,
false);
94 std::function<
void(
size_t,
size_t,
long long,
size_t)> visit =
95 [&](
const size_t start,
const size_t u,
const long long cost,
const size_t length)
99 for (
const auto & [a, v,
w] : edges)
104 if (
not best.exists
or cost +
w < best.cost
105 or (cost +
w == best.cost
and length + 1 < best.length))
106 best = {
true, cost +
w, length + 1};
108 else if (
not seen[v])
111 visit(start, v, cost +
w, length + 1);
116 for (
size_t s = 0; s < n; ++s)
128 for (
const size_t n : {0u, 5u})
130 auto b =
build_graph(n, n == 0 ? std::vector<Edge_Def>{}
131 : std::vector<Edge_Def>{{0, 1, -5}, {1, 2, -4}, {3, 4, 1}});
132 for (
size_t length = 0; length <= 3; ++length)
146 auto b =
build_graph(4, {{0, 0, 1}, {0, 1, 0}, {1, 0, -3},
147 {1, 2, 0}, {2, 0, -5}, {2, 3, -10}, {3, 0, -10}});
148 for (
size_t length = 1; length <= 3; ++length)
153 EXPECT_EQ(
r.total_cost, length == 1 ? 1 : length == 2 ? -3 : -5);
156 auto ring =
build_graph(4, {{0, 1, -1}, {1, 2, -1}, {2, 3, -1}, {3, 0, -1}});
164 auto b =
build_graph(3, {{0, 0, -4}, {1, 1, -4}, {2, 2, -4},
165 {0, 1, -2}, {1, 2, -2}, {2, 0, -2},
166 {1, 0, -1}, {2, 1, -1}, {0, 2, -1}});
195 auto b =
build_graph(3, {{0, 1, 10}, {0, 1, -2}, {0, 1, -2},
196 {1, 2, -1}, {2, 0, 0}});
200 EXPECT_EQ(
r.cycle_arcs.get_first(), b.arcs[1]);
203 EXPECT_EQ(hidden.cycle_arcs.get_first(), b.arcs[2]);
208 auto b =
build_graph(6, {{0, 1, 0}, {1, 2, 0}, {2, 0, -3},
209 {3, 4, 0}, {4, 3, -3}, {5, 5, -3}});
218 const double huge = std::ldexp(1.0, 100);
220 {{0, 1,
huge}, {1, 2, -1}, {2, 0, -
huge}, {3, 3, -0.5}});
234 const double big = std::ldexp(1.0, 54);
235 const double loop = std::nextafter(-2.0, 0.0);
236 ASSERT_EQ(
static_cast<double>(big + 6.0) + -(big + 8.0), 0.0);
238 {{0, 1, big}, {1, 2, 6.0}, {2, 0, -(big + 8.0)}, {3, 3, loop}});
248 const double tiny = std::ldexp(1.0, -100);
250 {{0, 1, -1}, {1, 2, -
tiny}, {2, 0, 0}, {3, 3, -1}});
258 const double m = std::numeric_limits<double>::max();
260 for (
size_t bound = 1; bound <= 3; ++bound)
266 const long long m = std::numeric_limits<long long>::max();
281 const long long m = std::numeric_limits<long long>::max();
282 auto ints =
build_graph(4, {{0, 1,
m}, {1, 2, 1}, {2, 0, -
m}, {3, 3, -1}});
287 auto above =
build_graph(4, {{0, 1,
m}, {1, 2,
m}, {2, 0, 1}, {3, 3, -1}});
298 const double d = std::numeric_limits<double>::max();
300 {{0, 1, d}, {1, 2, d}, {2, 0, -d}, {3, 3, -1}});
302 auto lone =
build_graph_generic<Float_Graph, double>(3, {{0, 1, d}, {1, 2, d}, {2, 0, -d}});
306 auto infinite =
build_graph_generic<Float_Graph, double>(3, {{0, 1, d}, {1, 2, d}, {2, 0, d}});
318 const double d = std::numeric_limits<double>::max();
320 6, {{0, 1, d}, {1, 2, 0x1p969}, {2, 0, 0x1p969}, {3, 4, -1}, {4, 5, -1}, {5, 3, -1}});
327 auto alone =
build_graph_generic<Float_Graph, double>(3, {{0, 1, d}, {1, 2, 0x1p969}, {2, 0, 0x1p969}});
333 for (
const double invalid : {std::numeric_limits<double>::infinity(),
334 std::numeric_limits<double>::quiet_NaN()})
344 auto b =
build_graph(4, {{0, 1, -1}, {0, 1, 5}, {1, 2, 0}, {2, 0, 0}, {3, 3, -2}});
347 for (
size_t bound = 1; bound <= 3; ++bound)
359 auto a =
build_graph_generic<Arr_Digraph, long long>(3, {{0, 1, -3}, {1, 2, 1}, {2, 0, 1}});
364 auto b =
build_graph_generic<U, unsigned long long>(3, {{0, 1, 2}, {1, 2, 3}, {2, 0, 1}});
370 std::mt19937_64
rng(0xD420261002ULL);
373 const size_t n = 1 +
rng() % 8,
m =
rng() % (n * n + 4);
374 std::vector<Edge_Def> edges;
375 for (
size_t i = 0; i <
m; ++i)
377 const size_t u =
rng() % n, v =
rng() % n;
378 const long long w =
static_cast<long long>(
rng() % 17) - 8;
379 edges.emplace_back(u, v,
w);
382 for (
size_t bound = 1; bound <= 3; ++bound)
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
Minimal std::expected-style result type for C++20.
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)
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...
Short_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_up_to_3(const GT &g, const size_t max_length=3, Distance distance=Distance(), SA sa=SA())
Minimum-total-cost simple cycle with at most one, two or three arcs.
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
Main namespace for Aleph-w library functions.
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
and
Check uniqueness with explicit hash + equality functors.
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....
Arc of graph implemented with double-linked adjacency lists.
Exact optimum returned by most_negative_cycle_up_to_3().
void plant(Built_Graph_T< GT > &built)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)