13#include <gtest/gtest.h>
32template <
class GT,
class SA = Dft_Show_Arc<GT>>
57 template <
class G,
class D,
class A>
63 template <
class G,
class D,
class A>
67template <
typename HeapTag,
typename Graph>
73template <
typename HeapTag,
typename Graph>
78template <
typename HeapTag>
81template <
typename HeapTag>
84template <
typename HeapTag>
87template <
typename HeapTag>
90template <
typename HeapTag>
93using HeapTypes = ::testing::Types<BinHeapTag, FibHeapTag>;
119 double d = dij(g, n0, n3, path);
147 double d = dij(g, n0, n0, path);
152 ASSERT_EQ(path.
size(), 1u);
166 double d = dij(g, n0, n1, path);
168 EXPECT_EQ(d, std::numeric_limits<double>::max());
214 double d = dij(g, n0, n1, path);
307 double d = dij(g, n0, n3, path);
330 double d = dij(g, n0, n3, path);
346 double d = dij(g, n0, n1, path);
354 DNode* n0 = g.insert_node(0);
355 DNode* n1 = g.insert_node(1);
356 DNode* n2 = g.insert_node(2);
358 g.insert_arc(n0, n1, 1.0);
359 g.insert_arc(n1, n2, 1.0);
364 double d = dij(g, n0, n2, path);
372 DNode* n0 = g.insert_node(0);
373 DNode* n1 = g.insert_node(1);
375 g.insert_arc(n0, n1, 1.0);
380 double d1 = dij(g, n0, n1, path_forward);
384 double d2 = dij(g, n1, n0, path_backward);
385 EXPECT_EQ(d2, std::numeric_limits<double>::max());
419 double d = dij(g, n0, n3, path);
437 double d = dij(g, n0, n2, path);
454 double d = dij(g, n0, n2, path);
471 double d = dij(g, n0, n2, path);
473 EXPECT_NEAR(d, 0.8, 1e-9);
492 dij(g, center, tree);
513 double d = dij(g, n0, n3, path);
520 path_nodes.
append(it.get_current_node()->get_info());
522 ASSERT_EQ(path_nodes.
size(), 4);
524 auto pit = path_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);
597 EXPECT_NE(tree_start,
nullptr);
601 double d = dij(g, n0, n3, path);
637 double d = dij(g, n0, n3, path);
640 EXPECT_EQ(d, std::numeric_limits<double>::max());
658 int d = dij(g, n0, n2, path);
726 double d1 = dij(g, n0, n2, path1);
731 double d2 = dij(g, n2, n0, path2);
776 std::vector<Node*>
nodes(
N);
777 for (
int i = 0; i <
N; ++i)
780 for (
int i = 0; i <
N - 1; ++i)
787 EXPECT_EQ(d,
static_cast<double>(
N - 1));
795 std::vector<Node*>
nodes(
N);
796 for (
int i = 0; i <
N; ++i)
800 for (
int i = 0; i <
N; ++i)
801 for (
int j = i + 1; j <
N; ++j)
806 dij(g,
nodes[0], tree);
850 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
855 double d = dij(g, n0, n2, path);
874 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
879 double d = dij(g, n0, n1, path);
906 GTEST_SKIP() <<
"Graph type does not support parallel arcs (multigraph)";
911 double d = dij(g, n0, n3, path);
937 ASSERT_NE(tree_start,
nullptr);
943 EXPECT_FALSE(path_to_n2.
is_empty());
948 EXPECT_FALSE(path_to_n4.
is_empty());
953 path_nodes.
append(it.get_current_node()->get_info());
955 ASSERT_EQ(path_nodes.
size(), 5);
956 auto pit = path_nodes.
get_it();
957 EXPECT_EQ(pit.get_curr(), 0); pit.next();
958 EXPECT_EQ(pit.get_curr(), 1); pit.next();
959 EXPECT_EQ(pit.get_curr(), 2); pit.next();
960 EXPECT_EQ(pit.get_curr(), 3); pit.next();
961 EXPECT_EQ(pit.get_curr(), 4);
993 EXPECT_EQ(p1.size(), 2);
994 EXPECT_EQ(p2.size(), 2);
995 EXPECT_EQ(p3.
size(), 2);
1022 if (++calls == fail_at)
1023 throw std::runtime_error(
"arc weight not available");
1047 const auto i =
static_cast<size_t>(a->
get_info());
1048 return weights ==
nullptr ?
static_cast<long>(i) : (*weights)[i];
1052template <
typename HeapTag>
1055 HeapTag::template Heap>;
1065 const unsigned seed)
1068 std::uniform_int_distribution<size_t> pick(0,
num_nodes - 1);
1069 std::uniform_int_distribution<long>
weight(1, 50);
1074 std::map<std::pair<size_t, size_t>,
bool> used;
1075 while (
weights.size() < num_arcs)
1077 auto u = pick(
rng), v = pick(
rng);
1084 used[{u, v}] =
true;
1099 std::map<IGT::Node *, long> d{{s, 0}};
1100 for (
bool changed =
true; changed; )
1103 for (IGT::Arc_Iterator it(
g); it.has_curr(); it.next_ne())
1105 auto a = it.get_curr();
1108 for (
int dir = 0; dir < 2; ++dir, std::swap(u, v))
1109 if (d.contains(u)
and
1110 (not d.contains(v) or d[u] +
weight(a) < d[v]))
1134 for (IGT::Arc_Iterator it(tree); it.has_curr(); it.next_ne())
1135 total +=
weight(it.get_curr());
1156 EXPECT_EQ(dij.find_min_path(g, a, c, path), 21);
1158 dij.paint_min_paths_tree(g, a);
1159 EXPECT_EQ(dij.get_distance(c), 21);
1160 EXPECT_EQ(dij.get_min_path(c, path), 21);
1162 EXPECT_EQ(dij.copy_painted_min_paths_tree(g, painted), 21);
1165 dij.compute_min_paths_tree(g, a, tree);
1166 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1168 dij.compute_partial_min_paths_tree(g, a, c, tree);
1169 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1200 EXPECT_EQ(dij.find_min_path(g, a, c, path), 21);
1202 dij.paint_min_paths_tree(g, a);
1203 EXPECT_EQ(dij.get_distance(c), 21);
1204 EXPECT_EQ(dij.get_min_path(c, path), 21);
1206 EXPECT_EQ(dij.copy_painted_min_paths_tree(g, painted), 21);
1209 dij.compute_min_paths_tree(g, a, tree);
1210 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1212 dij.compute_partial_min_paths_tree(g, a, c, tree);
1213 EXPECT_EQ(dij.get_min_path(tree, c, path), 21);
1218TEST(DijkstraTotal, UsesTheDistanceItWasGiven) {
1230 for (
auto arc : {ab, bc})
1232 EXPECT_TRUE(plain(arc));
1233 EXPECT_TRUE(tripled(arc));
1234 EXPECT_TRUE(doubled(arc));
1236 EXPECT_EQ(plain.dist, 3 + 4);
1237 EXPECT_EQ(tripled.dist, 3 * (3 + 4));
1238 EXPECT_EQ(doubled.dist, 2 * (3 + 4));
1243TEST(DijkstraTotal, WhatTheDistanceThrowsReachesTheCaller) {
1250 const std::vector<long> weights = {3, 4};
1256 EXPECT_TRUE(total(ab));
1257 EXPECT_THROW(total(bc), std::runtime_error);
1258 EXPECT_EQ(total.dist, 3);
1289 auto s =
w.nodes[0];
1290 auto dist =
w.distances_from(s);
1291 ASSERT_GT(dist.size(), 1u) <<
"seed " <<
seed;
1295 for (
auto t :
w.nodes)
1301 const long cost = dij.find_min_path(
w.g, s, t, path);
1302 if (not dist.contains(t))
1304 EXPECT_EQ(cost, std::numeric_limits<long>::max());
1307 EXPECT_EQ(cost, dist[t]) <<
"seed " <<
seed;
1308 EXPECT_EQ(
w.cost_of(path, s, t), dist[t]) <<
"seed " <<
seed;
1311 dij.paint_min_paths_tree(
w.g, s);
1312 for (
auto [t, d] : dist)
1314 EXPECT_EQ(dij.get_distance(t), d) <<
"seed " <<
seed;
1316 EXPECT_EQ(dij.get_min_path(t, path), d) <<
"seed " <<
seed;
1317 EXPECT_EQ(
w.cost_of(path, s, t), d) <<
"seed " <<
seed;
1321 const long painted_weight = dij.copy_painted_min_paths_tree(
w.g, painted);
1323 EXPECT_EQ(painted_weight,
w.weight_of(painted)) <<
"seed " <<
seed;
1326 dij.compute_min_paths_tree(
w.g, s, tree);
1328 for (
auto [t, d] : dist)
1331 EXPECT_EQ(dij.get_min_path(tree, t, path), d) <<
"seed " <<
seed;
1332 EXPECT_EQ(
w.cost_of(path, s, t), d) <<
"seed " <<
seed;
1335 for (
auto [t, d] : dist)
1338 dij.compute_partial_min_paths_tree(
w.g, s, t, partial);
1340 EXPECT_EQ(dij.get_min_path(partial, t, path), d) <<
"seed " <<
seed;
1351 auto s =
w.nodes[0];
1352 auto dist =
w.distances_from(s);
1353 ASSERT_GT(dist.size(), 3u);
1354 auto far = std::max_element(dist.begin(), dist.end(),
1355 [](
auto & x,
auto &
y)
1356 { return x.second < y.second; })->first;
1362 const std::vector<std::function<void()>> methods = {
1365 EXPECT_EQ(dij.find_min_path(
w.g, s, far, path), dist[far]);
1368 dij.paint_min_paths_tree(
w.g, s);
1369 for (
auto [t, d] : dist)
1370 EXPECT_EQ(dij.get_distance(t), d);
1373 dij.compute_min_paths_tree(
w.g, s, tree);
1375 EXPECT_EQ(dij.get_min_path(tree, far, path), dist[far]);
1378 dij.compute_partial_min_paths_tree(
w.g, s, far, tree);
1380 EXPECT_EQ(dij.get_min_path(tree, far, path), dist[far]);
1384 for (
size_t m = 0;
m < methods.
size(); ++
m)
1388 const long calls = failure.
calls;
1389 ASSERT_GT(calls, 0) <<
"method " <<
m;
1391 for (
long k = 1;
k <= calls; ++
k)
1395 EXPECT_THROW(methods[
m](), std::runtime_error)
1396 <<
"method " <<
m <<
", call " <<
k;
1417 for (
int m = 0;
m < 4; ++
m)
1419 big->get_info() = INT_MAX - 10;
1424 EXPECT_THROW(dij.
find_min_path(g, a, c, path), std::overflow_error);
1431 std::overflow_error);
1435 std::overflow_error);
1440 big->get_info() = 1;
1441 EXPECT_EQ(dij.
find_min_path(g, a, c, path), 21) <<
"method " <<
m;
1463 EXPECT_THROW(dij.
get_min_path(n2, path), std::domain_error);
1474 for (GT::Node_Iterator it(tree); it.has_curr(); it.next_ne())
1476 auto tp = it.get_curr();
1477 auto gp = mapped_node<GT>(tp);
1478 ASSERT_NE(gp,
nullptr);
1479 EXPECT_EQ(mapped_node<GT>(gp), tp);
1480 EXPECT_EQ(gp->get_info(), tp->get_info());
1482 for (GT::Arc_Iterator it(tree); it.has_curr(); it.next_ne())
1484 auto ta = it.get_curr();
1485 auto ga = mapped_arc<GT>(ta);
1486 ASSERT_NE(ga,
nullptr);
1487 EXPECT_EQ(mapped_arc<GT>(ga), ta);
1488 EXPECT_EQ(ga->get_info(), ta->get_info());
1547 ::testing::InitGoogleTest(&argc, argv);
1548 return RUN_ALL_TESTS();
Dijkstra's shortest path algorithm.
::testing::Types< BinHeapTag, FibHeapTag > HeapTypes
WeightedDigraph::Node Node
auto next() noexcept(noexcept(std::declval< I & >().next())) -> decltype(std::declval< I & >().next())
Move to the next item; what the underlying iterator throws.
auto has_curr() const noexcept(noexcept(std::declval< I & >().has_curr())) -> decltype(std::declval< I & >().has_curr())
true if there is a current item.
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.
Distance::Distance_Type get_min_path(typename GT::Node *end, Path< GT > &path)
Extracts a shortest path to end from a previously painted graph.
void paint_min_paths_tree(const GT &g, typename GT::Node *start)
Paints on graph g the spanning tree of all shortest paths starting from start.
Distance::Distance_Type copy_painted_min_paths_tree(GT &g, GT &tree)
Extracts the painted shortest paths tree and puts it in tree.
Distance::Distance_Type find_min_path(const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path)
Computes the shortest path between start and end by painting the graph.
bool is_painted() const noexcept
Check if the graph has been painted.
bool has_computation() const noexcept
Check if a computation has been performed.
GT * get_graph() const noexcept
Get the graph of the last computation.
Distance::Distance_Type get_distance(typename GT::Node *node)
Gets the accumulated distance to a node after painting.
GT::Node * compute_min_paths_tree(const GT &g, typename GT::Node *start, GT &tree)
Computes the spanning tree of all shortest paths from the start node.
GT::Node * get_start_node() const noexcept
Get the start node of the last computation.
void compute_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree)
Computes the partial spanning tree of all shortest paths from start that contains the path from start...
bool paint_partial_min_paths_tree(const GT &g, typename GT::Node *start, typename GT::Node *end)
Paints on graph g the partial shortest paths tree starting from start and stopping when the end node ...
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< int > Node
The graph type.
Graph_Arc< double > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Number Theoretic Transform over Z / MOD Z.
Filtered iterator for outcoming arcs of a node.
Iterator on nodes and arcs of a path.
Node * get_current_node() const
Return the current node of a path.
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.
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
GT::Arc * get_current_arc_ne() const noexcept
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Default filter for filtered iterators on arcs.
Filtered iterator of adjacent arcs of a node.
Factor_Only_Distance(int f) noexcept
int operator()(IGT::Arc *a) const
int operator()(IGT::Arc *a) const
Distance_Failure * failure
const std::vector< long > * weights
long operator()(IGT::Arc *a) const
Table_Distance(const std::vector< long > &w, Distance_Failure *f=nullptr) noexcept
long weight_of(IGT &tree) const
long weight(IGT::Arc *a) const
Weighted_Graph(const size_t num_nodes, const size_t num_arcs, const unsigned seed)
long cost_of(const Path< IGT > &path, IGT::Node *s, IGT::Node *t) const
std::vector< IGT::Node * > nodes
std::map< IGT::Node *, long > distances_from(IGT::Node *s)
std::vector< long > weights
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
void expect_mapped_to_graph(GT &tree)
TYPED_TEST(DijkstraHeapTest, BasicShortestPath)
List_Graph< Graph_Node< int >, Graph_Arc< int > > IGT
TYPED_TEST_SUITE(DijkstraHeapTest, HeapTypes)
Generic graph and digraph implementations.