45#include <gtest/gtest.h>
67using namespace testing;
142 n0 =
g.insert_node(0);
143 n1 =
g.insert_node(1);
144 n2 =
g.insert_node(2);
145 n3 =
g.insert_node(3);
158 for (
long i = 0; i < n; ++i)
180 return static_cast<long long>(_getpid());
182 return static_cast<long long>(getpid());
188 const auto *test_info = UnitTest::GetInstance()->current_test_info();
189 string base = string(
"floyd_test_latex_") + test_info->test_suite_name() +
"_" +
192 for (
auto &ch : base)
193 if (ch ==
'/' or ch ==
'\\' or ch ==
' ')
196 return (std::filesystem::temp_directory_path() / base).
string();
202 for (
int i = 0; i < 4; ++i)
203 nodes.push_back(
g.insert_node(i));
237 const long n = g.get_num_nodes();
238 for (
long i = 0; i < n; ++i)
239 EXPECT_DOUBLE_EQ(dist(i, i), 0.0) <<
"Diagonal at " << i <<
" should be 0";
250 long idx0 = get_index(dist, n0);
251 long idx1 = get_index(dist, n1);
254 EXPECT_DOUBLE_EQ(dist(idx0, idx1), 1.0);
264 long idx0 = get_index(dist, n0);
265 long idx2 = get_index(dist, n2);
268 EXPECT_DOUBLE_EQ(dist(idx0, idx2), 3.0);
278 long idx0 = get_index(dist, n0);
279 long idx3 = get_index(dist, n3);
282 EXPECT_DOUBLE_EQ(dist(idx0, idx3), 4.0);
292 long idx1 = get_index(dist, n1);
293 long idx0 = get_index(dist, n0);
296 EXPECT_TRUE(isinf(dist(idx1, idx0)));
306 const long n = g.get_num_nodes();
309 for (
long i = 0; i < n; ++i)
310 for (
long j = 0; j < n; ++j)
311 EXPECT_EQ(dist(i, j), dist(j, i))
312 <<
"Distance should be symmetric for i=" << i <<
", j=" << j;
323 long idx0 = -1, idx3 = -1;
324 const long n = g.get_num_nodes();
325 for (
long i = 0; i < n; ++i) {
326 if (dist(i)->get_info() == 0) idx0 = i;
327 if (dist(i)->get_info() == 3) idx3 = i;
331 EXPECT_EQ(dist(idx0, idx3), 6);
346 long idx0 = get_index(dist, n0);
351 EXPECT_EQ(p.
size(), 1u);
362 long idx0 = get_index(dist, n0);
363 long idx1 = get_index(dist, n1);
368 EXPECT_EQ(p.
size(), 2u);
379 long idx0 = get_index(dist, n0);
380 long idx2 = get_index(dist, n2);
385 EXPECT_EQ(p.
size(), 3u);
400 return to_string(mat(
static_cast<long>(i))->get_info());
410 long val = mat(
static_cast<long>(i),
static_cast<long>(j));
421 auto val = mat(
static_cast<long>(i),
static_cast<long>(j));
422 if (isinf(
static_cast<double>(val)))
434 string filename = latex_temp_filename();
435 ofstream
output(filename);
444 ifstream input(filename);
447 string content = ss.str();
449 EXPECT_NE(content.find(
"\\begin{figure}"), string::npos);
450 EXPECT_NE(content.find(
"\\end{figure}"), string::npos);
458 string filename = latex_temp_filename();
459 ofstream
output(filename);
467 ifstream input(filename);
470 string content = ss.str();
473 EXPECT_NE(content.find(
"D_0"), string::npos);
474 EXPECT_NE(content.find(
"P_0"), string::npos);
475 EXPECT_NE(content.find(
"D_1"), string::npos);
476 EXPECT_NE(content.find(
"P_1"), string::npos);
484 string filename = latex_temp_filename();
485 ofstream
output(filename);
493 ifstream input(filename);
496 string content = ss.str();
500 const long n = g.get_num_nodes();
501 for (
long i = 0; i <= n; ++i) {
503 EXPECT_NE(content.find(dmat), string::npos) <<
"Missing " << dmat;
511TEST(SingleNodeTest, FloydOnSingleNode)
524 EXPECT_EQ(dist(0L, 0L), 0);
527TEST(TwoNodeTest, FloydOnTwoNodes)
542 long idx0 = -1, idx1 = -1;
543 for (
long i = 0; i < 2; ++i) {
544 if (dist(i)->get_info() == 0) idx0 = i;
545 if (dist(i)->get_info() == 1) idx1 = i;
548 EXPECT_EQ(dist(idx0, idx0), 0);
549 EXPECT_EQ(dist(idx1, idx1), 0);
550 EXPECT_EQ(dist(idx0, idx1), 5);
576 vector<long> idx(4, -1);
577 for (
long i = 0; i < 4; ++i)
578 idx[dist(i)->get_info()] = i;
581 EXPECT_EQ(dist(idx[0], idx[1]), 1);
582 EXPECT_EQ(dist(idx[1], idx[0]), 1);
585 EXPECT_EQ(dist(idx[2], idx[3]), 2);
586 EXPECT_EQ(dist(idx[3], idx[2]), 2);
617 vector<long> idx(4, -1);
618 for (
long i = 0; i < 4; ++i)
619 idx[dist(i)->get_info()] = i;
622 EXPECT_EQ(dist(idx[0], idx[1]), -5);
623 EXPECT_EQ(dist(idx[1], idx[0]), 6);
632TEST(CompleteGraphTest, FloydOnCompleteGraph)
638 vector<Graph::Node*>
nodes;
640 for (
int i = 0; i <
N; ++i)
644 for (
int i = 0; i <
N; ++i)
645 for (
int j = 0; j <
N; ++j)
655 for (
long i = 0; i <
N; ++i) {
656 for (
long j = 0; j <
N; ++j) {
658 EXPECT_EQ(dist(i, j), 0);
660 EXPECT_EQ(dist(i, j), 1);
680TEST(CustomSemiringTest, MaxMinSemiring)
700 floyd_all_shortest_paths<Graph, MaxCompare, MinPlus>(g, cap, path);
711TEST(StressTest, FloydOnLargerGraph)
717 vector<Graph::Node*>
nodes;
719 for (
int i = 0; i <
N; ++i)
723 for (
int i = 0; i <
N - 1; ++i)
727 for (
int i = 0; i <
N - 2; i += 2)
736 vector<long> idx(
N, -1);
737 for (
long m = 0;
m <
N; ++
m) {
738 int info = dist(
m)->get_info();
743 for (
int i = 0; i <
N; ++i)
744 for (
int j = i; j <
N; ++j)
745 EXPECT_EQ(dist(idx[i], idx[j]), j - i) <<
"Distance from " << i <<
" to " << j;
759 const long n = g.get_num_nodes();
762 for (
long i = 0; i < n; ++i) {
763 for (
long j = 0; j < n; ++j) {
765 EXPECT_EQ(path(i, j), j) <<
"Diagonal should point to self";
767 long next = path(i, j);
782 const long n = g.get_num_nodes();
785 for (
long i = 0; i < n; ++i) {
786 for (
long j = 0; j < n; ++j) {
787 for (
long k = 0;
k < n; ++
k) {
788 if (!isinf(dist(i,
k)) && !isinf(dist(
k, j))) {
789 EXPECT_LE(dist(i, j), dist(i,
k) + dist(
k, j))
790 <<
"Triangle inequality violated for i=" << i <<
", j=" << j <<
", k=" <<
k;
810 const long n = g.get_num_nodes();
813 for (
long i = 0; i < n; ++i)
814 EXPECT_EQ(dist(i, i), 0);
817 bool found_finite =
false;
818 for (
long i = 0; i < n; ++i)
819 for (
long j = 0; j < n; ++j)
823 EXPECT_TRUE(found_finite);
830TEST(NegativeWeightsTest, FloydWithNegativeWeights)
852 long idx0 = -1, idx2 = -1;
853 for (
long i = 0; i < 3; ++i) {
854 if (dist(i)->get_info() == 0) idx0 = i;
855 if (dist(i)->get_info() == 2) idx2 = i;
859 EXPECT_DOUBLE_EQ(dist(idx0, idx2), -2.0);
866TEST(DenseGraphTest, FloydOnDenseGraph)
872 vector<Graph::Node*>
nodes;
874 for (
int i = 0; i <
N; ++i)
878 for (
int i = 0; i <
N; ++i)
879 for (
int j = 0; j <
N; ++j)
889 for (
long i = 0; i <
N; ++i)
890 EXPECT_EQ(dist(i, i), 0);
893 for (
long i = 0; i <
N; ++i)
894 for (
long j = 0; j <
N; ++j)
896 <<
"Should be reachable from " << i <<
" to " << j;
911 cost += it.get_current_arc_ne()->get_info().get_distance();
917 long index_of(Mat &
m,
int info)
919 for (
long i = 0; i < static_cast<long>(
m.get_num_nodes()); ++i)
920 if (
m(i)->get_info() == info)
926 string parallel_arcs_tex_name()
928 const auto *info = UnitTest::GetInstance()->current_test_info();
930 const long long pid = _getpid();
932 const long long pid = getpid();
934 string base = string(
"floyd_parallel_") + info->test_suite_name() +
"_" +
935 info->name() +
"_" +
to_string(pid) +
".tex";
936 for (
auto &ch : base)
937 if (ch ==
'/' or ch ==
'\\' or ch ==
' ')
939 return (std::filesystem::temp_directory_path() / base).string();
948 void build_parallel_arcs(
GT & g,
bool heavier_first)
953 auto insert_light = [&] ()
960 if (not heavier_first)
985 using GT = TypeParam;
986 for (
const bool heavier_first : {
true,
false})
989 build_parallel_arcs(g, heavier_first);
995 const long i0 = index_of(dist, 0), i1 = index_of(dist, 1);
996 const long i2 = index_of(dist, 2);
997 EXPECT_EQ(dist(i0, i1), 1) <<
"heavier first: " << heavier_first;
998 EXPECT_EQ(dist(i0, i2), 2) <<
"heavier first: " << heavier_first;
999 EXPECT_EQ(path(i0, i2), i1);
1000 EXPECT_EQ(dist(i1, i0),
1006 EXPECT_EQ(p.size(), 2u);
1007 EXPECT_EQ(path_cost(p), 1);
1011 EXPECT_EQ(q.
size(), 3u);
1012 EXPECT_EQ(path_cost(q), 2);
1018 using GT = TypeParam;
1019 for (
const bool heavier_first : {
true,
false})
1022 build_parallel_arcs(g, heavier_first);
1029 const string filename = parallel_arcs_tex_name();
1031 ofstream
output(filename);
1034 (g, latex_dist, latex_path,
output);
1036 std::filesystem::remove(filename);
1038 for (
long i = 0; i < 3; ++i)
1039 for (
long j = 0; j < 3; ++j)
1041 EXPECT_EQ(latex_dist(i, j), dist(i, j)) << i <<
", " << j;
1042 EXPECT_EQ(latex_path(i, j), path(i, j)) << i <<
", " << j;
1044 EXPECT_EQ(latex_dist(index_of(dist, 0), index_of(dist, 1)), 1);
1050 using GT = TypeParam;
1052 for (
const bool heavier_first : {
true,
false})
1055 build_parallel_arcs(g, heavier_first);
1059 dist.template operate_all_arcs_matrix<Initialize_Dist<Dist_Mat>>(&path);
1061 const long i0 = index_of(dist, 0), i1 = index_of(dist, 1);
1062 const long i2 = index_of(dist, 2);
1063 for (
long i = 0; i < 3; ++i)
1065 EXPECT_EQ(dist(i, i), 0);
1066 EXPECT_EQ(path(i, i), i);
1068 EXPECT_EQ(dist(i0, i1), 1);
1069 EXPECT_EQ(path(i0, i1), i1);
1070 EXPECT_EQ(dist(i0, i2), 3);
1071 EXPECT_EQ(dist(i1, i0),
1078 using GT = TypeParam;
1090 const long i0 = index_of(dist, 0), i1 = index_of(dist, 1);
1091 EXPECT_EQ(dist(i0, i0), 0);
1092 EXPECT_EQ(dist(i1, i1), 0);
1093 EXPECT_EQ(path(i0, i0), i0);
1094 EXPECT_EQ(path(i1, i1), i1);
1095 EXPECT_EQ(dist(i0, i1), 2);
1099 EXPECT_EQ(p.
size(), 2u);
1100 EXPECT_EQ(path_cost(p), 2);
1103TEST(MatPathTest, FindMinPathThrowsWithoutAnArc)
1111 const long i0 = index_of(path, 0), i1 = index_of(path, 1);
1115 EXPECT_THROW(
find_min_path(path, i0, i1, p), std::invalid_argument);
1118TEST(MatPathTest, FindMinPathWithArcsWithoutDistances)
1130 const long i0 = index_of(path, 0), i1 = index_of(path, 1);
1131 const long i2 = index_of(path, 2);
1137 ASSERT_EQ(p.
size(), 3u);
1144 finder(path, i0, i2, q);
1145 finder(path, n0, n2,
r);
1146 EXPECT_EQ(q.size(), 3u);
1147 EXPECT_EQ(
r.size(), 3u);
1150TEST(MatPathTest, FindMinPathTakesTheLightestByItsCompare)
1162 const long i0 = index_of(path, 0), i1 = index_of(path, 1);
1165 Path<Graph> by_less(g), by_greater(g), by_nodes(g), by_finder(g);
1167 find_min_path<Mat, std::greater<>>(path, i0, i1, by_greater);
1168 find_min_path<Mat, std::greater<>>(path, n0, n1, by_nodes);
1171 EXPECT_EQ(path_cost(by_less), 1);
1172 EXPECT_EQ(path_cost(by_greater), 5);
1173 EXPECT_EQ(path_cost(by_nodes), 5);
1174 EXPECT_EQ(path_cost(by_finder), 5);
List_Graph< Node, Arc > Graph
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Auxiliary adjacency matrix with custom entry type.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Arc Arc
typename BaseGraph::Node Node
Functor wrapper for find_min_path.
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Number Theoretic Transform over Z / MOD Z.
Iterator on nodes and arcs of a path.
bool has_current_arc() const noexcept
Return true if iterator has current arc.
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.
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Test fixture with integer weights.
string latex_temp_filename() const
static long long process_id() noexcept
IntDistanceArc::Distance_Type Dist
Test fixture with a simple weighted graph.
long get_index(Ady_Mat< Graph, Dist > &mat, Node *node) const
DistanceArc::Distance_Type Dist
bool is_digraph() const noexcept
Return true if the graph this is directed.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynArray< Graph::Node * > nodes
void floyd_all_shortest_paths_latex(GT &g, Ady_Mat< GT, typename GT::Arc_Type::Distance_Type > &dist, Ady_Mat< GT, long > &path, std::ofstream &output)
Floyd-Warshall algorithm with LaTeX step-by-step output.
void floyd_all_shortest_paths(GT &g, Ady_Mat< GT, typename GT::Arc_Type::Distance_Type > &dist, Ady_Mat< GT, long > &path)
Compute all-pairs shortest paths using Floyd-Warshall algorithm.
void find_min_path(Mat &p, long src_idx, long tgt_idx, Path< typename Mat::Graph_Type > &path)
Build a minimum path from Floyd-Warshall path matrix using indices.
Floyd-Warshall algorithm with LaTeX output generation.
TYPED_TEST_SUITE(ParallelArcsTest, ParallelArcsGraphs)
TYPED_TEST(ParallelArcsTest, TheLightestArcCounts)
TEST_F(FloydSimpleGraphTest, DiagonalIsZero)
Types< List_Digraph< Graph_Node< int >, Graph_Arc< IntDistanceArc > >, List_Graph< Graph_Node< int >, Graph_Arc< IntDistanceArc > >, List_SDigraph< Graph_Snode< int >, Graph_Sarc< IntDistanceArc > >, List_SGraph< Graph_Snode< int >, Graph_Sarc< IntDistanceArc > >, Array_Digraph< Graph_Anode< int >, Graph_Aarc< IntDistanceArc > >, Array_Graph< Graph_Anode< int >, Graph_Aarc< IntDistanceArc > > > ParallelArcsGraphs
Path reconstruction from Floyd-Warshall path matrices.
Main namespace for Aleph-w library functions.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
void next()
Advance all underlying iterators (bounds-checked).
Arc of graph implemented with double-linked adjacency lists.
Arc info type with required Distance_Type and constants.
Distance_Type get_distance() const
static constexpr Distance_Type Zero_Distance
DistanceArc(Distance_Type d)
static constexpr Distance_Type Max_Distance
Integer distance arc type.
Distance_Type get_distance() const
IntDistanceArc(Distance_Type d)
static constexpr Distance_Type Zero_Distance
static constexpr Distance_Type Max_Distance
bool operator()(int a, int b) const
int operator()(int a, int b) const
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Array-based graph implementation.
Generic graph and digraph implementations.
Adjacency matrix representations for graphs.
Simple graph implementation with adjacency lists.