Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bounded_negative_cycle_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <gtest/gtest.h>
38
39# include <cmath>
40# include <algorithm>
41# include <functional>
42# include <limits>
43# include <random>
44# include <set>
45# include <type_traits>
46# include <tuple>
47# include <vector>
48
49# include <Min_Mean_Cycle.H>
50# include <Negative_Cycles.H>
51# include <tpl_agraph.H>
52# include <tpl_graph.H>
53
55
56using namespace Aleph;
57using namespace Negative_Cycles_Test_Support;
58
59namespace
60{
61 // The witness must be a closed walk of consecutive graph arcs, without
62 // repeated nodes, whose weights sum to total_cost.
63 template <class GT, typename Cost>
65 {
66 if (not r.has_cycle)
67 return r.length == 0 and r.cycle_nodes.is_empty() and r.cycle_arcs.is_empty();
68
69 if (r.length == 0 or r.cycle_nodes.size() != r.length + 1 or r.cycle_arcs.size() != r.length)
70 return false;
71
72 std::set<typename GT::Node *> seen;
73 auto node_it = r.cycle_nodes.get_it();
74 typename GT::Node * first = node_it.get_curr();
75 typename GT::Node * curr = first;
76 node_it.next_ne();
77
78 Cost sum = Cost{0};
79 long double abs_sum = 0.0L;
80 for (auto arc_it = r.cycle_arcs.get_it(); arc_it.has_curr(); arc_it.next_ne())
81 {
82 typename GT::Arc * arc = arc_it.get_curr();
83 if (not node_it.has_curr() or g.get_src_node(arc) != curr
84 or g.get_tgt_node(arc) != node_it.get_curr() or not seen.insert(curr).second)
85 return false;
86 sum += arc->get_info();
87 abs_sum += std::fabs(static_cast<long double>(arc->get_info()));
88 curr = node_it.get_curr();
89 node_it.next_ne();
90 }
91
92 if (curr != first or node_it.has_curr())
93 return false;
94 if constexpr (std::is_floating_point_v<Cost>)
95 // total_cost is a compensated sum: it may differ from the plain sum above by
96 // the rounding error of plain summation, at most (terms + 1) * eps * sum of |w|
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;
99 else
100 return sum == r.total_cost;
101 }
102
103 struct Oracle
104 {
105 bool exists = false;
106 long long min_cost = 0;
107 size_t min_len = 0; // fewest arcs among the simple cycles of cost min_cost
108 };
109
110 // Exhaustive minimum over simple cycles of at most max_length arcs.
111 Oracle exact_bounded_minimum(const Built_Graph_T<Graph> & built, const size_t max_length)
112 {
113 const size_t n = built.nodes.size();
114 std::vector<bool> visited(n, false);
115 Oracle oracle;
116
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)
119 {
120 if (len >= max_length)
121 return;
122 for (Node_Arc_Iterator<Graph> it(built.nodes[u]); it.has_curr(); it.next_ne())
123 {
124 Arc * arc = it.get_current_arc_ne();
125 const size_t v = static_cast<size_t>(it.get_tgt_node()->get_info());
126 const long long w = arc->get_info();
127 if (v == start)
128 {
129 const long long total = cost + w;
130 if (not oracle.exists or total < oracle.min_cost
131 or (total == oracle.min_cost and len + 1 < oracle.min_len))
132 {
133 oracle.exists = true;
134 oracle.min_cost = total;
135 oracle.min_len = len + 1;
136 }
137 continue;
138 }
139 if (v < start or visited[v]) // canonical start: the minimum node of the cycle
140 continue;
141 visited[v] = true;
142 dfs(start, v, cost + w, len + 1);
143 visited[v] = false;
144 }
145 };
146
147 for (size_t s = 0; s < n; ++s)
148 {
149 visited[s] = true;
150 dfs(s, s, 0, 0);
151 visited[s] = false;
152 }
153 return oracle;
154 }
155
156
157 // Builds the 17-node graph of F3 (auditoria-rama-arbitrage-performance-bugs.md).
158 // Arcs are inserted in the order that matters: the dynamic program keeps a
159 // single predecessor per (layer, node), so ties are resolved by insertion
160 // order. Simple cycles: four 2-arc cycles hanging from the hubs 4..11 of
161 // cost -2 or -3, the 4-arc cycle 0-1-2-3 of cost -5, and the separate ring
162 // 12..16 of 5 arcs and cost -5.
164 {
165 std::vector<Edge_Def> edges;
166 for (size_t i = 0; i < 4; ++i)
167 {
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);
174 }
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);
181 return build_graph(17, edges);
182 }
183
184 // The same triangle of total cost -1 for every cost type.
185 template <typename W>
187 {
189 auto built = build_graph_generic<G, W>(
190 3, {{0, 1, W(1)}, {1, 2, W(-3)}, {2, 0, W(1)}});
191
192 const auto r = most_negative_cycle_bounded(built.g, 3);
193 ASSERT_TRUE(r.has_cycle);
194 EXPECT_TRUE(r.matches_relaxed_bound);
195 // Small integer weights: every sum is exact in any of these types, so the
196 // certificate is a proof also for floating-point costs.
197 EXPECT_TRUE(r.is_exact);
198 EXPECT_EQ(r.optimality_gap, W(0));
199 EXPECT_EQ(r.numeric_quality, Cycle_Numeric_Quality::Exact);
200 EXPECT_EQ(r.total_cost, W(-1));
201 EXPECT_EQ(r.length, 3u);
203 }
204} // namespace
205
206
208{
209 auto built = build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}});
210
211 const auto r3 = most_negative_cycle_bounded(built.g, 3);
212 ASSERT_TRUE(r3.has_cycle);
213 EXPECT_TRUE(r3.is_negative());
214 EXPECT_TRUE(r3.is_exact);
215 EXPECT_EQ(r3.total_cost, -1);
216 EXPECT_EQ(r3.length, 3u);
218
219 const auto r2 = most_negative_cycle_bounded(built.g, 2);
220 EXPECT_FALSE(r2.has_cycle);
221 EXPECT_FALSE(r2.is_negative());
223}
224
225
227{
228 // T: 0 -> 1 -> 2 -> 0 costs -3 over 3 arcs (mean -1).
229 // S: 0 -> 3 -> 4 -> 5 -> 6 -> 0 costs -4 over 5 arcs (mean -0.8).
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}
233 };
234 auto built = build_graph(7, edges);
235
236 const auto karp = karp_minimum_mean_cycle(built.g);
237 ASSERT_TRUE(karp.has_cycle);
238 EXPECT_NEAR(static_cast<double>(karp.minimum_mean), -1.0, 1e-12); // Karp picks T
239
240 const auto r = most_negative_cycle_bounded(built.g, 5);
241 ASSERT_TRUE(r.has_cycle);
242 EXPECT_TRUE(r.is_exact);
243 EXPECT_EQ(r.total_cost, -4); // total cost picks S
244 EXPECT_EQ(r.length, 5u);
246
247 // With the bound below S's length, T is the best cycle that fits.
248 const auto r4 = most_negative_cycle_bounded(built.g, 4);
249 ASSERT_TRUE(r4.has_cycle);
250 EXPECT_EQ(r4.total_cost, -3);
251 EXPECT_EQ(r4.length, 3u);
252}
253
254
256{
257 // Both cycles cost -1: the 2-cycle must win over the 3-cycle.
258 auto built = build_graph(4, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {2, 3, -2}, {3, 2, 1}});
259
260 const auto r = most_negative_cycle_bounded(built.g, 3);
261 ASSERT_TRUE(r.has_cycle);
262 EXPECT_EQ(r.total_cost, -1);
263 EXPECT_EQ(r.length, 2u);
265}
266
267
269{
270 // From 0, the walk 0 -> 1 -> 2 -> 1 -> 3 -> 1 -> 0 (6 arcs) costs -4 by
271 // going around both 2-cycles hanging from 1, and beats every closed walk
272 // that is a simple cycle; the best simple cycle is S: 0 -> 4 -> 5 -> 0 at
273 // -3. S must still be reported (it is a candidate through its own nodes),
274 // but the -4 walk keeps is_exact from certifying it.
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}
280 };
281 auto built = build_graph(6, edges);
282
283 const auto r = most_negative_cycle_bounded(built.g, 6);
284 ASSERT_TRUE(r.has_cycle);
285 EXPECT_FALSE(r.is_exact);
286 EXPECT_EQ(r.total_cost, -3);
287 EXPECT_EQ(r.length, 3u);
289
290 // With max_length = 3 no walk can repeat a node: the same answer, certified.
291 const auto r3 = most_negative_cycle_bounded(built.g, 3);
292 ASSERT_TRUE(r3.has_cycle);
293 EXPECT_TRUE(r3.is_exact);
294 EXPECT_EQ(r3.total_cost, -3);
295}
296
297
299{
300 auto built = build_graph(3, {{0, 1, 2}, {1, 0, 3}, {1, 2, 1}, {2, 1, 1}});
301
302 const auto r = most_negative_cycle_bounded(built.g, 4);
303 ASSERT_TRUE(r.has_cycle);
304 EXPECT_FALSE(r.is_negative());
305 EXPECT_TRUE(r.is_exact);
306 EXPECT_EQ(r.total_cost, 2); // 1 -> 2 -> 1
307 EXPECT_EQ(r.length, 2u);
309}
310
311
313{
314 Graph empty;
316
317 auto dag = build_graph(3, {{0, 1, -4}, {1, 2, -4}, {0, 2, -9}});
318 EXPECT_FALSE(most_negative_cycle_bounded(dag.g, 5).has_cycle);
319
320 auto cyc = build_graph(2, {{0, 1, -1}, {1, 0, -1}});
324}
325
326
327// max_length == 0 must not snapshot nor validate the graph: no exception for
328// an undirected graph and no call to the distance accessor.
330{
331 UGraph undirected;
332 auto * u0 = undirected.insert_node(0);
333 auto * u1 = undirected.insert_node(1);
334 undirected.insert_arc(u0, u1, -1);
336
337 auto built = build_graph(2, {{0, 1, -1}, {1, 0, -1}});
338 int calls = 0;
339 int filter_calls = 0;
341 built.g, 0, Counting_Dist{&calls}, Counting_Filter{&filter_calls, nullptr});
342 EXPECT_FALSE(r.has_cycle);
343 EXPECT_EQ(calls, 0);
345}
346
347
349{
350 auto built = build_graph(2, {{0, 0, -2}, {0, 1, -5}, {1, 0, 1}});
351
352 const auto r1 = most_negative_cycle_bounded(built.g, 1);
353 ASSERT_TRUE(r1.has_cycle);
354 EXPECT_EQ(r1.total_cost, -2);
355 EXPECT_EQ(r1.length, 1u);
357
358 const auto r2 = most_negative_cycle_bounded(built.g, 2);
359 ASSERT_TRUE(r2.has_cycle);
360 EXPECT_EQ(r2.total_cost, -4); // 0 -> 1 -> 0
361 EXPECT_EQ(r2.length, 2u);
362}
363
364
366{
367 auto built = build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}});
368
369 const auto r = most_negative_cycle_bounded(built.g, 1000);
370 ASSERT_TRUE(r.has_cycle);
371 EXPECT_EQ(r.total_cost, -1);
372 EXPECT_EQ(r.length, 3u);
373}
374
375
377{
378 auto built = build_graph(4, {{0, 1, -4}, {1, 0, 1}, {2, 3, -2}, {3, 2, 1}});
379
380 const auto full = most_negative_cycle_bounded(built.g, 2);
381 ASSERT_TRUE(full.has_cycle);
382 EXPECT_EQ(full.total_cost, -3);
383
384 const Hide_Arc filter{built.arcs[0]};
386 built.g, 2, Dft_Dist<Graph>(), filter);
387 ASSERT_TRUE(filtered.has_cycle);
388 EXPECT_EQ(filtered.total_cost, -1);
389 EXPECT_TRUE(witness_is_simple_cycle(built.g, filtered));
390}
391
392
394{
395 auto built = build_graph(2, {{0, 1, 5}, {0, 1, -4}, {1, 0, 1}});
396
397 const auto r = most_negative_cycle_bounded(built.g, 2);
398 ASSERT_TRUE(r.has_cycle);
399 EXPECT_EQ(r.total_cost, -3);
400 EXPECT_EQ(r.cycle_arcs.get_first(), built.arcs[1]);
402}
403
404
406{
408 3, {{0, 1, -0.75}, {1, 2, 0.25}, {2, 0, 0.25}, {2, 1, 0.5}});
409 const auto fr = most_negative_cycle_bounded(fbuilt.g, 3);
410 ASSERT_TRUE(fr.has_cycle);
411 EXPECT_TRUE(fr.matches_relaxed_bound);
412 // Binary fractions of a small range: every sum of the search is exact, so the
413 // certificate is a proof.
414 EXPECT_TRUE(fr.is_exact);
415 EXPECT_EQ(fr.optimality_gap, 0.0);
416 EXPECT_EQ(fr.numeric_quality, Cycle_Numeric_Quality::Exact);
417 EXPECT_NEAR(fr.total_cost, -0.25, 1e-12);
418 EXPECT_EQ(fr.length, 3u);
420
422 3, {{0, 1, -4}, {1, 0, 1}, {1, 2, 3}, {2, 1, 3}});
423 const auto ar = most_negative_cycle_bounded(abuilt.g, 4);
424 ASSERT_TRUE(ar.has_cycle);
425 EXPECT_EQ(ar.numeric_quality, Cycle_Numeric_Quality::Exact);
426 EXPECT_EQ(ar.total_cost, -3);
428}
429
430
432{
433 UGraph undirected;
434 auto * u0 = undirected.insert_node(0);
435 auto * u1 = undirected.insert_node(1);
436 undirected.insert_arc(u0, u1, -1);
437 EXPECT_THROW((most_negative_cycle_bounded(undirected, 2)), std::domain_error);
438
440 auto * i0 = inf_graph.insert_node(0);
441 auto * i1 = inf_graph.insert_node(1);
442 inf_graph.insert_arc(i0, i1, std::numeric_limits<double>::infinity());
443 inf_graph.insert_arc(i1, i0, 1.0);
444 EXPECT_THROW((most_negative_cycle_bounded(inf_graph, 2)), std::domain_error);
445
446 // 0 -> 1 reaches max, then max + 1 overflows on the way back.
447 auto overflow = build_graph(2, {{0, 1, std::numeric_limits<long long>::max()}, {1, 0, 1}});
448 EXPECT_THROW((most_negative_cycle_bounded(overflow.g, 2)), std::overflow_error);
449}
450
451
452// numeric_limits::max() used to double as the "unreachable" sentinel, so a
453// closed walk costing exactly max was discarded as if it did not exist.
455{
456 const long long M = std::numeric_limits<long long>::max();
457
458 auto loop = build_graph(1, {{0, 0, M}});
459 const auto r = most_negative_cycle_bounded(loop.g, 1);
460 ASSERT_TRUE(r.has_cycle);
461 EXPECT_EQ(r.total_cost, M);
462 EXPECT_EQ(r.length, 1u);
464
465 // A 2-cycle whose cost is exactly max is also found and beats nothing else.
466 auto pair = build_graph(2, {{0, 1, M - 1}, {1, 0, 1}});
467 const auto r2 = most_negative_cycle_bounded(pair.g, 2);
468 ASSERT_TRUE(r2.has_cycle);
469 EXPECT_EQ(r2.total_cost, M);
470}
471
472
473// ---------------------------------------------------------------------------
474// F1 of auditoria-rama-arbitrage-performance-bugs.md (Stage 2, compensated cost).
475//
476// The only cycle has the weights 1e16, 1, -1e16, -1, all exactly
477// representable, and its exact sum is zero (computed here with integers).
478// Accumulated in plain double, ((1e16 + 1) - 1e16) - 1 loses the +1 and
479// gives -1, which used to be reported as a certified negative cycle. The cost
480// of the reported cycle is now recomputed with compensated summation, and the
481// relaxed-bound condition is withdrawn when it disagrees in sign with the plain
482// sum the search ranked. Whatever order the weights are inserted in, the cycle
483// must never be presented as negative, and with floating-point weights it is
484// never certified.
485// ---------------------------------------------------------------------------
487{
488 std::vector<double> weights = {-1.0, -1e16, 1.0, 1e16};
489 std::sort(weights.begin(), weights.end());
490
491 size_t orders = 0;
492 size_t certified = 0;
493 do
494 {
496 4, {{0, 1, weights[0]}, {1, 2, weights[1]}, {2, 3, weights[2]}, {3, 0, weights[3]}});
497
498 long long exact_sum = 0; // independent oracle: integer arithmetic, exact here
499 for (const double w : weights)
500 exact_sum += static_cast<long long>(w);
502
503 const auto r = most_negative_cycle_bounded(built.g, 4);
504 ASSERT_TRUE(r.has_cycle);
505 EXPECT_EQ(r.length, 4u);
507 EXPECT_EQ(r.total_cost, 0.0);
508 EXPECT_FALSE(r.is_negative());
509
510 // The enumeration never reports it either.
511 EXPECT_TRUE(find_disjoint_negative_cycles(built.g, 1).is_empty());
512
513 EXPECT_FALSE(r.is_exact);
514 certified += r.matches_relaxed_bound;
515 ++orders;
516 }
517 while (std::next_permutation(weights.begin(), weights.end()));
518
519 EXPECT_EQ(orders, 24u);
520 // The relaxed-bound condition is kept only when plain and compensated sums
521 // agree on the sign: some orders do (plain sum 0), most do not.
523}
524
525
526// ---------------------------------------------------------------------------
527// Audit 2026-10-02, H1. The ring 0 -> 1 -> 2 -> 3 -> 0 weighs 2^e, 1, -2^e, -2:
528// its exact total is -1, an identity between powers of two, but for e = 54
529// and e = 100 its plain double sum loses the 1 and gives -2, below the
530// self-loop of node 4 (-1.5, the exact optimum). Every weight is a binary
531// fraction represented exactly.
532//
533// The search used to return the ring "certified optimal". The candidates it
534// offers are now compared by their exact sums, so the loop wins; and since the
535// dynamic program itself ran on rounded sums, no certificate is given: the gap
536// is rigorous, and large here because the weights are.
537// ---------------------------------------------------------------------------
539{
540 for (const int e : {54, 100})
541 {
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}});
545
546 const auto r = most_negative_cycle_bounded(built.g, 4);
547 ASSERT_TRUE(r.has_cycle) << "e=" << e;
548 EXPECT_EQ(r.total_cost, -1.5) << "e=" << e; // the exact optimum
549 EXPECT_EQ(r.length, 1u) << "e=" << e;
550 EXPECT_TRUE(r.is_negative()) << "e=" << e;
551 EXPECT_TRUE(witness_is_simple_cycle(built.g, r)) << "e=" << e;
552 EXPECT_FALSE(r.is_exact) << "e=" << e; // the search rounded: no proof
553 EXPECT_EQ(r.numeric_quality, Cycle_Numeric_Quality::Rounded) << "e=" << e;
554 EXPECT_GE(r.optimality_gap, 0.5) << "e=" << e; // covers the ring's rounding (-2 vs -1)
555 }
556
557 // The same structure with integer weights, where every sum is exact: the
558 // loop wins and the certificate is a proof. (Weights doubled to keep them
559 // integral: ring total -2, loop -3.)
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}});
562 const auto r = most_negative_cycle_bounded(built.g, 4);
563 ASSERT_TRUE(r.has_cycle);
564 EXPECT_EQ(r.numeric_quality, Cycle_Numeric_Quality::Exact);
565 EXPECT_TRUE(r.is_exact);
566 EXPECT_EQ(r.optimality_gap, 0);
567 EXPECT_EQ(r.total_cost, -3);
568 EXPECT_EQ(r.length, 1u);
569}
570
572{
573 // Stage D6 drops, without an exact sum, a candidate whose plain sum is
574 // above the best one's by more than twice the rounding bound. Here
575 // 2^54 + 2 ties to even at 2^54, so the triangle's plain sum is -4 while
576 // its exact cost is -2: it is offered first, and the loop just below -2,
577 // two units worse in plain sums, is exactly better. A filter whose bound
578 // is about 32 times too small drops the loop.
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); // the cast drops excess precision
583 4, {{0, 1, big}, {1, 2, 2.0}, {2, 0, -(big + 4.0)}, {3, 3, loop}});
584 const auto r = most_negative_cycle_bounded(built.g, 3);
585 ASSERT_TRUE(r.has_cycle);
586 EXPECT_EQ(r.length, 1u);
587 EXPECT_EQ(r.total_cost, loop);
589}
590
592{
593 // Final independent review of the branch, mutation M06: nothing pinned the
594 // exact regime from the conservative side. Sums of two weights with bits
595 // from 2^0 to 2^51 have at most 53 bits, so every sum of the search is
596 // exact and the result is a proof; with bits up to 2^52 they may round.
598 2, {{0, 1, 0x1p51 + 1}, {1, 0, -(0x1p51 + 3)}});
599 const auto r = most_negative_cycle_bounded(edge.g, 2);
600 ASSERT_TRUE(r.has_cycle);
601 EXPECT_EQ(r.total_cost, -2.0);
602 EXPECT_EQ(r.numeric_quality, Cycle_Numeric_Quality::Exact);
603 EXPECT_EQ(r.optimality_gap, 0.0);
604 EXPECT_TRUE(r.is_exact);
605
607 2, {{0, 1, 0x1p52 + 1}, {1, 0, -(0x1p52 + 3)}});
608 const auto s = most_negative_cycle_bounded(beyond.g, 2);
609 ASSERT_TRUE(s.has_cycle);
610 EXPECT_EQ(s.total_cost, -2.0);
611 EXPECT_EQ(s.numeric_quality, Cycle_Numeric_Quality::Rounded);
612 EXPECT_GT(s.optimality_gap, 0.0);
613}
614
615
616// ---------------------------------------------------------------------------
617// Audit 2026-10-02, H2. The six-arc ring with weights 2^100, 1, 2^-100, -1,
618// -2^100, -2^-100 (in some rotation) sums exactly to zero: three pairs that
619// cancel. Compensated summation, which the reported cost used to come from,
620// gave -2^-100 because its compensation is rounded too, and the zero cycle was
621// presented as negative. The cost is now an exact sum rounded once.
622// ---------------------------------------------------------------------------
624{
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};
628
629 for (size_t rotation = 0; rotation < ring.size(); ++rotation)
630 {
631 std::vector<std::tuple<size_t, size_t, double>> edges;
632 for (size_t i = 0; i < ring.size(); ++i)
633 edges.emplace_back(i, (i + 1) % ring.size(), ring[(i + rotation) % ring.size()]);
634 auto built = build_graph_generic<Float_Graph, double>(ring.size(), edges);
635
636 const auto r = most_negative_cycle_bounded(built.g, ring.size());
637 ASSERT_TRUE(r.has_cycle) << "rotation=" << rotation;
638 EXPECT_EQ(r.length, ring.size()) << "rotation=" << rotation;
639 EXPECT_TRUE(witness_is_simple_cycle(built.g, r)) << "rotation=" << rotation;
640 EXPECT_EQ(r.total_cost, 0.0) << "rotation=" << rotation;
641 EXPECT_FALSE(r.is_negative()) << "rotation=" << rotation;
642 EXPECT_EQ(r.numeric_quality, Cycle_Numeric_Quality::Rounded) << "rotation=" << rotation;
643
644 // The enumeration does not report it either.
645 EXPECT_TRUE(find_disjoint_negative_cycles(built.g, 5).is_empty()) << "rotation=" << rotation;
646 }
647}
648
649
650// ---------------------------------------------------------------------------
651// F3: the documented contract does not promise the globally cheapest cycle
652// with fewest arcs, nor that `is_exact` is false only when no simple walk
653// attains the minimum. These two cases pin the behaviour with small integers
654// (no floating point involved) and an independent exhaustive oracle.
655// ---------------------------------------------------------------------------
657{
658 auto built = build_tie_graph();
659
660 const Oracle oracle = exact_bounded_minimum(built, 5);
661 ASSERT_TRUE(oracle.exists);
662 EXPECT_EQ(oracle.min_cost, -5);
663 EXPECT_EQ(oracle.min_len, 4u); // the 4-arc cycle 0-1-2-3 attains the optimum
664
665 const auto r = most_negative_cycle_bounded(built.g, 5);
666 ASSERT_TRUE(r.has_cycle);
667 EXPECT_TRUE(r.is_exact); // the cost is certified optimal...
668 EXPECT_EQ(r.total_cost, oracle.min_cost);
669 EXPECT_EQ(r.length, 5u); // ...the number of arcs is not: the ring, not the 4-cycle
670 EXPECT_GT(r.length, oracle.min_len);
672}
673
674
676{
677 auto built = build_tie_graph();
678
679 const Oracle oracle = exact_bounded_minimum(built, 4);
680 ASSERT_TRUE(oracle.exists);
681 EXPECT_EQ(oracle.min_cost, -5); // the 4-cycle 0-1-2-3 is a simple cycle of cost -5
682
683 // The dynamic program keeps a non-simple walk of the same cost at some
684 // states, so the 4-cycle is never offered: the result is the best of the
685 // evaluated candidates, strictly worse than the optimum, and the flag says so.
686 const auto r = most_negative_cycle_bounded(built.g, 4);
687 ASSERT_TRUE(r.has_cycle);
688 EXPECT_FALSE(r.is_exact);
689 EXPECT_EQ(r.total_cost, -3);
690 EXPECT_EQ(r.length, 2u);
691 EXPECT_GT(r.total_cost, oracle.min_cost);
693 EXPECT_LT(r.total_cost, 0); // still strictly negative: the bound c_opt <= cost <= c_opt/m holds
694}
695
696
698{
699 // 5 arcs, one of them hidden by the filter: the filter is asked about all
700 // of them exactly once and the distance accessor only about accepted ones.
701 auto built = build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {0, 2, 7}, {2, 2, 4}});
702 int dist_calls = 0;
703 int filter_calls = 0;
704
706 built.g, 4, Counting_Dist{&dist_calls}, Counting_Filter{&filter_calls, built.arcs[3]});
707
708 ASSERT_TRUE(r.has_cycle);
709 EXPECT_EQ(r.total_cost, -1);
712}
713
714
716{
717 Float_Graph g;
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); // cycle of cost -1
723 g.insert_arc(b, c, std::numeric_limits<double>::infinity());
724 g.insert_arc(c, a, std::numeric_limits<double>::quiet_NaN());
725
726 EXPECT_THROW((most_negative_cycle_bounded(g, 4)), std::domain_error);
727
730 ASSERT_TRUE(r.has_cycle);
731 EXPECT_NEAR(r.total_cost, -1.0, 1e-12);
732 EXPECT_EQ(r.length, 2u);
733}
734
735
737{
738 auto built = build_graph(4, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {2, 3, -2}, {3, 2, 1}});
740 planted.plant(built);
741 ASSERT_TRUE(planted.intact(built));
742
743 const auto r = most_negative_cycle_bounded(built.g, 4);
744 ASSERT_TRUE(r.has_cycle);
745 EXPECT_TRUE(planted.intact(built));
746}
747
748
750{
751 for (const long long w : {-3LL, 0LL, 3LL})
752 {
753 auto built = build_graph(1, {{0, 0, w}});
754 for (const size_t bound : {1u, 2u, 100u})
755 {
756 const auto r = most_negative_cycle_bounded(built.g, bound);
757 ASSERT_TRUE(r.has_cycle) << "w=" << w << " bound=" << bound;
758 EXPECT_EQ(r.total_cost, w);
759 EXPECT_EQ(r.length, 1u);
760 EXPECT_TRUE(r.is_exact);
761 EXPECT_EQ(r.is_negative(), w < 0);
763 }
764 }
765
769}
770
771
773{
774 // 0 <-> 1 positive, 2 isolated, 3 <-> 4 negative, 5 -> 6 acyclic.
775 auto built = build_graph(7, {{0, 1, 2}, {1, 0, 2}, {3, 4, -2}, {4, 3, 1}, {5, 6, -9}});
776
777 const auto r = most_negative_cycle_bounded(built.g, 4);
778 ASSERT_TRUE(r.has_cycle);
779 EXPECT_EQ(r.total_cost, -1);
780 EXPECT_EQ(r.length, 2u);
781 EXPECT_TRUE(r.is_exact);
783 EXPECT_EQ(r.cycle_nodes.get_first(), built.nodes[3]); // the negative component, not the positive one
784}
785
786
791
792
797
798
800{
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();
807
808 // A loop of the extreme value is a cycle of that cost, found even though
809 // the value equals the type's maximum (no value is reserved as a sentinel).
811 const auto r_lo = most_negative_cycle_bounded(int_lo.g, 1);
812 ASSERT_TRUE(r_lo.has_cycle);
813 EXPECT_EQ(r_lo.total_cost, int_min);
814
816 const auto r_hi = most_negative_cycle_bounded(int_hi.g, 1);
817 ASSERT_TRUE(r_hi.has_cycle);
818 EXPECT_EQ(r_hi.total_cost, int_max);
819 EXPECT_FALSE(r_hi.is_negative());
820
822 const auto q_lo = most_negative_cycle_bounded(ld_lo.g, 1);
823 ASSERT_TRUE(q_lo.has_cycle);
824 EXPECT_EQ(q_lo.total_cost, ld_lowest);
825
827 const auto q_hi = most_negative_cycle_bounded(ld_hi.g, 1);
828 ASSERT_TRUE(q_hi.has_cycle);
829 EXPECT_EQ(q_hi.total_cost, ld_max);
830
831 // Sums that leave the type throw: overflow for integers, a non-finite
832 // accumulation for floating point.
833 auto int_two = build_graph_generic<IntGraph, int>(2, {{0, 1, int_max}, {1, 0, int_max}});
834 EXPECT_THROW((most_negative_cycle_bounded(int_two.g, 2)), std::overflow_error);
836 EXPECT_THROW((most_negative_cycle_bounded(ld_two.g, 2)), std::domain_error);
837}
838
839
841{
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);
848
849 size_t exact_results = 0;
850 size_t results = 0;
851 for (size_t trial = 0; trial < 200; ++trial)
852 {
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)
858 {
859 const bool keep = (u == v) ? has_loop(rng) : has_edge(rng);
860 if (keep)
861 edges.emplace_back(u, v, static_cast<long long>(weight_dist(rng)));
862 }
863
864 auto built = build_graph(n, edges);
865 const Oracle oracle = exact_bounded_minimum(built, L);
866 const auto r = most_negative_cycle_bounded(built.g, L);
867
868 ASSERT_EQ(r.has_cycle, oracle.exists) << "trial=" << trial;
869 ASSERT_TRUE(witness_is_simple_cycle(built.g, r)) << "trial=" << trial;
870 if (not r.has_cycle)
871 continue;
872
873 ++results;
874 ASSERT_LE(r.length, L) << "trial=" << trial;
875 // A simple cycle can never beat the exhaustive optimum.
876 ASSERT_GE(r.total_cost, oracle.min_cost) << "trial=" << trial;
877 if (r.is_exact)
878 {
880 ASSERT_EQ(r.total_cost, oracle.min_cost) << "trial=" << trial;
881 }
882 else if (oracle.min_cost < 0)
883 // An optimal negative walk decomposes into simple cycles at least one of
884 // which is strictly negative, and every component is a candidate.
885 ASSERT_LT(r.total_cost, 0) << "trial=" << trial;
886 }
887 EXPECT_GT(results, 100u);
888 EXPECT_GT(exact_results * 10, results * 8); // the optimum walk is usually simple
889}
890
891
893{
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);
898
899 for (size_t trial = 0; trial < 150; ++trial)
900 {
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)
905 if (u != v and has_edge(rng))
906 edges.emplace_back(u, v, static_cast<long long>(weight_dist(rng)));
907
908 auto built = build_graph(n, edges);
909 const Oracle oracle = exact_bounded_minimum(built, 3);
910 const auto r = most_negative_cycle_bounded(built.g, 3);
911
912 ASSERT_EQ(r.has_cycle, oracle.exists) << "trial=" << trial;
913 if (not r.has_cycle)
914 continue;
915 ASSERT_TRUE(r.is_exact) << "trial=" << trial;
916 ASSERT_EQ(r.total_cost, oracle.min_cost) << "trial=" << trial;
917 ASSERT_TRUE(witness_is_simple_cycle(built.g, r)) << "trial=" << trial;
918 }
919}
920
921
922// ---------------------------------------------------------------------------
923// Cyclic core (reduction to strongly connected components) and traversal order
924// ---------------------------------------------------------------------------
925
927{
928 // Many very negative arcs, all pointing forward: no cycle at all.
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));
934 auto built = build_graph(12, edges);
935
936 for (const size_t L : {1u, 2u, 5u, 12u, 40u})
937 {
938 const auto r = most_negative_cycle_bounded(built.g, L);
939 EXPECT_FALSE(r.has_cycle) << "L=" << L;
940 EXPECT_FALSE(r.is_negative()) << "L=" << L;
941 EXPECT_TRUE(witness_is_simple_cycle(built.g, r)) << "L=" << L;
942 }
943}
944
945
947{
948 constexpr size_t n = 500;
949 auto built = build_graph(n, {{250, 250, -3}});
950
951 for (const size_t L : {1u, 2u, 7u, 500u, 509u})
952 {
953 const auto r = most_negative_cycle_bounded(built.g, L);
954 ASSERT_TRUE(r.has_cycle) << "L=" << L;
955 EXPECT_TRUE(r.is_exact) << "L=" << L;
956 EXPECT_EQ(r.total_cost, -3) << "L=" << L;
957 EXPECT_EQ(r.length, 1u) << "L=" << L;
958 EXPECT_EQ(r.cycle_nodes.get_first(), built.nodes[250]) << "L=" << L;
959 EXPECT_TRUE(witness_is_simple_cycle(built.g, r)) << "L=" << L;
960 }
961}
962
963
965{
966 // Triangle {0,1,2} of cost -1, triangle {3,4,5} of cost -2, and the arc
967 // 2 -> 3 of cost -1000 joining them one way: it can close no cycle.
968 auto built = build_graph(6, {{0, 1, 1}, {1, 2, 1}, {2, 0, -3},
969 {2, 3, -1000},
970 {3, 4, 1}, {4, 5, 1}, {5, 3, -4}});
971
972 const auto r = most_negative_cycle_bounded(built.g, 6);
973 ASSERT_TRUE(r.has_cycle);
974 EXPECT_EQ(r.total_cost, -2);
975 EXPECT_EQ(r.length, 3u);
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";
979}
980
981
983{
984 // {0,1} is a positive component, 1 -> 2 leaves it, node 2 has a negative
985 // self-loop of its own and 2 -> 3 leads to a node without cycles.
986 auto built = build_graph(4, {{0, 1, 2}, {1, 0, 2}, {1, 2, 1}, {2, 2, -4}, {2, 3, 1}});
987
988 const auto r = most_negative_cycle_bounded(built.g, 3);
989 ASSERT_TRUE(r.has_cycle);
990 EXPECT_EQ(r.total_cost, -4);
991 EXPECT_EQ(r.length, 1u);
992 EXPECT_EQ(r.cycle_nodes.get_first(), built.nodes[2]);
994}
995
996
998{
999 // The chain 0 -> 1 -> 2 would overflow if it were accumulated, but it is
1000 // on no cycle: only the arcs of the cyclic core are ever added up. (An
1001 // overflow on a cycle does throw: see CostsAtTheLimitsOfTheType.)
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}});
1004
1005 const auto r = most_negative_cycle_bounded(built.g, 5);
1006 ASSERT_TRUE(r.has_cycle);
1007 EXPECT_EQ(r.total_cost, -2);
1009}
1010
1011
1012// Among equally cheap arcs into a node, the one with the lowest index in the
1013// snapshot wins, whether the step visits the arcs of the reached nodes only
1014// (many arcs elsewhere) or scans them all (few arcs).
1016{
1017 for (const bool padded : {false, true})
1018 {
1019 // 0 -> 1 and 0 -> 2 are reached in step 1, in that order; 2 -> 3 is
1020 // inserted before 1 -> 3 and both cost the same, so 3 must be reached
1021 // through 2: the cycle is 0 -> 2 -> 3 -> 0.
1022 std::vector<Edge_Def> edges = {{0, 1, 1}, {0, 2, 1}, {2, 3, 1}, {1, 3, 1}, {3, 0, -5}};
1023 if (padded) // a positive complete digraph that the origin 0 never reaches
1024 for (size_t u = 4; u < 12; ++u)
1025 for (size_t v = 4; v < 12; ++v)
1026 if (u != v)
1027 edges.emplace_back(u, v, 5);
1028 auto built = build_graph(padded ? 12 : 4, edges);
1029
1030 const auto r = most_negative_cycle_bounded(built.g, 3);
1031 ASSERT_TRUE(r.has_cycle) << "padded=" << padded;
1032 EXPECT_EQ(r.total_cost, -3) << "padded=" << padded;
1033 ASSERT_EQ(r.length, 3u) << "padded=" << padded;
1034 ASSERT_TRUE(witness_is_simple_cycle(built.g, r)) << "padded=" << padded;
1035
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());
1039 EXPECT_EQ(ids, (std::vector<int>{0, 2, 3, 0})) << "padded=" << padded;
1040 }
1041}
1042
1043
1045{
1046 for (const bool padded : {false, true})
1047 {
1048 // Two parallel arcs 0 -> 1 of the same cost, inserted after 1 -> 0.
1049 std::vector<Edge_Def> edges = {{1, 0, -5}, {0, 1, 1}, {0, 1, 1}};
1050 if (padded)
1051 for (size_t u = 2; u < 10; ++u)
1052 for (size_t v = 2; v < 10; ++v)
1053 if (u != v)
1054 edges.emplace_back(u, v, 5);
1055 auto built = build_graph(padded ? 10 : 2, edges);
1056
1057 const auto r = most_negative_cycle_bounded(built.g, 2);
1058 ASSERT_TRUE(r.has_cycle) << "padded=" << padded;
1059 EXPECT_EQ(r.total_cost, -4) << "padded=" << padded;
1060 ASSERT_EQ(r.length, 2u) << "padded=" << padded;
1061 EXPECT_TRUE(witness_is_simple_cycle(built.g, r)) << "padded=" << padded;
1062
1063 bool uses_first = false;
1064 bool uses_second = false;
1065 for (auto it = r.cycle_arcs.get_it(); it.has_curr(); it.next_ne())
1066 {
1067 uses_first = uses_first or it.get_curr() == built.arcs[1];
1068 uses_second = uses_second or it.get_curr() == built.arcs[2];
1069 }
1070 EXPECT_TRUE(uses_first) << "padded=" << padded;
1071 EXPECT_FALSE(uses_second) << "padded=" << padded;
1072 }
1073}
1074
1075
1077{
1078 // A random core plus nodes that feed it, nodes it feeds, isolated nodes and
1079 // an occasional self-loop on a fringe node: the reduction to the cyclic
1080 // core must change nothing observable.
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);
1089
1090 size_t results = 0;
1091 size_t exact_results = 0;
1092 for (size_t trial = 0; trial < 300; ++trial)
1093 {
1094 const size_t c = static_cast<size_t>(core_dist(rng));
1095 const size_t tin = static_cast<size_t>(fringe_dist(rng));
1096 const size_t tout = static_cast<size_t>(fringe_dist(rng));
1097 const size_t iso = static_cast<size_t>(fringe_dist(rng));
1098 const size_t n = c + tin + tout + iso;
1099 const size_t L = static_cast<size_t>(len_dist(rng));
1100
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))); };
1104
1105 for (size_t u = 0; u < c; ++u)
1106 for (size_t v = 0; v < c; ++v)
1107 if ((u == v) ? has_loop(rng) : has_edge(rng))
1108 add(u, v);
1109 for (size_t i = 0; i < tin; ++i) // feeders: into the core, forward among themselves
1110 {
1111 for (size_t u = 0; u < c; ++u)
1112 if (coin(rng))
1113 add(c + i, u);
1114 if (i > 0 and coin(rng))
1115 add(c + i - 1, c + i);
1116 }
1117 for (size_t i = 0; i < tout; ++i) // fed by the core, forward among themselves
1118 {
1119 for (size_t u = 0; u < c; ++u)
1120 if (coin(rng))
1121 add(u, c + tin + i);
1122 if (i > 0 and coin(rng))
1123 add(c + tin + i - 1, c + tin + i);
1124 }
1125 if (tin > 0 and tout > 0 and coin(rng)) // a bridge that skips the core
1126 add(c, c + tin);
1127 if (n > c and has_loop(rng)) // a cyclic component made of one node
1128 edges.emplace_back(c, c, static_cast<long long>(weight_dist(rng)));
1129
1130 auto built = build_graph(n, edges);
1131 const Oracle oracle = exact_bounded_minimum(built, L);
1132 const auto r = most_negative_cycle_bounded(built.g, L);
1133
1134 ASSERT_EQ(r.has_cycle, oracle.exists) << "trial=" << trial;
1135 ASSERT_TRUE(witness_is_simple_cycle(built.g, r)) << "trial=" << trial;
1136 if (not r.has_cycle)
1137 continue;
1138
1139 ++results;
1140 ASSERT_LE(r.length, L) << "trial=" << trial;
1141 ASSERT_GE(r.total_cost, oracle.min_cost) << "trial=" << trial;
1142 if (r.is_exact)
1143 {
1144 ++exact_results;
1145 ASSERT_EQ(r.total_cost, oracle.min_cost) << "trial=" << trial;
1146 }
1147 else if (oracle.min_cost < 0)
1148 ASSERT_LT(r.total_cost, 0) << "trial=" << trial;
1149 }
1150 EXPECT_GT(results, 100u);
1151 EXPECT_GT(exact_results * 10, results * 7);
1152}
1153
1154
1156{
1157 // 0 <-> 1 with a negative self-loop on 1: a closed walk that goes around
1158 // the loop several times beats the simple loop, so the certificate needs
1159 // the walks of at least 3 arcs to have been examined. `L` is
1160 // min(max_length, V) with V the node count of the *whole* graph, isolated
1161 // nodes included, and not of the cyclic core: with V = 2 the bound is 2 and
1162 // the certificate holds; with 4 isolated nodes added it is 4 and it does not.
1163 const std::vector<Edge_Def> edges = {{0, 1, -1}, {1, 0, -1}, {1, 1, -5}};
1164
1165 auto small = build_graph(2, edges);
1166 const auto r_small = most_negative_cycle_bounded(small.g, 4);
1167 ASSERT_TRUE(r_small.has_cycle);
1168 EXPECT_EQ(r_small.total_cost, -5);
1169 EXPECT_TRUE(r_small.is_exact);
1170
1171 auto padded = build_graph(6, edges);
1172 const auto r_padded = most_negative_cycle_bounded(padded.g, 4);
1173 ASSERT_TRUE(r_padded.has_cycle);
1174 EXPECT_EQ(r_padded.total_cost, -5); // the same cycle...
1175 EXPECT_EQ(r_padded.length, 1u);
1176 EXPECT_FALSE(r_padded.is_exact); // ...but a cheaper walk exists in the larger bound
1177}
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
WeightedDigraph::Arc Arc
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
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.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
#define TEST(name)
static mt19937 rng
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.
Definition ahPair.H:89
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....
#define LL
Definition ran_array.c:24
Result of most_negative_cycle_bounded().
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Gsl_Compatible_Rng r
Array-based graph implementation.
Generic graph and digraph implementations.