9# include <gtest/gtest.h>
35TEST(ZeroOneBFS, BasicShortestPath)
50 int d = bfs(g, n0, n3, path);
60TEST(ZeroOneBFS, AllZeroWeights)
74 int d = bfs(g, n0, n3, path);
82TEST(ZeroOneBFS, AllOneWeights)
96 int d = bfs(g, n0, n3, path);
112 int d = bfs(g, n0, n0, path);
133 int d = bfs(g, n0, n2, path);
135 EXPECT_EQ(d, std::numeric_limits<int>::max());
149 int d = bfs(g, n0, n0, path);
173 int d = bfs(g, n0, n4, path);
181TEST(ZeroOneBFS, ZeroWeightShortcut)
201 int d = bfs(g, n0, n3, path);
209TEST(ZeroOneBFS, NullNodeValidation)
217 EXPECT_THROW(bfs(g,
nullptr, n0, path), std::domain_error);
218 EXPECT_THROW(bfs(g, n0,
nullptr, path), std::domain_error);
224TEST(ZeroOneBFS, EmptyGraphValidation)
236 EXPECT_THROW(bfs(g, n, n, path), std::domain_error);
243TEST(ZeroOneBFS, InvalidWeightValidation)
254 EXPECT_THROW(bfs(g, n0, n1, path), std::domain_error);
265TEST(ZeroOneBFS, PaintAndGetMinPath)
292TEST(ZeroOneBFS, GetDistanceAfterPainting)
316TEST(ZeroOneBFS, GetDistanceBeforePainting)
328TEST(ZeroOneBFS, PathNodeVerification)
343 int d = bfs(g, n0, n3, path);
353 ASSERT_EQ(
nodes.size(), 2u);
354 auto pit =
nodes.get_it();
355 EXPECT_EQ(pit.get_curr(), 0); pit.next();
356 EXPECT_EQ(pit.get_curr(), 3);
362TEST(ZeroOneBFS, CompleteGraphK4)
379 int d = bfs(g, n0, n3, path);
389TEST(ZeroOneBFS, GraphWithCycle)
404 int d = bfs(g, n0, n3, path);
416 auto n0 = g.insert_node(0);
417 auto n1 = g.insert_node(1);
418 auto n2 = g.insert_node(2);
420 g.insert_arc(n0, n1, 1);
421 g.insert_arc(n1, n2, 0);
425 int d = bfs(g, n0, n2, path);
433TEST(ZeroOneBFS, DigraphNoReversePath)
436 auto n0 = g.insert_node(0);
437 auto n1 = g.insert_node(1);
439 g.insert_arc(n0, n1, 1);
444 int d1 = bfs(g, n0, n1, path_fwd);
448 int d2 = bfs(g, n1, n0, path_bck);
449 EXPECT_EQ(d2, std::numeric_limits<int>::max());
455TEST(ZeroOneBFS, MultipleComputations)
468 int d1 = bfs(g, n0, n2, p1);
472 int d2 = bfs(g, n2, n0, p2);
479TEST(ZeroOneBFS, LargeLinearGraph)
485 for (
int i = 0; i <
N; ++i)
489 for (
int i = 0; i <
N - 1; ++i)
504TEST(ZeroOneBFS, CrossValidationWithDijkstra)
525 int bfs_d = bfs(g, n0, n4, bfs_path);
531 int dij_d = dij(g, n0, n4, dij_path);
533 EXPECT_EQ(bfs_d, dij_d);
586 for (
int r = 0;
r < 3; ++
r)
587 for (
int c = 0; c < 3; ++c)
608 int d = bfs(g, grid[0][0], grid[2][2], path);
617TEST(ZeroOneBFS, TwoNodeZeroWeight)
627 int d = bfs(g, n0, n1, path);
635TEST(ZeroOneBFS, TwoNodeOneWeight)
645 int d = bfs(g, n0, n1, path);
653TEST(ZeroOneBFS, NegativeWeightValidation)
664 EXPECT_THROW(bfs(g, n0, n1, path), std::domain_error);
698TEST(ZeroOneBFS, DisconnectedPainting)
719TEST(ZeroOneBFS, LargeGraphManyZeros)
725 for (
int i = 0; i <
N; ++i)
729 for (
int i = 0; i <
N - 1; ++i)
745TEST(ZeroOneBFS, ParallelEdgesKeepSingleParentArcMarked)
752 auto direct_one = g.
insert_arc(start, end, 1);
753 auto start_mid_zero = g.
insert_arc(start, mid, 0);
755 auto mid_end_zero = g.
insert_arc(mid, end, 0);
769 size_t painted_arcs = 0;
770 for (GT::Arc_Iterator it(g); it.has_curr(); it.next())
774 EXPECT_EQ(painted_arcs, 2u);
781TEST(ZeroOneBFS, PaintingStaysAfterTheObjectGoes)
807TEST(ZeroOneBFS, TheGraphMayGoFirst)
820 auto m1 =
h.insert_node(1);
821 h.insert_arc(m0, m1, 1);
824 EXPECT_EQ(path.
size(), 2U);
Dijkstra's shortest path algorithm.
0-1 BFS shortest path algorithm.
WeightedDigraph::Node Node
static Array create(const size_t n)
Create an array with n logical elements.
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
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
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 is_empty() const noexcept
Return true if the path is empty.
0-1 BFS algorithm for shortest paths in graphs with 0/1 weights.
bool is_painted() const noexcept
Check if a computation has been performed.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance_Type get_min_path(Node *end, Path< GT > &path)
Extracts a shortest path from a previously painted graph.
Distance_Type find_min_path(const GT &g, Node *start, Node *end, Path< GT > &path)
Computes the shortest path from start to end.
void paint_min_paths_tree(const GT &g, Node *start)
Paints the shortest paths tree on the graph from start.
Distance_Type get_distance(Node *node)
Gets the accumulated distance to a node after painting.
Node * get_start_node() const noexcept
Get the start node of the last computation.
Minimal std::expected-style result type for C++20.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
DynArray< Graph::Node * > nodes
#define NODE_COUNTER(p)
Get the counter of a node.
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define NODE_COOKIE(p)
Return the node cookie
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.