38#include <gtest/gtest.h>
58template <
class GT,
class SA>
65 typename GT::Arc * get_current_arc_ne() const noexcept
73 template <
class G,
class D,
class A>
81using DijkstraIntAllArcs =
87 void operator()(
DGraph &,
DNode *)
const noexcept {}
90struct InitArcRandNonNegative
93 std::uniform_int_distribution<int> dist;
95 explicit InitArcRandNonNegative(uint32_t
seed,
int max_w)
96 : rng(
seed), dist(0, max_w)
102 a->get_info() = dist(rng);
106struct InitArcRandAllowNegative
109 std::uniform_int_distribution<int> dist;
111 explicit InitArcRandAllowNegative(uint32_t
seed,
int min_w,
int max_w)
112 : rng(
seed), dist(min_w, max_w)
118 a->get_info() = dist(rng);
122static DNode * pick_node_by_index(
DGraph & g,
size_t idx)
125 for (
auto it = g.get_node_it(); it.has_curr(); it.next_ne(), ++i)
127 return it.get_curr();
131static std::pair<DNode *, DNode *> pick_two_distinct_nodes(
DGraph & g,
135 std::uniform_int_distribution<size_t> dist(0, g.get_num_nodes() - 1);
137 const size_t i = dist(
rng);
138 size_t j = dist(
rng);
142 auto *s = pick_node_by_index(g, i);
143 auto *t = pick_node_by_index(g, j);
149 ASSERT_NE(s,
nullptr);
150 ASSERT_NE(t,
nullptr);
155 const int dij_dist = dij(g, s, t, dij_path);
159 const bool neg = bf.paint_spanning_tree(s);
163 const int bf_dist = bf.get_min_path(t, bf_path);
166 EXPECT_EQ(dij_dist, bf_dist);
169 if (bf_dist != std::numeric_limits<int>::max())
171 EXPECT_FALSE(dij_path.is_empty());
172 EXPECT_FALSE(bf_path.is_empty());
174 EXPECT_EQ(dij_path.get_last_node(), t);
175 EXPECT_EQ(bf_path.get_last_node(), t);
179 EXPECT_TRUE(dij_path.is_empty());
180 EXPECT_TRUE(bf_path.is_empty());
185TEST(GraphScenarios, DisconnectedGraph_UnreachablePath)
188 auto *a = g.insert_node(1);
189 auto *b = g.insert_node(2);
190 auto *c = g.insert_node(3);
191 auto *d = g.insert_node(4);
194 g.insert_arc(a, b, 5);
197 g.insert_arc(c, d, 7);
201 const int dist = dij(g, a, d, path);
203 EXPECT_EQ(dist, std::numeric_limits<int>::max());
207TEST(GraphScenarios, Multigraph_ParallelArcs_DijkstraChoosesMin)
210 auto *s = g.insert_node(0);
211 auto *t = g.insert_node(1);
213 const auto arcs_before = g.get_num_arcs();
215 g.insert_arc(s, t, 10);
216 g.insert_arc(s, t, 3);
217 g.insert_arc(s, t, 7);
221 if (
const auto arcs_after = g.get_num_arcs(); arcs_after - arcs_before < 3)
222 GTEST_SKIP() <<
"Graph type does not appear to support parallel arcs (multigraph).";
226 DijkstraIntAllArcs dij;
228 const int dist = dij(g, s, t, path);
234TEST(GraphScenarios, BellmanFord_NegativeWeights_NoNegativeCycle)
237 auto *n0 = g.insert_node(0);
238 auto *n1 = g.insert_node(1);
239 auto *n2 = g.insert_node(2);
240 auto *n3 = g.insert_node(3);
243 g.insert_arc(n0, n1, -2);
244 g.insert_arc(n1, n2, 3);
245 g.insert_arc(n2, n3, -1);
246 g.insert_arc(n0, n3, 5);
258TEST(GraphScenarios, SparseVsDense_DijkstraMatchesBellmanFord)
261 const size_t N = 200;
265 InitNodeNoop init_node;
266 InitArcRandNonNegative init_arc(1234, 20);
269 777, init_node, init_arc);
271 auto g = gen(
N,
size_t(4 *
N),
false );
273 auto [s, t] = pick_two_distinct_nodes(g, 999);
274 expect_dijkstra_matches_bellman_ford(g, s, t);
279 InitNodeNoop init_node;
280 InitArcRandNonNegative init_arc(4321, 20);
283 888, init_node, init_arc);
285 auto g = gen(
N, 0.08,
false );
287 auto [s, t] = pick_two_distinct_nodes(g, 1001);
288 expect_dijkstra_matches_bellman_ford(g, s, t);
292TEST(GraphScenarios, MultiGraph_TarjanCycleDetection)
295 auto *a = g.insert_node(1);
296 auto *b = g.insert_node(2);
299 g.insert_arc(a, b, 1);
300 g.insert_arc(a, b, 2);
306 g.insert_arc(b, a, 0);
Bellman-Ford algorithm for single-source shortest paths.
Dijkstra's shortest path algorithm.
Tarjan's algorithm for strongly connected components.
Bellman-Ford algorithm for shortest paths with negative weights.
bool paint_spanning_tree(Node *start)
Paint the shortest paths tree from a start node.
Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extract the shortest path to a node from a previously painted tree.
auto get_current_arc() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
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.
Number Theoretic Transform over Z / MOD Z.
Filtered iterator for outcoming arcs of a node.
Digraph_Iterator< GT, __Out_Filt< GT > > Base
bool is_empty() const noexcept
Return true if the path is empty.
Random directed graph (digraph) generator.
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
bool has_cycle(typename GT::Node *root)
Depth-first search that detects a cycle reachable from root.
Main namespace for Aleph-w library functions.
Random graph generation utilities.
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
Filtered iterator of adjacent arcs of a node.
Generic graph and digraph implementations.