56#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};
119 s.
arcs = {{0, 1}, {1, 2}, {0, 2}};
125 std::set<int> path_arcs(
const Path<GT> & p)
135 std::set<int> graph_arcs(
const GT & g)
138 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
139 r.insert(it.get_curr_ne()->get_info());
153 using GT = TypeParam;
156 for_each_filter<GT>([&] (
auto sa,
const char * name)
158 using SA =
decltype(sa);
174 using GT = TypeParam;
176 for_each_filter<GT>([&] (
auto sa,
const char * name)
178 using SA =
decltype(sa);
181 auto * s = b.
nodes[0];
182 auto * t = b.
nodes[3];
184 const int cost = ladder_cost<GT, SA>;
185 const auto arcs = ladder_arcs<GT, SA>();
189 Dijkstra x(Dft_Dist<GT>(), sa);
191 EXPECT_EQ(x.find_min_path(g, s, t, path), cost);
192 EXPECT_EQ(path_arcs(path), arcs);
193 EXPECT_EQ(x(g, s, t, path), cost);
194 EXPECT_TRUE(x.paint_partial_min_paths_tree(g, s, t));
195 x.paint_min_paths_tree(g, s);
196 EXPECT_TRUE(x.has_computation());
197 EXPECT_TRUE(x.is_painted());
198 EXPECT_EQ(x.get_start_node(), s);
199 EXPECT_EQ(x.get_graph(), &b.g);
200 EXPECT_EQ(x.get_distance(t), cost);
201 EXPECT_EQ(x.get_min_path(t, path), cost);
202 EXPECT_EQ(path_arcs(path), arcs);
205 EXPECT_NE(x.compute_min_paths_tree(g, s, tree), nullptr);
206 EXPECT_EQ(graph_arcs(tree), arcs);
207 x.compute_partial_min_paths_tree(g, s, t, tree);
208 EXPECT_EQ(graph_arcs(tree), arcs);
210 EXPECT_EQ(graph_arcs(tree), arcs);
213 EXPECT_EQ(x.get_min_path(tree, t, on_tree), cost);
214 EXPECT_EQ(path_arcs(on_tree), arcs);
222 EXPECT_EQ(graph_arcs(tree),
arcs);
229 using GT = TypeParam;
232 for_each_filter<GT>([&] (
auto sa,
const char * name)
234 using SA =
decltype(sa);
237 auto * s = b.
nodes[0];
238 auto * t = b.
nodes[3];
240 const int cost = ladder_cost<GT, SA>;
241 const auto arcs = ladder_arcs<GT, SA>();
245 AStar x(Dft_Dist<GT>(), Heuristic(), sa);
247 EXPECT_EQ(x.find_path(g, s, t, path), cost);
248 EXPECT_EQ(path_arcs(path), arcs);
249 EXPECT_EQ(x(g, s, t, path), cost);
250 EXPECT_EQ(x.find_min_path(g, s, t, path), cost);
251 EXPECT_EQ(path_arcs(path), arcs);
252 EXPECT_TRUE(x.paint_partial_path(g, s, t));
253 EXPECT_TRUE(x.paint_path(g, s, t));
254 EXPECT_TRUE(x.paint_partial_min_paths_tree(g, s, t));
255 x.paint_min_paths_tree(g, s);
256 EXPECT_TRUE(x.has_computation());
257 EXPECT_TRUE(x.is_painted());
258 EXPECT_EQ(x.get_start_node(), s);
259 EXPECT_EQ(x.get_graph(), &b.g);
260 EXPECT_EQ(x.get_distance(t), cost);
261 EXPECT_EQ(x.get_min_path(t, path), cost);
262 EXPECT_EQ(path_arcs(path), arcs);
265 EXPECT_NE(x.compute_partial_path(g, s, t, tree), nullptr);
266 EXPECT_EQ(graph_arcs(tree), arcs);
267 EXPECT_NE(x.compute_path(g, s, t, tree), nullptr);
268 EXPECT_EQ(graph_arcs(tree), arcs);
269 x.compute_partial_min_paths_tree(g, s, t, tree);
270 EXPECT_EQ(graph_arcs(tree), arcs);
272 EXPECT_EQ(graph_arcs(tree), arcs);
273 EXPECT_NE(x.compute_min_paths_tree(g, s, tree), nullptr);
274 EXPECT_EQ(graph_arcs(tree), arcs);
277 EXPECT_EQ(x.get_min_path(tree, t, on_tree), cost);
278 EXPECT_EQ(path_arcs(on_tree), arcs);
283 x.paint_min_paths_tree(b.
g, s);
285 EXPECT_EQ(x.copy_painted_min_paths_tree(b.
g, tree), cost);
286 EXPECT_EQ(graph_arcs(tree),
arcs);
292 using GT = TypeParam;
294 const Spec spec = ladder();
296 auto * s = b.
nodes[0];
297 auto * t = b.
nodes[3];
302 for_each_filter<GT>([&] (
auto sa,
const char * name)
304 using SA =
decltype(sa);
305 if constexpr (hides<GT, SA>)
318 EXPECT_EQ(
y.find_path(b.
g, s, t, path), 4);
332 using GT = TypeParam;
335 for_each_filter<GT>([&] (
auto sa,
const char * name)
337 using SA =
decltype(sa);
339 auto * s = b.
nodes[0];
340 auto * t = b.
nodes[3];
342 const int cost = ladder_cost<GT, SA>;
343 const auto arcs = ladder_arcs<GT, SA>();
346 IDA_Star<GT, Dft_Dist<GT>, Heuristic, Node_Arc_Iterator, SA> x(Dft_Dist<GT>(), Heuristic(), sa);
348 EXPECT_EQ(x.find_path(g, s, t, path), cost);
349 EXPECT_EQ(path_arcs(path), arcs);
350 EXPECT_EQ(x(g, s, t, path), cost);
351 EXPECT_EQ(path_arcs(path), arcs);
358 using GT = TypeParam;
360 for_each_filter<GT>([&] (
auto sa,
const char * name)
362 using SA =
decltype(sa);
364 auto * s = b.
nodes[0];
365 auto * t = b.
nodes[3];
367 const int cost = ladder_cost<GT, SA>;
368 const auto arcs = ladder_arcs<GT, SA>();
371 Dial_Min_Paths<GT, Dft_Dist<GT>, Node_Arc_Iterator, SA> x(Dft_Dist<GT>(), sa);
373 EXPECT_EQ(x.find_min_path(g, s, t, path), cost);
374 EXPECT_EQ(path_arcs(path), arcs);
375 EXPECT_EQ(x(g, s, t, path), cost);
376 x.paint_min_paths_tree(g, s);
377 EXPECT_TRUE(x.is_painted());
378 EXPECT_EQ(x.get_start_node(), s);
379 EXPECT_EQ(x.get_graph(), &b.g);
380 EXPECT_EQ(x.get_distance(t), cost);
381 EXPECT_EQ(x.get_min_path(t, path), cost);
382 EXPECT_EQ(path_arcs(path), arcs);
389 using GT = TypeParam;
391 for_each_filter<GT>([&] (
auto sa,
const char * name)
393 using SA =
decltype(sa);
395 auto * s = b.
nodes[0];
396 auto * t = b.
nodes[2];
400 Zero_One_BFS<GT, Dft_Dist<GT>, Node_Arc_Iterator, SA> x(Dft_Dist<GT>(), sa);
402 if constexpr (not hides<GT, SA>)
404 EXPECT_THROW(x.find_min_path(g, s, t, path), std::domain_error);
405 EXPECT_THROW(x.paint_min_paths_tree(g, s), std::domain_error);
409 EXPECT_EQ(x.find_min_path(g, s, t, path), 1);
410 EXPECT_EQ(path_arcs(path), (std::set<int>{0, 1}));
411 EXPECT_EQ(x(g, s, t, path), 1);
412 x.paint_min_paths_tree(g, s);
413 EXPECT_TRUE(x.is_painted());
414 EXPECT_EQ(x.get_start_node(), s);
415 EXPECT_EQ(x.get_graph(), &b.
g);
416 EXPECT_EQ(x.get_distance(t), 1);
417 EXPECT_EQ(x.get_min_path(t, path), 1);
418 EXPECT_EQ(path_arcs(path), (std::set<int>{0, 1}));
A* shortest path algorithm.
Dial's algorithm for shortest paths with small integer weights.
Dijkstra's shortest path algorithm.
IDA* (Iterative Deepening A*) shortest path algorithm.
0-1 BFS shortest path algorithm.
A* algorithm for finding the shortest path between two nodes.
Default distance accessor for arc weights.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
void paint_min_paths_tree(const GT &g, typename GT::Node *start)
Paints on graph g the spanning tree of all shortest paths starting from start.
Distance::Distance_Type copy_painted_min_paths_tree(GT &g, GT &tree)
Extracts the painted shortest paths tree and puts it in tree.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes the shortest path between start and end by painting the graph.
bool is_painted() const noexcept
Check if the graph has been painted.
Node belonging to a double circular linked list with header node.
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Base class providing common infrastructure for shortest path algorithms.
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
bool is_painted() const noexcept
Check if the graph has been painted.
bool has_computation() const noexcept
Check if a computation has been performed.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance::Distance_Type get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
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_SUITE(ContractPathsSingleSource, All_Kinds)
TYPED_TEST(ContractPathsSingleSource, ShortestPathBase)
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
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_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.
Common utilities and base class for shortest path algorithms.
Filtered iterator of adjacent arcs of a node.
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
Generic graph and digraph implementations.