Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
find_path_test.cc
Go to the documentation of this file.
1
6#include <gtest/gtest.h>
7#include <stdexcept>
8#include <tpl_find_path.H>
9#include <tpl_graph.H>
10
11using namespace Aleph;
12
13// =============================================================================
14// Type Definitions
15// =============================================================================
16
18using Node = GT::Node;
19using Arc = GT::Arc;
20
21// =============================================================================
22// Test Fixture
23// =============================================================================
24
25class FindPathTest : public ::testing::Test
26{
27protected:
29
30 void SetUp() override
31 {
32 g = GT();
33 }
34
35 void TearDown() override {}
36
37 // Helper: create linear path graph: n0 -> n1 -> n2 -> ... -> n(n-1)
38 std::vector<Node*> create_linear_graph(size_t n)
39 {
40 std::vector<Node*> nodes;
41 for (size_t i = 0; i < n; ++i)
42 nodes.push_back(g.insert_node(i));
43
44 for (size_t i = 0; i < n - 1; ++i)
45 g.insert_arc(nodes[i], nodes[i + 1]);
46
47 return nodes;
48 }
49
50 // Helper: create complete graph
51 std::vector<Node*> create_complete_graph(size_t n)
52 {
53 std::vector<Node*> nodes;
54 for (size_t i = 0; i < n; ++i)
55 nodes.push_back(g.insert_node(i));
56
57 for (size_t i = 0; i < n; ++i)
58 for (size_t j = i + 1; j < n; ++j)
59 g.insert_arc(nodes[i], nodes[j]);
60
61 return nodes;
62 }
63};
64
65// =============================================================================
66// Depth-First Path Tests
67// =============================================================================
68
69TEST_F(FindPathTest, DFS_SimplePath)
70{
71 auto nodes = create_linear_graph(5);
72
74 Path<GT> path(g);
75
76 bool found = finder(g, nodes[0], nodes[4], path);
77
78 EXPECT_TRUE(found);
79 EXPECT_FALSE(path.is_empty());
80 EXPECT_EQ(path.size(), 5u);
81}
82
83TEST_F(FindPathTest, DFS_PathToSelf)
84{
85 auto n = g.insert_node(0);
86
88 Path<GT> path(g);
89
90 bool found = finder(g, n, n, path);
91
92 EXPECT_TRUE(found);
93}
94
95TEST_F(FindPathTest, DFS_NoPath)
96{
97 auto n1 = g.insert_node(1);
98 auto n2 = g.insert_node(2);
99 auto n3 = g.insert_node(3);
100
101 g.insert_arc(n1, n2);
102 // n3 is disconnected
103
104 // It threw underflow_error, when it shortened the path to nothing
106 Path<GT> path(g);
107 bool found = finder(g, n1, n3, path);
108 EXPECT_FALSE(found);
109 EXPECT_TRUE(path.is_empty());
110
111 // the free function, too
112 EXPECT_TRUE(find_path_depth_first(g, n1, n3).is_empty());
113}
114
115TEST_F(FindPathTest, DFS_CompleteGraph)
116{
117 auto nodes = create_complete_graph(10);
118
120 Path<GT> path(g);
121
122 bool found = finder(g, nodes[0], nodes[9], path);
123
124 EXPECT_TRUE(found);
125 EXPECT_FALSE(path.is_empty());
126}
127
128TEST_F(FindPathTest, DFS_WithPredicate)
129{
130 auto nodes = create_linear_graph(10);
131
133
134 // Find node with value 7
135 auto path = finder(g, nodes[0], [](Node* p) { return p->get_info() == 7; });
136
137 EXPECT_FALSE(path.is_empty());
138}
139
140TEST_F(FindPathTest, DFS_PredicateNotSatisfied)
141{
142 const auto nodes = create_linear_graph(5);
143
144 // Look for value 100 (doesn't exist) - returns empty path, no exception
146 const auto path = finder(g, nodes[0], [](Node* p) { return p->get_info() == 100; });
147 EXPECT_TRUE(path.is_empty());
148}
149
150// =============================================================================
151// Breadth-First Path Tests
152// =============================================================================
153
154TEST_F(FindPathTest, BFS_SimplePath)
155{
156 auto nodes = create_linear_graph(5);
157
159 Path<GT> path(g);
160
161 bool found = finder(g, nodes[0], nodes[4], path);
162
163 EXPECT_TRUE(found);
164 EXPECT_FALSE(path.is_empty());
165}
166
167TEST_F(FindPathTest, BFS_PathToSelf)
168{
169 auto n = g.insert_node(0);
170
171 auto m = g.insert_node(1);
172 g.insert_arc(n, m);
173 g.insert_arc(n, n);
174
176
177 // the path of no arc, as that of Find_Path_Depth_First, and not the loop
178 auto path = finder(g, n, n);
179 ASSERT_EQ(path.size(), 1u);
180 EXPECT_EQ(path.get_first_node(), n);
181
182 Path<GT> other(g);
183 EXPECT_TRUE(finder(g, n, n, other));
184 ASSERT_EQ(other.size(), 1u);
185 EXPECT_EQ(other.get_first_node(), n);
186}
187
188TEST_F(FindPathTest, StartThatMeetsTheCriterion)
189{
190 auto nodes = create_linear_graph(3);
191 auto is_first = [] (Node * p) { return p->get_info() == 0; };
192
193 // the start alone, the path of no arc, in both searches
194 const auto dfs = Find_Path_Depth_First<GT>()(g, nodes[0], is_first);
195 const auto bfs = Find_Path_Breadth_First<GT>()(g, nodes[0], is_first);
196 for (const Path<GT> * path : {&dfs, &bfs})
197 {
198 ASSERT_EQ(path->size(), 1u);
199 EXPECT_EQ(path->get_first_node(), nodes[0]);
200 }
201}
202
203namespace
204{
205 // a filter that throws E on the arcs of info 2
206 template <class E>
207 struct Throws_On_Two
208 {
209 bool operator()(Arc * arc) const
210 {
211 if (arc->get_info() == 2)
212 throw E("the filter fails");
213 return true;
214 }
215 };
216
217 template <class E>
218 void expect_the_searches_throw(GT & g, Node * from, Node * to)
219 {
220 using F = Throws_On_Two<E>;
221 Path<GT> path(g);
222 EXPECT_THROW((Find_Path_Depth_First<GT, Node_Arc_Iterator, F>()(g, from, to, path)), E);
223 EXPECT_THROW((Find_Path_Breadth_First<GT, Node_Arc_Iterator, F>()(g, from, to, path)), E);
224 }
225}
226
227// What the arc filter throws reaches the caller of the searches. The
228// iterators of the arcs caught it, stayed on the arc on which it threw, and
229// the searches took that arc.
230TEST_F(FindPathTest, WhatTheFilterThrowsReachesTheCaller)
231{
232 // b - c between the two other arcs of b, so that the filter throws while
233 // the iterator of b advances
234 auto a = g.insert_node(0);
235 auto b = g.insert_node(1);
236 auto c = g.insert_node(2);
237 auto d = g.insert_node(3);
238 g.insert_arc(a, b, 1);
239 g.insert_arc(b, c, 2);
240 g.insert_arc(b, d, 3);
241
242 expect_the_searches_throw<std::domain_error>(g, a, c);
243 expect_the_searches_throw<std::overflow_error>(g, a, c);
244}
245
247{
248 auto n1 = g.insert_node(1);
249 auto n2 = g.insert_node(2);
250 auto n3 = g.insert_node(3);
251
252 g.insert_arc(n1, n2);
253 // n3 disconnected
254
256 Path<GT> path(g);
257
258 bool found = finder(g, n1, n3, path);
259
260 EXPECT_FALSE(found);
261}
262
263TEST_F(FindPathTest, BFS_ShortestPath)
264{
265 /*
266 * Create diamond graph:
267 * n1
268 * / \
269 * n2 n3
270 * \ /
271 * n4
272 */
273 auto n1 = g.insert_node(1);
274 auto n2 = g.insert_node(2);
275 auto n3 = g.insert_node(3);
276 auto n4 = g.insert_node(4);
277
278 g.insert_arc(n1, n2);
279 g.insert_arc(n1, n3);
280 g.insert_arc(n2, n4);
281 g.insert_arc(n3, n4);
282
284 auto path = finder(g, n1, n4);
285
286 EXPECT_FALSE(path.is_empty());
287 EXPECT_EQ(path.size(), 3u); // n1 -> n2/n3 -> n4 (shortest)
288}
289
290TEST_F(FindPathTest, BFS_CompleteGraph)
291{
292 auto nodes = create_complete_graph(8);
293
295 auto path = finder(g, nodes[0], nodes[7]);
296
297 EXPECT_FALSE(path.is_empty());
298}
299
300TEST_F(FindPathTest, BFS_WithPredicate)
301{
302 auto nodes = create_linear_graph(10);
303
305
306 auto path = finder(g, nodes[0], [](Node* p) { return p->get_info() == 5; });
307
308 EXPECT_FALSE(path.is_empty());
309}
310
311// =============================================================================
312// Directed Graph Path Tests
313// =============================================================================
314
315TEST_F(FindPathTest, DirectedDFS)
316{
318 DGT dg;
319
320 auto n1 = dg.insert_node(1);
321 auto n2 = dg.insert_node(2);
322 auto n3 = dg.insert_node(3);
323
324 dg.insert_arc(n1, n2);
325 dg.insert_arc(n2, n3);
326
327 Directed_Find_Path<DGT> finder(dg);
328 auto path = finder.dfs(n1, n3);
329
330 EXPECT_FALSE(path.is_empty());
331 EXPECT_EQ(path.size(), 3u);
332}
333
334TEST_F(FindPathTest, DirectedBFS)
335{
337 DGT dg;
338
339 auto n1 = dg.insert_node(1);
340 auto n2 = dg.insert_node(2);
341 auto n3 = dg.insert_node(3);
342
343 dg.insert_arc(n1, n2);
344 dg.insert_arc(n2, n3);
345
346 Directed_Find_Path<DGT> finder(dg);
347 auto path = finder.bfs(n1, n3);
348
349 EXPECT_FALSE(path.is_empty());
350}
351
352TEST_F(FindPathTest, DirectedNoPath)
353{
355 DGT dg;
356
357 auto n1 = dg.insert_node(1);
358 auto n2 = dg.insert_node(2);
359 auto n3 = dg.insert_node(3);
360
361 dg.insert_arc(n1, n2);
362 dg.insert_arc(n3, n2); // n3 -> n2, can't reach n3 from n1
363
364 Directed_Find_Path<DGT> finder(dg);
365 auto path = finder.dfs(n1, n3);
366
367 EXPECT_TRUE(path.is_empty());
368}
369
370TEST_F(FindPathTest, DirectedPathToSelf)
371{
373 DGT dg;
374
375 auto n1 = dg.insert_node(1);
376 auto n2 = dg.insert_node(2);
377
378 dg.insert_arc(n1, n2);
379 dg.insert_arc(n2, n1);
380 dg.insert_arc(n1, n1);
381
382 // the path of no arc, as that of Find_Path_Depth_First, and neither the
383 // loop nor the cycle; the same from a start that meets the criterion
384 Directed_Find_Path<DGT> finder(dg);
385 auto is_first = [] (DGT::Node * p) { return p->get_info() == 1; };
386 const auto dfs = finder.dfs(n1, n1);
387 const auto bfs = finder.bfs(n1, n1);
388 const auto dfs_op = finder.dfs(n1, is_first);
389 const auto bfs_op = finder.bfs(n1, is_first);
390 for (const Path<DGT> * path : {&dfs, &bfs, &dfs_op, &bfs_op})
391 {
392 ASSERT_EQ(path->size(), 1u);
393 EXPECT_EQ(path->get_first_node(), n1);
394 }
395}
396
397TEST_F(FindPathTest, DirectedWithPredicate)
398{
400 DGT dg;
401
402 auto n1 = dg.insert_node(1);
403 auto n2 = dg.insert_node(2);
404 auto n3 = dg.insert_node(3);
405
406 dg.insert_arc(n1, n2);
407 dg.insert_arc(n2, n3);
408
409 Directed_Find_Path<DGT> finder(dg);
410 auto path = finder.dfs(n1, [](DGT::Node* p) { return p->get_info() == 3; });
411
412 EXPECT_FALSE(path.is_empty());
413}
414
415// =============================================================================
416// Stress Tests
417// =============================================================================
418
419TEST_F(FindPathTest, LargeLinearGraph)
420{
421 auto nodes = create_linear_graph(1000);
422
423 Find_Path_Depth_First<GT> dfs_finder;
424 auto dfs_path = dfs_finder(g, nodes[0], nodes[999]);
425
426 EXPECT_FALSE(dfs_path.is_empty());
427 EXPECT_EQ(dfs_path.size(), 1000u);
428}
429
430TEST_F(FindPathTest, LargeCompleteGraph)
431{
432 auto nodes = create_complete_graph(50);
433
435 auto path = bfs_finder(g, nodes[0], nodes[49]);
436
437 EXPECT_FALSE(path.is_empty());
438 EXPECT_LE(path.size(), 3u); // Should find direct path
439}
440
441// =============================================================================
442// Edge Cases
443// =============================================================================
444
446{
447 auto n = g.insert_node(1);
448
450 auto path = finder(g, n, n);
451
452 EXPECT_EQ(path.size(), 1u);
453}
454
455TEST_F(FindPathTest, TwoNodesConnected)
456{
457 auto n1 = g.insert_node(1);
458 auto n2 = g.insert_node(2);
459 g.insert_arc(n1, n2);
460
462 auto path = finder(g, n1, n2);
463
464 EXPECT_FALSE(path.is_empty());
465 EXPECT_EQ(path.size(), 2u);
466}
467
468TEST_F(FindPathTest, TwoNodesDisconnected)
469{
470 auto n1 = g.insert_node(1);
471 auto n2 = g.insert_node(2);
472
473 try {
475 auto path = finder(g, n1, n2);
476 // Empty path - expected behavior
477 SUCCEED();
478 } catch (...) {
479 // Exception on empty path is also valid
480 SUCCEED();
481 }
482}
483
484TEST_F(FindPathTest, CyclicGraph)
485{
486 auto n1 = g.insert_node(1);
487 auto n2 = g.insert_node(2);
488 auto n3 = g.insert_node(3);
489
490 g.insert_arc(n1, n2);
491 g.insert_arc(n2, n3);
492 g.insert_arc(n3, n1); // Cycle
493
495 auto path = finder(g, n1, n3);
496
497 EXPECT_FALSE(path.is_empty());
498}
499
500// =============================================================================
501// Multiple Paths Tests
502// =============================================================================
503
504TEST_F(FindPathTest, MultiplePaths_FindsOne)
505{
506 // Create graph with two paths from n1 to n4:
507 // n1 -> n2 -> n4
508 // n1 -> n3 -> n4
509 auto n1 = g.insert_node(1);
510 auto n2 = g.insert_node(2);
511 auto n3 = g.insert_node(3);
512 auto n4 = g.insert_node(4);
513
514 g.insert_arc(n1, n2);
515 g.insert_arc(n1, n3);
516 g.insert_arc(n2, n4);
517 g.insert_arc(n3, n4);
518
520 auto path = finder(g, n1, n4);
521
522 EXPECT_FALSE(path.is_empty());
523 EXPECT_EQ(path.size(), 3u);
524}
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
typename BaseGraph::Node Node
Definition graph-dry.H:4380
Path search on directed graphs defined through a graph class (not a digraph).
Path< GT > dfs(typename GT::Node *start, Op &op)
Path< GT > bfs(typename GT::Node *start, Op &op)
Performs a breadth-first search for a path between a pair of nodes.
Performs a depth-first search for a path between a pair of nodes.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Graph_Arc< int > Arc
The node class type.
Definition tpl_graph.H:444
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Path on a graph.
Definition tpl_graph.H:3832
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
bool is_empty() const noexcept
Return true if the path is empty.
Definition tpl_graph.H:3993
void SetUp() override
std::vector< Node * > create_linear_graph(size_t n)
std::vector< Node * > create_complete_graph(size_t n)
void TearDown() override
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
TEST_F(FindPathTest, DFS_SimplePath)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Path< GT > find_path_depth_first(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Depth-first search of a path between two nodes.
Definition tpl_graph.H:4695
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Path finding algorithms in graphs.
Generic graph and digraph implementations.