40# include <gtest/gtest.h>
49# include <type_traits>
67 template <
class GT,
typename Cost>
77 std::set<typename GT::Node *> seen;
82 for (
auto it =
arcs.get_it(); it.has_curr(); it.next_ne())
84 typename GT::Arc * arc = it.get_curr();
94 if constexpr (std::is_floating_point_v<Cost>)
97 return std::fabs(
static_cast<long double>(sum) -
static_cast<long double>(item.
total_cost))
98 <= (item.
length + 1) *
static_cast<long double>(std::numeric_limits<Cost>::epsilon()) *
abs_sum
104 template <
class GT,
typename Cost>
107 std::set<typename GT::Arc *> s;
108 item.
cycle.for_each_arc([&s](
typename GT::Arc * a) { s.insert(a); });
112 template <
class GT,
typename Cost>
116 std::vector<std::set<typename GT::Arc *>>
sets;
117 for (
auto it = items.get_it(); it.has_curr(); it.next_ne())
118 sets.push_back(arc_set(it.get_curr()));
120 for (
size_t i = 0; i <
sets.size(); ++i)
121 for (
size_t j = i + 1; j <
sets.size(); ++j)
126 for (
auto * a :
sets[i])
136 const std::function<
bool(
Arc *)> &
allow)
138 const size_t n = built.
nodes.size();
139 std::vector<bool> visited(n,
false);
142 std::function<
void(
size_t,
size_t,
long long)>
dfs =
143 [&](
const size_t start,
const size_t u,
const long long cost)
149 Arc * arc = it.get_current_arc_ne();
152 const size_t v =
static_cast<size_t>(it.get_tgt_node()->get_info());
160 if (v < start
or visited[v])
163 dfs(start, v, cost +
w);
168 for (
size_t s = 0; s < n
and not found; ++s)
179 template <
typename W>
184 3, {{0, 1, W(1)}, {1, 2, W(-3)}, {2, 0, W(1)}});
189 EXPECT_EQ(cycles.get_first().total_cost, W(-1));
190 EXPECT_EQ(cycles.get_first().length, 3u);
198 const std::vector<Edge_Def> edges = {
199 {0, 1, 1}, {1, 2, -3}, {2, 0, 1},
201 {3, 4, -2}, {4, 3, 1}
205 for (
const auto policy : {Negative_Cycle_Exclusion::Min_Weight_Arc,
206 Negative_Cycle_Exclusion::All_Arcs})
211 std::multiset<size_t> lengths;
212 for (
auto it = cycles.get_it(); it.has_curr(); it.next_ne())
214 const Item & item = it.get_curr();
217 lengths.insert(item.length);
219 EXPECT_EQ(lengths, (std::multiset<size_t>{2, 3}));
227 const std::vector<Edge_Def> edges = {
228 {0, 1, 2}, {1, 2, -5}, {2, 0, 1},
236 EXPECT_EQ(cycles.get_first().total_cost, -2);
237 EXPECT_EQ(cycles.get_first().length, 3u);
243 const std::vector<Edge_Def>
positive = {
244 {0, 1, 1}, {1, 2, 1}, {2, 0, 1}, {1, 0, 3}
249 const std::vector<Edge_Def> dag = {
250 {0, 1, -4}, {1, 2, -4}, {0, 2, -9}
262 const std::vector<Edge_Def> edges = {{0, 1, -1}, {1, 0, -1}};
282 using Distance_Type =
long long;
284 Distance_Type operator()(
Arc * a)
const { ++*calls;
return a->get_info(); }
286 auto built =
build_graph(2, {{0, 1, -1}, {1, 0, -1}});
289 built.
g, 0, Negative_Cycle_Exclusion::Min_Weight_Arc,
Counting_Dist{&calls})).is_empty());
297 const std::vector<Edge_Def> edges = {
298 {0, 1, -1}, {1, 0, 0},
299 {2, 3, -1}, {3, 2, 0},
300 {4, 5, -1}, {5, 4, 0},
314 const std::vector<Edge_Def> edges = {
315 {0, 1, -2}, {1, 0, 1},
316 {0, 2, -3}, {2, 0, 1}
320 for (
const auto policy : {Negative_Cycle_Exclusion::Min_Weight_Arc,
321 Negative_Cycle_Exclusion::All_Arcs})
325 for (
auto it = cycles.get_it(); it.has_curr(); it.next_ne())
336 const std::vector<Edge_Def> edges = {
338 {1, 2, -5}, {2, 0, 1},
339 {1, 3, -2}, {3, 0, -1}
344 built.
g, 10, Negative_Cycle_Exclusion::Min_Weight_Arc);
346 for (
auto it = min_arc.get_it(); it.has_curr(); it.next_ne())
351 built.
g, 10, Negative_Cycle_Exclusion::All_Arcs);
361 auto built =
build_graph(2, {{0, 1, 5}, {0, 1, -4}, {1, 0, 1}});
365 const Item & item = cycles.get_first();
375 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}});
389 3, {{0, 1, -0.75}, {1, 2, 0.25}, {2, 0, 0.25}, {2, 1, 0.5}});
394 EXPECT_NEAR(cycles.get_first().total_cost, -0.25, 1e-12);
401 3, {{0, 1, -4}, {1, 0, 1}, {1, 2, 3}, {2, 1, 3}});
406 EXPECT_EQ(cycles.get_first().total_cost, -3);
421 inf_graph.insert_arc(
i0,
i1, -std::numeric_limits<double>::infinity());
428 nan_graph.insert_arc(n0, n1, std::numeric_limits<double>::quiet_NaN());
433 auto overflow =
build_graph(2, {{0, 1, -5}, {1, 0, std::numeric_limits<long long>::min()}});
440 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}});
450 for (
auto * a : built.
arcs)
452 for (
auto * p : built.
nodes)
477 auto * s = g.insert_node(0);
478 auto * v = g.insert_node(1);
481 g.insert_arc(s, v, -1
e16);
482 g.insert_arc(v, v, -1.0);
486 g.insert_arc(v, v, -1.0);
487 g.insert_arc(s, v, -1
e16);
509 auto * s = g.insert_node(0);
510 auto * v = g.insert_node(1);
511 g.insert_arc(s, v, -1
e16);
512 g.insert_arc(v, s, 1
e16);
513 g.insert_arc(v, v, -1.0);
535 const std::vector<std::vector<double>>
rings = {
540 {-1
e16, 4.0, -7.0, 1
e16, 3.0}
543 for (
const auto & weights :
rings)
546 for (
const double w : weights)
551 std::vector<Float_Graph::Node *>
nodes;
552 for (
size_t i = 0; i < weights.size(); ++i)
553 nodes.push_back(g.insert_node(
static_cast<int>(i)));
554 for (
size_t i = 0; i < weights.size(); ++i)
555 g.insert_arc(
nodes[i],
nodes[(i + 1) % weights.size()], weights[i]);
557 for (
const auto policy : {Negative_Cycle_Exclusion::Min_Weight_Arc,
558 Negative_Cycle_Exclusion::All_Arcs})
568 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {0, 2, 7}, {2, 2, 4}});
573 built.
g, 4, Negative_Cycle_Exclusion::All_Arcs,
Counting_Dist{&dist_calls},
585 auto * a = g.insert_node(0);
586 auto * b = g.insert_node(1);
587 auto * c = g.insert_node(2);
588 g.insert_arc(a, b, -2.0);
589 g.insert_arc(b, a, 1.0);
590 g.insert_arc(b, c, std::numeric_limits<double>::infinity());
591 g.insert_arc(c, a, std::numeric_limits<double>::quiet_NaN());
600 EXPECT_NEAR(cycles.get_first().total_cost, -1.0, 1e-12);
606 auto built =
build_graph(4, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {2, 3, -2}, {3, 2, 1}});
611 for (
const auto policy : {Negative_Cycle_Exclusion::Min_Weight_Arc,
612 Negative_Cycle_Exclusion::All_Arcs})
627 EXPECT_EQ(found.get_first().total_cost, -3);
630 for (
const long long w : {0
LL, 3LL})
645 auto built =
build_graph(7, {{0, 1, 2}, {1, 0, 2}, {3, 4, -2}, {4, 3, 1}, {5, 6, -9}});
650 EXPECT_EQ(cycles.get_first().total_cost, -1);
672 const int int_min = std::numeric_limits<int>::min();
673 const int int_max = std::numeric_limits<int>::max();
674 const long double ld_lowest = std::numeric_limits<long double>::lowest();
675 const long double ld_max = std::numeric_limits<long double>::max();
705 std::mt19937_64
rng(0x5EED0C1CULL);
706 std::uniform_int_distribution<int>
n_dist(2, 7);
707 std::bernoulli_distribution
has_edge(0.4);
708 std::bernoulli_distribution has_loop(0.1);
709 std::uniform_int_distribution<int>
weight_dist(-9, 13);
714 const size_t n =
static_cast<size_t>(
n_dist(
rng));
715 std::vector<Edge_Def> edges;
716 for (
size_t u = 0; u < n; ++u)
717 for (
size_t v = 0; v < n; ++v)
721 edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng)));
729 for (
const auto policy : {Negative_Cycle_Exclusion::Min_Weight_Arc,
730 Negative_Cycle_Exclusion::All_Arcs})
735 for (
auto it = cycles.get_it(); it.has_curr(); it.next_ne())
738 cycles, policy == Negative_Cycle_Exclusion::All_Arcs)) <<
"trial=" <<
trial;
742 if (policy == Negative_Cycle_Exclusion::All_Arcs
and cycles.size() < 10)
744 std::set<Arc *> used;
745 for (
auto it = cycles.get_it(); it.has_curr(); it.next_ne())
746 for (
auto * a : arc_set(it.get_curr()))
749 built, [&used](
Arc * a) {
return used.count(a) == 0; })) <<
"trial=" <<
trial;
765 constexpr size_t ring = 5000;
766 std::vector<Edge_Def> edges;
767 for (
size_t i = 0; i <
ring; ++i)
768 edges.emplace_back(i, (i + 1) %
ring, i == 0 ? -
static_cast<long long>(
ring) : 1);
774 EXPECT_EQ(cycles.get_first().total_cost, -1);
777 constexpr size_t crowd = 20000;
782 EXPECT_EQ(loops.get_first().cycle.get_first_node(),
sparse.nodes[12345]);
794 EXPECT_EQ(cycles.get_first().total_cost, -1);
807 for (
size_t i = 0; i < built.
nodes.size(); ++i)
808 index.insert(built.
nodes[i], 1000 + i);
810 for (
size_t i = 0; i < built.
nodes.size(); ++i)
813 EXPECT_THROW((
void) index.find(
nullptr), std::domain_error);
815 EXPECT_THROW(index.insert(
nullptr, 0), std::domain_error);
832 std::vector<Edge_Def> edges = {{0, 1, 2}, {1, 2, -5}, {2, 0, 2},
833 {3, 4, 1}, {4, 5, -4}, {5, 3, 2},
834 {2, 3, 1}, {5, 0, 1}, {1, 4, 3}, {4, 1, 3}};
835 for (
size_t u = 6; u < 40; ++u)
837 edges.emplace_back(u, (u + 1) % 40, 1);
838 edges.emplace_back(u, (u * 7 + 3) % 40, 2);
851 auto ia = a.get_it();
853 for (;
ia.has_curr();
ia.next_ne(), ir.next_ne())
854 if (
ia.get_curr().total_cost != ir.get_curr().total_cost
855 or ia.get_curr().cycle.arcs() != ir.get_curr().cycle.arcs())
860 constexpr size_t threads = 8;
861 constexpr size_t rounds = 25;
862 std::vector<int> failures(threads, 0);
863 std::vector<std::thread> pool;
864 for (
size_t t = 0; t < threads; ++t)
865 pool.emplace_back([&, t]
867 for (size_t i = 0; i < rounds; ++i)
869 const auto cycles = find_disjoint_negative_cycles(built.g, 5);
870 const auto bounded = most_negative_cycle_bounded(built.g, 6);
871 if (not same_cycles(cycles) or bounded.total_cost != bounded_ref.total_cost
872 or bounded.length != bounded_ref.length
873 or bounded.is_exact != bounded_ref.is_exact
874 or bounded.cycle_arcs != bounded_ref.cycle_arcs)
878 for (
auto &
th : pool)
881 for (
size_t t = 0; t < threads; ++t)
882 EXPECT_EQ(failures[t], 0) <<
"thread " << t;
Bellman-Ford algorithm for single-source shortest paths.
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
Bellman-Ford algorithm for shortest paths with negative weights.
bool has_negative_cycle(Node *start)
Test if a negative cycle exists starting from a specific node.
Generic directed graph (digraph) wrapper template.
void insert(Dlink *node) noexcept
Insert node after this.
Doubly-linked list (defined in tpl_dynList.H).
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)
Minimal std::expected-style result type for C++20.
Map from node pointers to their positions, built once and never modified afterwards.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
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...
#define ARC_COOKIE(p)
Return the arc cookie
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...
#define NODE_COOKIE(p)
Return the node cookie
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.
and
Check uniqueness with explicit hash + equality functors.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
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....
One negative cycle reported by find_disjoint_negative_cycles().
Path< GT > cycle
Closed path: is_cycle() holds, every arc is a graph arc.
Cost_Type total_cost
Sum of the arc weights, negative; for floating-point costs the exact sum, rounded once.
size_t length
Number of arcs.
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.