56#include <gtest/gtest.h>
76 std::set<int> arc_numbers(
const List &
l)
79 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
80 r.insert(it.get_curr()->get_info());
95 s.
arcs = {{0, 1}, {2, 3}, {1, 2}, {1, 2}};
101 template <
class GT,
class SA>
102 constexpr int ladder_cost = hides<GT, SA> ? 4 : 3;
106 template <
class GT,
class SA>
107 std::set<int> ladder_arcs()
109 return hides<GT, SA> ? std::set<int>{0, 1, 3} : std::set<int>{0, 1, 2};
117 s.
arcs = {{0, 1}, {1, 2}, {2, 0}};
128 b.
arcs[0]->get_info() = -3;
135 s.
arcs = {{0, 1}, {1, 2}, {1, 0}};
141 std::set<int> path_arcs(
const Path<GT> & p)
151 std::set<int> graph_arcs(
const GT & g)
154 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
155 r.insert(it.get_curr_ne()->get_info());
169 using GT = TypeParam;
171 for_each_filter<GT>([&] (
auto sa,
const char * name)
173 using SA =
decltype(sa);
179 constexpr bool hidden = hides<GT, SA>;
180 const int cost = ladder_cost<GT, SA>;
181 const auto arcs = ladder_arcs<GT, SA>();
187 EXPECT_FALSE(x.has_computation());
188 EXPECT_EQ(&x.get_graph(), &
l.g);
189 EXPECT_FALSE(x.paint_spanning_tree(
l.nodes[0]));
190 EXPECT_TRUE(x.is_painted());
191 EXPECT_EQ(x.get_start_node(),
l.nodes[0]);
192 EXPECT_EQ(x.get_distance(
l.nodes[3]), cost);
194 EXPECT_EQ(x.get_min_path(
l.nodes[3], path), cost);
195 EXPECT_EQ(path_arcs(path),
arcs);
196 std::set<int> tree_arcs;
197 const auto tree_array = x.extract_min_spanning_tree();
198 for (
size_t i = 0; i < tree_array.size(); ++i)
199 if (tree_array(i) !=
nullptr)
200 tree_arcs.insert(tree_array(i)->get_info());
201 EXPECT_EQ(tree_arcs,
arcs);
203 x.build_tree(tree,
true);
204 EXPECT_EQ(graph_arcs(tree),
arcs);
208 EXPECT_FALSE(x.faster_paint_spanning_tree(
l.nodes[0]));
209 EXPECT_EQ(x.get_distance(
l.nodes[3]), cost);
211 EXPECT_FALSE(x.is_painted());
212 EXPECT_EQ(x.compute_nodes_weights().size(), 4U);
217 EXPECT_EQ(x.has_negative_cycle(neg.
nodes[0]), not hidden);
218 EXPECT_EQ(x.has_negative_cycle(), not hidden);
219 if constexpr (hidden)
221 EXPECT_FALSE(x.paint_spanning_tree(neg.
nodes[0]));
222 EXPECT_EQ(x.get_distance(neg.
nodes[2]), -2);
225 EXPECT_EQ(x.test_negative_cycle(neg.
nodes[0]).is_empty(), hidden);
226 EXPECT_EQ(x.test_negative_cycle().is_empty(), hidden);
227 EXPECT_EQ(std::get<0>(x.search_negative_cycle(neg.
nodes[0], 0.7, 1)).is_empty(), hidden);
228 EXPECT_EQ(x.search_negative_cycle(neg.
nodes[0]).is_empty(), hidden);
229 EXPECT_EQ(std::get<0>(x.search_negative_cycle(0.7, 1)).is_empty(), hidden);
230 EXPECT_EQ(x.search_negative_cycle().is_empty(), hidden);
232 EXPECT_EQ(x.test_negative_cycle(neg.
nodes[0], cycle), not hidden);
233 EXPECT_EQ(x.test_negative_cycle(cycle), not hidden);
234 EXPECT_EQ(path_arcs(cycle).
count(
Hidden), hidden ? 0U : 1U);
240 using GT = TypeParam;
242 for_each_filter<GT>([&] (
auto sa,
const char * name)
244 using SA =
decltype(sa);
247 auto * s = neg.
nodes[0];
249 constexpr bool hidden = hides<GT, SA>;
257 EXPECT_EQ(x(neg.
g, path, d, sa), not hidden);
258 EXPECT_EQ(x(neg.
g, s, path, d, sa), not hidden);
259 EXPECT_EQ(x(neg.
g, s, d, sa).is_empty(), hidden);
260 EXPECT_EQ(x(neg.
g, d, sa).is_empty(), hidden);
263 EXPECT_EQ(x(neg.
g, path,
Dft_Dist<GT>(), SA(sa)), not hidden);
264 EXPECT_EQ(x(neg.
g, s, path,
Dft_Dist<GT>(), SA(sa)), not hidden);
265 EXPECT_EQ(x(neg.
g, s,
Dft_Dist<GT>(), SA(sa)).is_empty(), hidden);
266 EXPECT_EQ(x(neg.
g,
Dft_Dist<GT>(), SA(sa)).is_empty(), hidden);
272 using GT = TypeParam;
274 for_each_filter<GT>([&] (
auto sa,
const char * name)
276 using SA =
decltype(sa);
279 constexpr bool hidden = hides<GT, SA>;
282 const auto r = karp_minimum_mean_cycle(g, Dft_Dist<GT>(), sa);
283 EXPECT_EQ(r.has_cycle, not hidden);
286 EXPECT_EQ(r.minimum_mean, 1.0L);
287 EXPECT_EQ(arc_numbers(r.cycle_arcs), (std::set<int>{0, 1, 2}));
294 EXPECT_EQ(karp(g).
has_cycle, not hidden);
303 using GT = TypeParam;
305 for_each_filter<GT>([&] (
auto sa,
const char * name)
307 using SA =
decltype(sa);
310 constexpr bool hidden = hides<GT, SA>;
313 const auto r = howard_minimum_mean_cycle(g, Dft_Dist<GT>(), sa);
314 EXPECT_EQ(r.has_cycle, not hidden);
317 EXPECT_EQ(r.minimum_mean, 1.0L);
318 EXPECT_EQ(arc_numbers(r.cycle_arcs), (std::set<int>{0, 1, 2}));
331 using GT = TypeParam;
333 for_each_filter<GT>([&] (
auto sa,
const char * name)
335 using SA =
decltype(sa);
339 constexpr bool hidden = hides<GT, SA>;
342 const auto cycles = find_disjoint_negative_cycles(g, 2, Negative_Cycle_Exclusion::Min_Weight_Arc,
344 ASSERT_EQ(cycles.size(), hidden ? 0U : 1U);
347 EXPECT_EQ(cycles.get_first().total_cost, -1);
Bellman-Ford algorithm for single-source shortest paths.
Minimum mean cycle by Howard's policy iteration.
Minimum mean cycle in directed graphs (Karp algorithm).
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
size_t size_t int32_t value
Bellman-Ford algorithm for shortest paths with negative weights.
Default distance accessor for arc weights.
Node belonging to a double circular linked list with header node.
Functor wrapper for value-only minimum mean cycle.
Functor wrapper for Karp minimum mean cycle.
Filtered iterator for outcoming arcs of a node.
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
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.
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractPathsCycles, BellmanFord)
TYPED_TEST_SUITE(ContractPathsCycles, Directed_Kinds)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
DynArray< Graph::Arc * > arcs
Min_Mean_Cycle_Value_Result minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Alias for karp_minimum_mean_cycle_value().
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 > minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Alias for karp_minimum_mean_cycle().
Min_Mean_Cycle_Value_Result karp_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute only the minimum mean value (without witness walk).
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.
Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > howard_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle by Howard's policy iteration.
Min_Mean_Cycle_Value_Result howard_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max())
Minimum mean cycle value by Howard's policy iteration, without the witness.
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
constexpr int Hidden
The number of the arc that the filters hide.
::testing::Types< Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Directed_Kinds
The directed kinds, with int infos.
Filtered iterator on all the arcs of a graph.
Detects if a negative cycle exists and eventually computes it.
Filtered iterator of adjacent arcs of a node.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
std::vector< typename GT::Arc * > arcs
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
Generic graph and digraph implementations.