6#include <gtest/gtest.h>
40 std::vector<Node*>
nodes;
41 for (
size_t i = 0; i < n; ++i)
44 for (
size_t i = 0; i < n - 1; ++i)
53 std::vector<Node*>
nodes;
54 for (
size_t i = 0; i < n; ++i)
57 for (
size_t i = 0; i < n; ++i)
58 for (
size_t j = i + 1; j < n; ++j)
71 auto nodes = create_linear_graph(5);
76 bool found = finder(g,
nodes[0],
nodes[4], path);
80 EXPECT_EQ(path.
size(), 5u);
85 auto n = g.insert_node(0);
90 bool found = finder(g, n, n, path);
97 auto n1 = g.insert_node(1);
98 auto n2 = g.insert_node(2);
99 auto n3 = g.insert_node(3);
101 g.insert_arc(n1, n2);
107 bool found = finder(g, n1, n3, path);
117 auto nodes = create_complete_graph(10);
122 bool found = finder(g,
nodes[0],
nodes[9], path);
130 auto nodes = create_linear_graph(10);
135 auto path = finder(g,
nodes[0], [](
Node* p) {
return p->get_info() == 7; });
137 EXPECT_FALSE(path.is_empty());
142 const auto nodes = create_linear_graph(5);
146 const auto path = finder(g,
nodes[0], [](
Node* p) {
return p->get_info() == 100; });
147 EXPECT_TRUE(path.is_empty());
156 auto nodes = create_linear_graph(5);
161 bool found = finder(g,
nodes[0],
nodes[4], path);
169 auto n = g.insert_node(0);
171 auto m = g.insert_node(1);
178 auto path = finder(g, n, n);
179 ASSERT_EQ(path.size(), 1u);
180 EXPECT_EQ(path.get_first_node(), n);
183 EXPECT_TRUE(finder(g, n, n, other));
184 ASSERT_EQ(other.
size(), 1u);
190 auto nodes = create_linear_graph(3);
191 auto is_first = [] (
Node * p) {
return p->get_info() == 0; };
196 for (
const Path<GT> * path : {&dfs, &bfs})
198 ASSERT_EQ(path->size(), 1u);
199 EXPECT_EQ(path->get_first_node(),
nodes[0]);
209 bool operator()(
Arc * arc)
const
211 if (arc->get_info() == 2)
212 throw E(
"the filter fails");
218 void expect_the_searches_throw(
GT & g,
Node * from,
Node * to)
220 using F = Throws_On_Two<E>;
242 expect_the_searches_throw<std::domain_error>(g, a, c);
243 expect_the_searches_throw<std::overflow_error>(g, a, c);
258 bool found = finder(g, n1, n3, path);
284 auto path = finder(g, n1, n4);
286 EXPECT_FALSE(path.is_empty());
287 EXPECT_EQ(path.size(), 3u);
292 auto nodes = create_complete_graph(8);
297 EXPECT_FALSE(path.is_empty());
302 auto nodes = create_linear_graph(10);
306 auto path = finder(g,
nodes[0], [](
Node* p) {
return p->get_info() == 5; });
308 EXPECT_FALSE(path.is_empty());
320 auto n1 = dg.insert_node(1);
321 auto n2 = dg.insert_node(2);
322 auto n3 = dg.insert_node(3);
324 dg.insert_arc(n1, n2);
325 dg.insert_arc(n2, n3);
328 auto path = finder.
dfs(n1, n3);
330 EXPECT_FALSE(path.is_empty());
331 EXPECT_EQ(path.size(), 3u);
339 auto n1 = dg.insert_node(1);
340 auto n2 = dg.insert_node(2);
341 auto n3 = dg.insert_node(3);
343 dg.insert_arc(n1, n2);
344 dg.insert_arc(n2, n3);
347 auto path = finder.
bfs(n1, n3);
349 EXPECT_FALSE(path.is_empty());
357 auto n1 = dg.insert_node(1);
358 auto n2 = dg.insert_node(2);
359 auto n3 = dg.insert_node(3);
361 dg.insert_arc(n1, n2);
362 dg.insert_arc(n3, n2);
365 auto path = finder.
dfs(n1, n3);
367 EXPECT_TRUE(path.is_empty());
375 auto n1 = dg.insert_node(1);
376 auto n2 = dg.insert_node(2);
378 dg.insert_arc(n1, n2);
379 dg.insert_arc(n2, n1);
380 dg.insert_arc(n1, n1);
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})
392 ASSERT_EQ(path->size(), 1u);
393 EXPECT_EQ(path->get_first_node(), n1);
402 auto n1 = dg.insert_node(1);
403 auto n2 = dg.insert_node(2);
404 auto n3 = dg.insert_node(3);
406 dg.insert_arc(n1, n2);
407 dg.insert_arc(n2, n3);
410 auto path = finder.
dfs(n1, [](
DGT::Node* p) {
return p->get_info() == 3; });
412 EXPECT_FALSE(path.is_empty());
421 auto nodes = create_linear_graph(1000);
424 auto dfs_path = dfs_finder(g,
nodes[0],
nodes[999]);
426 EXPECT_FALSE(dfs_path.is_empty());
427 EXPECT_EQ(dfs_path.size(), 1000u);
432 auto nodes = create_complete_graph(50);
435 auto path = bfs_finder(g,
nodes[0],
nodes[49]);
437 EXPECT_FALSE(path.is_empty());
438 EXPECT_LE(path.size(), 3u);
450 auto path = finder(g, n, n);
452 EXPECT_EQ(path.size(), 1u);
462 auto path = finder(g, n1, n2);
464 EXPECT_FALSE(path.is_empty());
465 EXPECT_EQ(path.size(), 2u);
475 auto path = finder(g, n1, n2);
495 auto path = finder(g, n1, n3);
497 EXPECT_FALSE(path.is_empty());
520 auto path = finder(g, n1, n4);
522 EXPECT_FALSE(path.is_empty());
523 EXPECT_EQ(path.size(), 3u);
WeightedDigraph::Node Node
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Node Node
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.
Graph_Node< int > Node
The graph type.
Graph_Arc< int > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
size_t size() const noexcept
Return the path length in nodes.
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
bool is_empty() const noexcept
Return true if the path is empty.
std::vector< Node * > create_linear_graph(size_t n)
std::vector< Node * > create_complete_graph(size_t n)
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
TEST_F(FindPathTest, DFS_SimplePath)
DynArray< Graph::Node * > nodes
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.
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Path finding algorithms in graphs.
Generic graph and digraph implementations.