57#include <gtest/gtest.h>
78 std::set<int> arc_numbers(
const List &
l)
81 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
82 r.insert(it.get_curr()->get_info());
97 s.
arcs = {{0, 1}, {2, 3}, {1, 2}, {1, 2}};
103 template <
class GT,
class SA>
104 constexpr int ladder_cost = hides<GT, SA> ? 4 : 3;
108 template <
class GT,
class SA>
109 std::set<int> ladder_arcs()
111 return hides<GT, SA> ? std::set<int>{0, 1, 3} : std::set<int>{0, 1, 2};
116 std::set<int> path_arcs(
const Path<GT> & p)
126 std::set<int> graph_arcs(
const GT & g)
129 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
130 r.insert(it.get_curr_ne()->get_info());
136 std::set<int> painted_arcs(
const GT & g)
139 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
141 r.insert(it.get_curr_ne()->get_info());
153 s.
arcs = {{0, 1}, {2, 3}, {1, 2}, {3, 0}, {1, 3}};
158 template <
class List>
159 bool some_path_has_hidden(
const List &
l)
161 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
164 it.get_curr().path.for_each_arc([&has] (
auto a) { has = has or a->get_info() ==
Hidden; });
181 using GT = TypeParam;
183 for_each_filter<GT>([&] (
auto sa,
const char * name)
185 using SA =
decltype(sa);
188 auto * s = b.
nodes[0];
189 auto * t = b.
nodes[3];
191 const int cost = ladder_cost<GT, SA>;
192 const auto arcs = ladder_arcs<GT, SA>();
199 Floyd y(g, std::move(moved));
200 Floyd z(g, Dft_Dist<GT>(), sa);
201 Floyd_All_Shortest_Paths w(g, Dft_Dist<GT>(), sa);
202 static_assert(std::is_same_v<decltype(w), Floyd>);
203 for (const Floyd * f : {&x, &y, &z, &w})
205 EXPECT_FALSE(f->has_negative_cycle());
206 const long i = f->index_node(s);
207 const long j = f->index_node(t);
208 EXPECT_EQ(f->select_node(i), s);
209 EXPECT_EQ(f->get_dist_mat()(i, j), cost);
210 EXPECT_EQ(path_arcs(f->get_min_path(s, t)),
arcs);
211 EXPECT_EQ(path_arcs(f->get_min_path(i, j)),
arcs);
213 EXPECT_EQ(x.get_nodes().size(), 4U);
214 EXPECT_EQ(x.get_nodes_ref().size(), 4U);
215 EXPECT_EQ(x.get_nodes_copy().size(), 4U);
216 EXPECT_EQ(x.get_path_mat()(x.index_node(s), x.index_node(s)), x.index_node(s));
217 EXPECT_EQ(x.entry(cost), std::to_string(cost));
218 testing::internal::CaptureStdout();
219 x.print(x.get_dist_mat());
220 EXPECT_NE(testing::internal::GetCapturedStdout().
find(std::to_string(cost)),
228 using GT = TypeParam;
230 for_each_filter<GT>([&] (
auto sa,
const char * name)
232 using SA =
decltype(sa);
234 auto * s = b.
nodes[0];
235 auto * t = b.
nodes[3];
237 constexpr bool hidden = hides<GT, SA>;
238 const int cost = ladder_cost<GT, SA>;
239 const auto arcs = ladder_arcs<GT, SA>();
242 const auto explicit_args =
243 yen_k_shortest_paths<GT, Dft_Dist<GT>, SA>(g, s, t, 2, Dft_Dist<GT>(), sa);
244 const auto deduced = yen_k_shortest_paths(g, s, t, 2, Dft_Dist<GT>(), sa);
245 const Yen_K_Shortest_Paths<GT, Dft_Dist<GT>, SA> yen(Dft_Dist<GT>(), sa);
246 const auto by_object = yen(g, s, t, 2);
247 for (const auto * l : {&explicit_args, &deduced, &by_object})
251 ASSERT_EQ(
l->
size(), hidden ? 1U : 2U);
254 EXPECT_EQ(some_path_has_hidden(*
l), not hidden);
262 using GT = TypeParam;
264 for_each_filter<GT>([&] (
auto sa,
const char * name)
266 using SA =
decltype(sa);
268 auto * s = b.
nodes[0];
269 auto * t = b.
nodes[3];
271 const int cost = ladder_cost<GT, SA>;
272 const auto arcs = ladder_arcs<GT, SA>();
275 const auto explicit_args =
276 eppstein_k_shortest_paths<GT, Dft_Dist<GT>, SA>(g, s, t, 2, Dft_Dist<GT>(), sa);
277 const auto deduced = eppstein_k_shortest_paths(g, s, t, 2, Dft_Dist<GT>(), sa);
278 const Eppstein_K_Shortest_Paths<GT, Dft_Dist<GT>, SA> eppstein(Dft_Dist<GT>(), sa);
279 const auto by_object = eppstein(g, s, t, 2);
280 for (const auto * l : {&explicit_args, &deduced, &by_object})
288 EXPECT_FALSE(some_path_has_hidden(*
l));
304 using GT = TypeParam;
306 for_each_filter<GT>([&] (
auto sa,
const char * name)
308 using SA =
decltype(sa);
310 auto * s = b.
nodes[0];
311 auto * t = b.
nodes[3];
313 const int cost = hides<GT, SA> ? 4 : 3;
314 const auto arcs = hides<GT, SA> ? std::set<int>{0, 4} : std::set<int>{0, 1, 2};
317 Johnson<GT, Dft_Dist<GT>, Arc_Iterator, Node_Arc_Iterator, SA> x(g, Dft_Dist<GT>(), sa);
318 EXPECT_TRUE(x.is_initialized());
319 EXPECT_EQ(x.get_distance(s, t), cost);
320 EXPECT_EQ(path_arcs(x.find_path(s, t)), arcs);
321 const auto all = x.compute_all_pairs_distances();
322 EXPECT_EQ(all.find(std::make_pair(s, t)), cost);
323 EXPECT_EQ(x.get_potential(s), 0);
324 EXPECT_EQ(x.get_reweighted_graph().get_num_nodes(), 4U);
325 EXPECT_EQ(std::as_const(x).get_reweighted_graph().get_num_arcs(), 5U);
326 EXPECT_NE(x.get_copy_node(s), nullptr);
333 using GT = TypeParam;
341 spec.
arcs = {{0, 1}, {1, 2}, {0, 2}, {2, 3}, {1, 3}};
343 const int weight[] = {-1, 5, 2, 0, 3};
344 for (
size_t i = 0; i < b.
arcs.size(); ++i)
345 b.
arcs[i]->get_info() = weight[i];
351 Check johnson(
"Johnson with a filter that reads the infos of the arcs");
352 Check bellman_ford(
"Bellman_Ford on the same graph and filter");
354 for_each_filter<GT>([&] (
auto sa,
const char * name)
356 using SA =
decltype(sa);
357 if constexpr (hides<GT, SA>)
363 johnson.
fail(spec, std::string(name) +
": distance " + std::to_string(d) +
", not 2");
367 y.paint_spanning_tree(b.
nodes[0]);
368 const int e =
y.get_distance(b.
nodes[3]);
370 bellman_ford.
fail(spec, std::string(name) +
": distance " + std::to_string(e) +
", not 2");
387 using GT = TypeParam;
389 for_each_filter<GT>([&] (
auto sa,
const char * name)
391 using SA =
decltype(sa);
394 const auto arcs = ladder_arcs<GT, SA>();
397 Kruskal_Min_Spanning_Tree<GT, Dft_Dist<GT>, SA> x(Dft_Dist<GT>(), sa);
398 x.paint_min_spanning_tree(g);
399 EXPECT_TRUE(x.is_painted());
400 EXPECT_EQ(painted_arcs(g), arcs);
402 x.paint_min_spanning_tree(g, tree);
403 EXPECT_EQ(graph_arcs(tree), arcs);
405 EXPECT_EQ(graph_arcs(tree), arcs);
407 EXPECT_EQ(painted_arcs(g), arcs);
414 using GT = TypeParam;
416 for_each_filter<GT>([&] (
auto sa,
const char * name)
418 using SA =
decltype(sa);
421 const auto arcs = ladder_arcs<GT, SA>();
424 Prim_Min_Spanning_Tree<GT, Dft_Dist<GT>, SA> x(Dft_Dist<GT>(), sa);
427 EXPECT_EQ(graph_arcs(tree), arcs);
428 x(g, b.nodes[3], tree);
429 EXPECT_EQ(graph_arcs(tree), arcs);
431 EXPECT_EQ(painted_arcs(g), arcs);
433 EXPECT_EQ(painted_arcs(g), arcs);
Floyd-Warshall algorithm for all-pairs shortest paths.
Johnson's algorithm for all-pairs shortest paths.
K-shortest path algorithms (Yen and Eppstein-style API).
Kruskal's minimum spanning tree algorithm.
Prim's algorithm for minimum spanning trees.
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.
T & get_first()
Return the first item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
constexpr bool is_empty() const noexcept
Johnson's algorithm for all-pairs shortest paths.
Distance_Type get_distance(Node *src, Node *tgt)
Get the shortest distance from source to target.
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.
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractPathsAllPairs, FloydAllShortestPaths)
TYPED_TEST_SUITE(ContractPathsAllPairs, All_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
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
Main namespace for Aleph-w library functions.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element 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.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > >, 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 > > > All_Kinds
The six kinds, with int infos.
Filtered iterator on all the arcs of a graph.
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.