9# include <gtest/gtest.h>
45TEST(BidirectionalBFS, BasicShortestPath)
60 bool found = bibfs(g, n0, n3, path);
69TEST(BidirectionalBFS, PathToSelf)
76 bool found = bibfs(g, n0, n0, path);
84TEST(BidirectionalBFS, NoPathExists)
92 bool found = bibfs(g, n0, n1, path);
100TEST(BidirectionalBFS, SingleNode)
107 bool found = bibfs(g, n0, n0, path);
115TEST(BidirectionalBFS, LinearChain)
129 bool found = bibfs(g, n0, n3, path);
138TEST(BidirectionalBFS, CompleteGraphK4)
155 bool found = bibfs(g, n0, n3, path);
164TEST(BidirectionalBFS, NullNodeValidation)
172 EXPECT_THROW(bibfs(g,
nullptr, n0, path), std::domain_error);
173 EXPECT_THROW(bibfs(g, n0,
nullptr, path), std::domain_error);
179TEST(BidirectionalBFS, DisconnectedComponents)
192 bool found = bibfs(g, n0, n3, path);
200TEST(BidirectionalBFS, TwoNodeGraph)
210 bool found = bibfs(g, n0, n1, path);
219TEST(BidirectionalBFS, GraphWithCycle)
235 bool found = bibfs(g, n0, n2, path);
244TEST(BidirectionalBFS, OddLengthPath)
256 bool found = bibfs(g, n0, n2, path);
265 ASSERT_EQ(
nodes.size(), 3u);
266 auto pit =
nodes.get_it();
267 EXPECT_EQ(pit.get_curr(), 0); pit.next();
268 EXPECT_EQ(pit.get_curr(), 1); pit.next();
269 EXPECT_EQ(pit.get_curr(), 2);
275TEST(BidirectionalBFS, EvenLengthPath)
289 bool found = bibfs(g, n0, n3, path);
298TEST(BidirectionalBFS, CrossValidationWithBFS)
316 bool bi_found = bibfs(g, n0, n4, bi_path);
321 bool std_found = std_bfs(g, n0, n4, std_path);
323 EXPECT_EQ(bi_found, std_found);
324 if (bi_found
and std_found)
333TEST(BidirectionalBFS, MultipleComputations)
346 EXPECT_TRUE(bibfs(g, n0, n2, p1));
349 EXPECT_TRUE(bibfs(g, n2, n0, p2));
357TEST(BidirectionalBFS, LargeGraph)
363 for (
int i = 0; i <
N; ++i)
366 for (
int i = 0; i <
N - 1; ++i)
371 bool found = bibfs(g,
nodes[0],
nodes[
N - 1], path);
380TEST(BidirectionalBFS, OperatorReturnsPath)
391 auto path = bibfs(g, n0, n2);
393 EXPECT_FALSE(path.is_empty());
400TEST(BidirectionalBFS, StarGraph)
416 bool found = bibfs(g, n1, n4, path);
425TEST(BidirectionalBFS, BinaryTreeGraph)
450 bool found = bibfs(g, n3, n6, path);
459TEST(BidirectionalBFS, DigraphForward)
462 auto n0 = g.insert_node(0);
463 auto n1 = g.insert_node(1);
464 auto n2 = g.insert_node(2);
466 g.insert_arc(n0, n1, 0);
467 g.insert_arc(n1, n2, 0);
472 bool found = bibfs(g, n0, n2, path);
481TEST(BidirectionalBFS, DigraphNoBackward)
484 auto n0 = g.insert_node(0);
485 auto n1 = g.insert_node(1);
487 g.insert_arc(n0, n1, 0);
491 bool found = bibfs(g, n1, n0, p2);
499TEST(BidirectionalBFS, DigraphInIteratorBackward)
502 auto n0 = g.insert_node(0);
503 auto n1 = g.insert_node(1);
504 auto n2 = g.insert_node(2);
505 auto n3 = g.insert_node(3);
507 g.insert_arc(n0, n1, 0);
508 g.insert_arc(n1, n2, 0);
509 g.insert_arc(n2, n3, 0);
513 bool found = bibfs(g, n0, n3, path);
523 ASSERT_EQ(
nodes.size(), 4u);
524 auto pit =
nodes.get_it();
525 EXPECT_EQ(pit.get_curr(), 0); pit.next();
526 EXPECT_EQ(pit.get_curr(), 1); pit.next();
527 EXPECT_EQ(pit.get_curr(), 2); pit.next();
528 EXPECT_EQ(pit.get_curr(), 3);
547 for (
int i = 0; i < 5; ++i)
557 ASSERT_TRUE(bibfs(g, n[0], n[3], path));
558 EXPECT_EQ(path.
size(), 3u);
565 EXPECT_FALSE(bibfs(g, n[3], n[0],
none));
566 EXPECT_FALSE(bibfs(g, n[1], n[4],
none));
567 EXPECT_TRUE(bibfs(g, n[4], n[3],
none));
568 EXPECT_EQ(
none.size(), 2u);
571TEST(BidirectionalBFS, DigraphBackwardStepsSeeTheArcsIntoEachNode)
573 diamond_backward<DGT>();
580TEST(BidirectionalBFS, AGraphThatSaysThatItIsDirectedSeesTheArcsIntoEachNode)
582 diamond_backward<GT>(
true);
583 diamond_backward<List_SGraph<Graph_Snode<int>,
Graph_Sarc<int>>>(
true);
584 diamond_backward<Array_Graph<Graph_Anode<int>,
Graph_Aarc<int>>>(
true);
598TEST(BidirectionalBFS, SocialNetworkExample)
617 bool found = bibfs(g, alice, frank, path);
626TEST(BidirectionalBFS, PathEndsCorrectness)
642 bool found = bibfs(g, n0, n4, path);
651 ASSERT_GE(
nodes.size(), 2u);
652 EXPECT_EQ(
nodes.get_first(), 0);
653 EXPECT_EQ(
nodes.get_last(), 4);
659TEST(BidirectionalBFS, CrossValidationMultipleEndpoints)
665 for (
int i = 0; i <
N; ++i)
669 for (
int i = 0; i <
N - 1; ++i)
676 for (
int start = 0; start <
N; start += 3)
677 for (
int end = start + 1; end <
N; end += 2)
683 bool bi_found = bibfs(g,
nodes[start],
nodes[end], bi_path);
684 bool std_found = std_bfs(g,
nodes[start],
nodes[end], std_path);
686 EXPECT_EQ(bi_found, std_found)
687 <<
"Mismatch for start=" << start <<
" end=" << end;
688 if (bi_found
and std_found)
691 <<
"Length mismatch for start=" << start <<
" end=" << end;
702 const int ROWS = 4, COLS = 4;
703 Node * grid[ROWS][COLS];
705 for (
int r = 0;
r < ROWS; ++
r)
706 for (
int c = 0; c < COLS; ++c)
709 for (
int r = 0;
r < ROWS; ++
r)
710 for (
int c = 0; c < COLS; ++c)
720 bool found = bibfs(g, grid[0][0], grid[ROWS - 1][COLS - 1], path);
724 EXPECT_EQ(
path_edge_count(path),
static_cast<size_t>(ROWS + COLS - 2));
730TEST(BidirectionalBFS, MeetingPointDepthSumRegression)
756 ASSERT_TRUE(bibfs(g, s, t, bi_path));
757 ASSERT_TRUE(std_bfs(g, s, t, std_path));
Bidirectional BFS for shortest unweighted path.
WeightedDigraph::Node Node
static Array create(const size_t n)
Create an array with n logical elements.
Bidirectional BFS for finding shortest unweighted paths.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
typename BaseGraph::Node Node
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Performs a breadth-first search for a path between a pair of nodes.
Arc for graphs implemented with simple adjacency lists.
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)
Number Theoretic Transform over Z / MOD Z.
Iterator on nodes and arcs of a path.
size_t size() const noexcept
Return the path length in nodes.
bool check_directed() const
Return true if the directed path is consistent: each arc goes from its node to the next one,...
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.
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
void set_digraph(bool val)
Temporal indication for preventing to other algorithms that an graph must be treated as a directed gr...
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Arc of graph implemented with double-linked adjacency lists.
size_t path_edge_count(Path< G > &p)
void diamond_backward(bool says_directed=false)
Array-based graph implementation.
Path finding algorithms in graphs.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.