Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_contract_matrix_paths_single_source_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
50#include <set>
51#include <stdexcept>
52#include <string>
53#include <type_traits>
54#include <utility>
55
56#include <gtest/gtest.h>
57
58#include <tpl_graph.H>
60#include <Dijkstra.H>
61#include <AStar.H>
62#include <IDA_Star.H>
63#include <Dial.H>
64#include <Zero_One_BFS.H>
65
66#include "graph_harness.H"
69
70using namespace Aleph;
71using namespace graph_harness;
72using namespace graph_contract_matrix;
73
74namespace
75{
77 template <class List>
78 std::set<int> arc_numbers(const List & l)
79 {
80 std::set<int> r;
81 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
82 r.insert(it.get_curr()->get_info());
83 return r;
84 }
85} // namespace
86
87namespace
88{
93 Spec ladder()
94 {
95 Spec s;
96 s.n = 4;
97 s.arcs = {{0, 1}, {2, 3}, {1, 2}, {1, 2}};
98 return s;
99 }
100
103 template <class GT, class SA>
104 constexpr int ladder_cost = hides<GT, SA> ? 4 : 3;
105
108 template <class GT, class SA>
109 std::set<int> ladder_arcs()
110 {
111 return hides<GT, SA> ? std::set<int>{0, 1, 3} : std::set<int>{0, 1, 2};
112 }
113
115 Spec triangle()
116 {
117 Spec s;
118 s.n = 3;
119 s.arcs = {{0, 1}, {1, 2}, {0, 2}};
120 return s;
121 }
122
124 template <class GT>
125 std::set<int> path_arcs(const Path<GT> & p)
126 {
127 std::set<int> r;
128 p.for_each_arc([&r] (typename GT::Arc * a) { r.insert(a->get_info()); });
129 return r;
130 }
131
134 template <class GT>
135 std::set<int> graph_arcs(const GT & g)
136 {
137 std::set<int> r;
138 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
139 r.insert(it.get_curr_ne()->get_info());
140 return r;
141 }
142} // namespace
143
144// On the six kinds
145
146template <class GT>
147class ContractPathsSingleSource : public ::testing::Test {};
148
150
152{
153 using GT = TypeParam;
154
155 // Before a computation: the base cannot paint by itself
156 for_each_filter<GT>([&] (auto sa, const char * name)
157 {
158 using SA = decltype(sa);
159 Built<GT> b(ladder());
160 SCOPED_TRACE(name);
162 EXPECT_FALSE(x.has_computation());
163 EXPECT_FALSE(x.is_painted());
164 EXPECT_EQ(x.get_start_node(), nullptr);
165 EXPECT_EQ(x.get_graph(), nullptr);
166 EXPECT_THROW(x.get_distance(b.nodes[3]), std::domain_error);
167 Path<GT> path(b.g);
168 EXPECT_THROW(x.get_min_path(b.nodes[3], path), std::domain_error);
169 });
170}
171
173{
174 using GT = TypeParam;
175
176 for_each_filter<GT>([&] (auto sa, const char * name)
177 {
178 using SA = decltype(sa);
180 Built<GT> b(ladder());
181 auto * s = b.nodes[0];
182 auto * t = b.nodes[3];
183 SCOPED_TRACE(name);
184 const int cost = ladder_cost<GT, SA>;
185 const auto arcs = ladder_arcs<GT, SA>();
186
187 for_each_constness(b.g, [&] (auto & g)
188 {
189 Dijkstra x(Dft_Dist<GT>(), sa);
190 Path<GT> path(g);
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);
203
204 GT tree;
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);
209 x(g, s, tree);
210 EXPECT_EQ(graph_arcs(tree), arcs);
211
212 Path<GT> on_tree(g);
213 EXPECT_EQ(x.get_min_path(tree, t, on_tree), cost);
214 EXPECT_EQ(path_arcs(on_tree), arcs);
215 });
216
217 // copy_painted_min_paths_tree() takes the graph as GT &
218 Dijkstra x(Dft_Dist<GT>(), sa);
219 x.paint_min_paths_tree(b.g, s);
220 GT tree;
221 EXPECT_EQ(x.copy_painted_min_paths_tree(b.g, tree), cost);
222 EXPECT_EQ(graph_arcs(tree), arcs);
223 EXPECT_FALSE(x.is_painted());
224 });
225}
226
228{
229 using GT = TypeParam;
230 using Heuristic = Zero_Heuristic<GT, Dft_Dist<GT>>;
231
232 for_each_filter<GT>([&] (auto sa, const char * name)
233 {
234 using SA = decltype(sa);
235 using AStar = AStar_Min_Path<GT, Dft_Dist<GT>, Heuristic, Node_Arc_Iterator, SA>;
236 Built<GT> b(ladder());
237 auto * s = b.nodes[0];
238 auto * t = b.nodes[3];
239 SCOPED_TRACE(name);
240 const int cost = ladder_cost<GT, SA>;
241 const auto arcs = ladder_arcs<GT, SA>();
242
243 for_each_constness(b.g, [&] (auto & g)
244 {
245 AStar x(Dft_Dist<GT>(), Heuristic(), sa);
246 Path<GT> path(g);
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);
263
264 GT tree;
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);
271 x(g, s, tree);
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);
275
276 Path<GT> on_tree(g);
277 EXPECT_EQ(x.get_min_path(tree, t, on_tree), cost);
278 EXPECT_EQ(path_arcs(on_tree), arcs);
279 });
280
281 // copy_painted_min_paths_tree() takes the graph as GT &
282 AStar x(Dft_Dist<GT>(), Heuristic(), sa);
283 x.paint_min_paths_tree(b.g, s);
284 GT tree;
285 EXPECT_EQ(x.copy_painted_min_paths_tree(b.g, tree), cost);
286 EXPECT_EQ(graph_arcs(tree), arcs);
287 });
288}
289
290TYPED_TEST(ContractPathsSingleSource, DijkstraAndAStarAfterAnotherPainting)
291{
292 using GT = TypeParam;
293 using Heuristic = Zero_Heuristic<GT, Dft_Dist<GT>>;
294 const Spec spec = ladder();
295 Built<GT> b(spec);
296 auto * s = b.nodes[0];
297 auto * t = b.nodes[3];
298
299 // A painting without the filter leaves the bit Spanning_Tree in the arc
300 // that the next filter hides: the next computation must reset it, and
301 // neither the path nor the copied tree may take that arc
302 for_each_filter<GT>([&] (auto sa, const char * name)
303 {
304 using SA = decltype(sa);
305 if constexpr (hides<GT, SA>)
306 {
307 SCOPED_TRACE(name);
308 Dijkstra_Min_Paths<GT> unfiltered;
309 Path<GT> path(b.g);
310
311 unfiltered.paint_min_paths_tree(b.g, s);
313 EXPECT_EQ(x.find_min_path(b.g, s, t, path), 4);
314 EXPECT_EQ(path_arcs(path).count(Hidden), 0U);
315
316 unfiltered.paint_min_paths_tree(b.g, s);
317 AStar_Min_Path<GT, Dft_Dist<GT>, Heuristic, Node_Arc_Iterator, SA> y(Dft_Dist<GT>(), Heuristic(), sa);
318 EXPECT_EQ(y.find_path(b.g, s, t, path), 4);
319 EXPECT_EQ(path_arcs(path).count(Hidden), 0U);
320
321 unfiltered.paint_min_paths_tree(b.g, s);
322 x.paint_min_paths_tree(b.g, s);
323 GT tree;
324 EXPECT_EQ(x.copy_painted_min_paths_tree(b.g, tree), 4);
325 EXPECT_EQ(tree.get_num_arcs(), 3U);
326 }
327 });
328}
329
331{
332 using GT = TypeParam;
333 using Heuristic = Zero_Heuristic<GT, Dft_Dist<GT>>;
334
335 for_each_filter<GT>([&] (auto sa, const char * name)
336 {
337 using SA = decltype(sa);
338 Built<GT> b(ladder());
339 auto * s = b.nodes[0];
340 auto * t = b.nodes[3];
341 SCOPED_TRACE(name);
342 const int cost = ladder_cost<GT, SA>;
343 const auto arcs = ladder_arcs<GT, SA>();
344 for_each_constness(b.g, [&] (auto & g)
345 {
346 IDA_Star<GT, Dft_Dist<GT>, Heuristic, Node_Arc_Iterator, SA> x(Dft_Dist<GT>(), Heuristic(), sa);
347 Path<GT> path(g);
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);
352 });
353 });
354}
355
357{
358 using GT = TypeParam;
359
360 for_each_filter<GT>([&] (auto sa, const char * name)
361 {
362 using SA = decltype(sa);
363 Built<GT> b(ladder());
364 auto * s = b.nodes[0];
365 auto * t = b.nodes[3];
366 SCOPED_TRACE(name);
367 const int cost = ladder_cost<GT, SA>;
368 const auto arcs = ladder_arcs<GT, SA>();
369 for_each_constness(b.g, [&] (auto & g)
370 {
371 Dial_Min_Paths<GT, Dft_Dist<GT>, Node_Arc_Iterator, SA> x(Dft_Dist<GT>(), sa);
372 Path<GT> path(g);
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);
383 });
384 });
385}
386
388{
389 using GT = TypeParam;
390
391 for_each_filter<GT>([&] (auto sa, const char * name)
392 {
393 using SA = decltype(sa);
394 Built<GT> b(triangle());
395 auto * s = b.nodes[0];
396 auto * t = b.nodes[2];
397 SCOPED_TRACE(name);
398 for_each_constness(b.g, [&] (auto & g)
399 {
400 Zero_One_BFS<GT, Dft_Dist<GT>, Node_Arc_Iterator, SA> x(Dft_Dist<GT>(), sa);
401 Path<GT> path(g);
402 if constexpr (not hides<GT, SA>)
403 { // the arc of weight 2 is seen
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);
406 }
407 else
408 {
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}));
419 }
420 });
421 });
422}
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.
Definition AStar.H:176
Default distance accessor for arc weights.
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Definition Dijkstra.H:117
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.
Definition Dijkstra.H:455
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.
Definition Dijkstra.H:536
bool is_painted() const noexcept
Check if the graph has been painted.
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Path on a graph.
Definition tpl_graph.H:3832
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Definition tpl_graph.H:4540
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.
Definition graph-dry.H:854
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
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
Definition graphpic.C:408
static mpfr_t y
Definition mpfr_mul_d.c:3
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.
Definition ahAlgo.H:127
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.
Definition tpl_graph.H:1155
Default heuristic for A* (zero heuristic, degrades to Dijkstra).
Definition AStar.H:79
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
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
DynList< int > l