45#include <gtest/gtest.h>
67#include <unordered_map>
80 void num(std::string &
out,
const unsigned long long x)
83 out += std::to_string(x);
86 void real(std::string &
out,
const double x)
89 std::snprintf(buf,
sizeof buf,
" %.6g", x);
96 template <
class GT,
class Node_Info,
class Arc_Info>
100 std::unordered_map<const void *, size_t> number;
103 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
105 auto p = it.get_curr();
106 number.emplace(p, number.size());
110 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
112 auto a = it.get_curr();
121 std::string describe(
const GT & g)
123 return describe(g, [] (std::string &,
auto) {}, [] (std::string &,
auto) {});
131 std::string describe_euclidian(
const EGraph & g)
133 using Point = std::pair<long, long>;
134 std::vector<Point>
nodes;
135 for (EGraph::Node_Iterator it(g); it.has_curr(); it.next_ne())
136 nodes.emplace_back(it.get_curr()->get_info().x, it.get_curr()->get_info().y);
139 std::vector<std::tuple<Point, Point, double>>
arcs;
140 for (EGraph::Arc_Iterator it(g); it.has_curr(); it.next_ne())
142 auto a = it.get_curr();
148 arcs.emplace_back(u, v, a->get_info());
150 std::sort(
arcs.begin(),
arcs.end());
155 for (
const auto & [x,
y] :
nodes)
161 for (
const auto & [u, v,
w] :
arcs)
175 for (
auto c = p->
get_left_child(); c !=
nullptr; c = c->get_right_sibling())
178 for (
auto c = p->
get_left_child(); c !=
nullptr; c = c->get_right_sibling())
187 std::vector<UGraph::Node *> n;
188 for (
int i = 0; i < 12; ++i)
190 for (
int c = 0; c < 12; c += 6)
191 for (
int i = c; i < c + 6; ++i)
192 for (
int j = i + 1; j < c + 6; ++j)
205 std::string describe_cut(
UGraph & g,
const size_t size,
210 std::unordered_map<const void *, size_t> number;
212 number.emplace(it.get_curr(), number.size());
213 std::unordered_map<const void *, size_t> arc_number;
215 arc_number.emplace(it.get_curr(), arc_number.size());
220 for (
auto it = vs.
get_it(); it.has_curr(); it.next_ne())
221 num(
out, number.at(it.get_curr()));
223 for (
auto it = vt.
get_it(); it.has_curr(); it.next_ne())
224 num(
out, number.at(it.get_curr()));
226 for (
auto it = cut.
get_it(); it.has_curr(); it.next_ne())
227 num(
out, arc_number.at(it.get_curr()));
231 std::uint64_t fnv1a(
const std::string & s)
233 std::uint64_t
h = 0xcbf29ce484222325ULL;
234 for (
const unsigned char c : s)
237 h *= 0x100000001b3ULL;
243 std::vector<std::string> lines_for(
const unsigned long s)
245 std::vector<std::string> lines;
246 std::string
out =
"== seed";
248 lines.push_back(
out);
252 lines.push_back(
"graph sparse:" + describe(gen(30,
size_t(60))));
253 lines.push_back(
"graph p:" + describe(gen(25, 0.2)));
254 lines.push_back(
"graph sparse, not connected:" + describe(gen(30,
size_t(20),
false)));
255 lines.push_back(
"graph eulerian:" + describe(gen.eulerian(20,
size_t(40))));
256 lines.push_back(
"graph eulerian p:" + describe(gen.eulerian(16, 0.3)));
257 lines.push_back(
"graph hamiltonian:" + describe(gen.sufficient_hamiltonian(14, 0.3)));
261 lines.push_back(
"digraph sparse:" + describe(gen(30,
size_t(70))));
262 lines.push_back(
"digraph p:" + describe(gen(20, 0.15)));
263 lines.push_back(
"digraph eulerian:" + describe(gen.eulerian(16,
size_t(40))));
264 lines.push_back(
"digraph eulerian p:" + describe(gen.eulerian(12, 0.3)));
265 lines.push_back(
"digraph hamiltonian:" + describe(gen.sufficient_hamiltonian(12, 0.3)));
269 Net * net = gen(4, 6);
270 lines.push_back(
"network:" + describe(*net, [] (std::string &,
auto) {},
271 [] (std::string & o,
auto a) { real(o, a->cap); }));
273 net = gen(3, 5, 0.3, 50, 10, 0.5, 0.1);
274 lines.push_back(
"network 2:" + describe(*net, [] (std::string &,
auto) {},
275 [] (std::string & o,
auto a) { real(o, a->cap); }));
280 for (
const size_t n : {1, 2, 9, 60})
283 out =
"tree " + std::to_string(n) +
":";
285 lines.push_back(
out);
290 UGraph g = karger_graph();
294 size_t size = karger(g, vs, vt, cut, 3);
295 out =
"karger:" + describe_cut(g,
size, vs, vt, cut);
299 size = karger.fast(g, vs, vt, cut, 2);
300 out +=
" fast:" + describe_cut(g,
size, vs, vt, cut);
301 num(
out, karger.compute_min_cut_size(g, 4));
306 num(
out, karger.compute_min_cut_size(g, 1));
307 lines.push_back(
out);
313 for (
auto it = seeds.
get_it(); it.has_curr(); it.next_ne())
315 lines.push_back(
out);
319 for (
int i = 0; i < 1000; ++i)
322 for (
const int x : sampler.get_sample())
325 for (
int i = 0; i < 300; ++i)
328 for (
const int x : sampler.get_sample())
330 std::vector<int> v(500);
331 for (
int i = 0; i < 500; ++i)
336 lines.push_back(
out);
339 const EGraph g = gen_random_euclidian_graph<EGraph>(25, 50, 40, 30,
340 static_cast<unsigned int>(s));
341 lines.push_back(
"euclidian:" + describe_euclidian(g));
344 init_jsw(
static_cast<std::uint32_t
>(s));
346 for (
int b = 0; b < 256; ++b)
348 const unsigned char c =
static_cast<unsigned char>(b);
352 lines.push_back(
out);
359TEST(RandomGenerators, WhatTheyMadeWithGsl)
368 static const std::uint64_t golden[] = {
370 0xbf6aeb12f149fed4ULL, 0x6f511526ebaf569dULL, 0x4deba50a2eb8f7bfULL,
371 0x381da76af356cab3ULL, 0x7d7424f2d8d0b8e1ULL, 0xc50c17f5f47b752bULL,
372 0x1f0f26144038e7afULL, 0xb31eb3736eb3f411ULL, 0x6e272aeee69e1748ULL,
373 0x8003699f11cf9091ULL, 0xdd2be74b369a4f2cULL, 0xe4856f3b289dccc4ULL,
374 0x879b4e00418acd95ULL, 0x604a5935b83cf980ULL, 0x869a8495dc14f410ULL,
375 0x1fd3b68b81e0134cULL, 0x7deb970b96ba0f48ULL, 0xc021367a91012c46ULL,
376 0x43a0c494702d1e9dULL, 0x3d624cbf1b43cc29ULL, 0x023cff92af896d37ULL,
377 0xcc42b61139e1f130ULL, 0xc231bc7683da6e00ULL,
379 0xbf6aec12f14a0087ULL, 0x56d0bbcf5af437ceULL, 0x3ab41b2c09e37704ULL,
380 0x573c642fbf2f41e0ULL, 0x070de05da19320c0ULL, 0x264d306783592285ULL,
381 0x6cedd2e2e32a2cb3ULL, 0xdc9175f4ec78fecdULL, 0x54041d6fd6ca1e58ULL,
382 0x4a3a05c605ca3a0eULL, 0x52525b04fd596d26ULL, 0x16797da1dd32516eULL,
383 0x03ca174932a6cec9ULL, 0x8a6f04069cf466d3ULL, 0x869a8495dc14f410ULL,
384 0x1fd3b68b81e0134cULL, 0x6e42745bebcf360aULL, 0xf94ee3ba7def82ccULL,
385 0x961132833c4dbbfbULL, 0x08460e090b658b25ULL, 0xfc3619b02331dd7eULL,
386 0xc8efa1228c3f4cfbULL, 0xd011d4ff65e89969ULL,
388 0x118e2e24d31a4c4fULL, 0x47a3a754887a996bULL, 0x54361a13cda8abb7ULL,
389 0x175dd2f949705fd8ULL, 0x87fdc70c5167a565ULL, 0x6faffbcd27687fedULL,
390 0xcce4ecf6e78fe225ULL, 0x3965f88a369d62a9ULL, 0x3435461ab9f4aa48ULL,
391 0xa5ec352947df983cULL, 0x755a5e0527a471bfULL, 0x53b99ddd4a1ef6b0ULL,
392 0x4dc48e12ede388d7ULL, 0xdcb58dc54b6f1424ULL, 0x869a8495dc14f410ULL,
393 0x1fd3b68b81e0134cULL, 0x01436d933b157c12ULL, 0xaa77eac337f88546ULL,
394 0x5b422700949e4ed5ULL, 0x2e3c29003279ca09ULL, 0xa38ea345c62a0ffcULL,
395 0xdf00ab511e4a950fULL, 0x7e1acb6a3066d4d8ULL,
397 0x0d24555385f3f048ULL, 0x126d1c47f46d7d7bULL, 0x6ba1fbd423966727ULL,
398 0x4eb4a207cb87715dULL, 0x21656c559ce34bbdULL, 0x594ba60185f79232ULL,
399 0x62fec0c48f08de63ULL, 0x691ac06dd47e445aULL, 0x83f445956ca9ed11ULL,
400 0x2745874268d4fa70ULL, 0x902e749f5766405dULL, 0x5693c2ea7bab028bULL,
401 0xb136634f8e41d264ULL, 0x296a197a405ebf8eULL, 0x869a8495dc14f410ULL,
402 0x1fd3b68b81e0134cULL, 0x0ae4cc656280e88cULL, 0xc5223f7e67c1b05aULL,
403 0x791ea0482fe7868fULL, 0x1cbed8d3f9ac9a59ULL, 0xec3276c7d3d4d486ULL,
404 0x21bc3e31556dc712ULL, 0x11b74001e3c3be38ULL,
406 0x0d24565385f3f1fbULL, 0x6f511526ebaf569dULL, 0x4deba50a2eb8f7bfULL,
407 0x381da76af356cab3ULL, 0x7d7424f2d8d0b8e1ULL, 0xc50c17f5f47b752bULL,
408 0x1f0f26144038e7afULL, 0xb31eb3736eb3f411ULL, 0x6e272aeee69e1748ULL,
409 0x8003699f11cf9091ULL, 0xdd2be74b369a4f2cULL, 0xe4856f3b289dccc4ULL,
410 0x879b4e00418acd95ULL, 0x604a5935b83cf980ULL, 0x869a8495dc14f410ULL,
411 0x1fd3b68b81e0134cULL, 0x7deb970b96ba0f48ULL, 0xc021367a91012c46ULL,
412 0x43a0c494702d1e9dULL, 0x3d624cbf1b43cc29ULL, 0x6ba91bf1c4cdde62ULL,
413 0xcc42b61139e1f130ULL, 0xc231bc7683da6e00ULL,
415 0x042cb55380c9916dULL, 0xbb3a9f9aadeda92fULL, 0x72d4029847eef99cULL,
416 0x0072c6a658d68841ULL, 0x342bea24d3ed1ff0ULL, 0x4db7bd2097f438f1ULL,
417 0xafa2dc2d2911b783ULL, 0x16c2089df454e958ULL, 0x582699ac9686606bULL,
418 0x6d8d2143b2a23eaeULL, 0x05a6eb57a52c50f8ULL, 0x885da9241efdf674ULL,
419 0x559d5766aa658ddeULL, 0xad574524c68180abULL, 0x869a8495dc14f410ULL,
420 0x1fd3b68b81e0134cULL, 0xab1cf6530f406d02ULL, 0xdfacce1ca0dc16e0ULL,
421 0x1dbaf9068def4d74ULL, 0xfe5efd0c084ef1d7ULL, 0xe3914214f007e432ULL,
422 0xcd587691155ba501ULL, 0x7b22f892cc2c2d90ULL,
431 static const std::uint64_t golden_jsw_long32[] = {
432 0x3ff9424109e6654dULL, 0x49f945dd5ec91e54ULL, 0x3521995e48d2e088ULL,
433 0x575c8e62a68e5ce6ULL, 0x3ff9424109e6654dULL,
438 const unsigned long long seeds[] = {0, 1, 12345, 0xfffffffeULL, 0xffffffffULL,
440 const size_t lines_per_seed = 23;
442 for (
size_t k = 0;
k < std::size(seeds); ++
k)
444 const unsigned long long s = seeds[
k];
445 if (s > std::numeric_limits<unsigned long>::max())
451 const std::vector<std::string> lines = lines_for(
static_cast<unsigned long>(s));
452 ASSERT_EQ(lines.size(), lines_per_seed);
453 for (
size_t j = 0; j < lines_per_seed; ++j, ++i)
455 ASSERT_LT(i, std::size(golden));
457 if (
sizeof(
long) == 4
and j == lines_per_seed - 1)
459 if (
sizeof(std::size_t) != 8)
461 ASSERT_LT(
k, std::size(golden_jsw_long32));
464 EXPECT_EQ(fnv1a(lines[j]),
expected) << lines[j];
467 EXPECT_EQ(i, std::size(golden));
470TEST(RandomGenerators, TheGeneratorsAreNotCopied)
491 struct Move_Throwing_Filter
493 static inline bool armed =
false;
495 Move_Throwing_Filter() =
default;
497 Move_Throwing_Filter(
const Move_Throwing_Filter &) =
default;
499 Move_Throwing_Filter(Move_Throwing_Filter &&)
502 throw std::runtime_error(
"the move of the filter");
505 Move_Throwing_Filter & operator = (
const Move_Throwing_Filter &) =
default;
507 Move_Throwing_Filter & operator = (Move_Throwing_Filter &&)
510 throw std::runtime_error(
"the move of the filter");
514 bool operator () (
UGraph::Arc *)
const noexcept {
return true; }
518TEST(RandomGenerators, AKargerKeepsItsGeneratorWhenTheMoveOfItsFilterThrows)
521 UGraph g = karger_graph();
524 Move_Throwing_Filter::armed =
true;
525 EXPECT_THROW((
void) Karger(std::move(
k)), std::runtime_error);
526 EXPECT_THROW(other = std::move(
k), std::runtime_error);
527 Move_Throwing_Filter::armed =
false;
530 const std::pair<Karger *, unsigned long> cases[] = {{&
k, 7}, {&other, 8}};
531 for (
const auto & [karger,
seed] : cases)
536 const size_t c1 = (*karger)(g, vs1, vt1, cut1, 5);
537 const size_t c2 = twin(g, vs2, vt2, cut2, 5);
538 EXPECT_EQ(describe_cut(g, c1, vs1, vt1, cut1), describe_cut(g, c2, vs2, vt2, cut2))
543TEST(RandomGenerators, TheEuclideanGeneratorClearsItsGenerator)
545 (void) gen_random_euclidian_graph<EGraph>(10, 20, 10, 10, 3u);
549 EXPECT_THROW((
void) gen_random_euclidian_graph<EGraph>(0, 1, 10, 10, 3u), std::domain_error);
553TEST(RandomGenerators, AReservoirRefusesTheElementsThatItsGeneratorCannotPlace)
558 for (
int i = 0; i < 3; ++i)
561 EXPECT_NO_THROW(s.
update(7));
563 EXPECT_THROW(s.
update(8), std::runtime_error);
567TEST(RandomGenerators, TheSeedsOfABloomFilterAreTheNumbersOfGsl)
569 for (
const unsigned long s : {0UL, 7UL, 0xfffffffeUL, 0xffffffffUL})
571 gsl_rng *
r = gsl_rng_alloc(gsl_rng_mt19937);
572 gsl_rng_set(
r, s % gsl_rng_max(
r));
575 ASSERT_EQ(seeds.
size(), 9u);
576 for (
auto it = seeds.
get_it(); it.has_curr(); it.next_ne())
577 EXPECT_EQ(it.get_curr(), gsl_rng_get(
r)) <<
"seed " << s;
582TEST(RandomGenerators, TheTableOfJswHashHasTheNumbersOfGsl)
587 const size_t rotated = (size_t(16777551) << 1) | (
size_t(16777551) >> 31);
588 for (
const std::uint32_t s : {0u, 1u, 52679987u, 0xfffffffeu, 0xffffffffu})
591 gsl_rng *
r = gsl_rng_alloc(gsl_rng_mt19937);
592 gsl_rng_set(
r, s % gsl_rng_max(
r));
593 for (
int b = 0; b < 256; ++b)
595 const unsigned char c =
static_cast<unsigned char>(b);
596 const long entry =
static_cast<long>(gsl_rng_get(
r));
597 EXPECT_EQ(
jsw_hash(&c, 1), rotated ^
static_cast<size_t>(entry))
598 <<
"seed " << s <<
", byte " << b;
605TEST(RandomGenerators, TheSampleOfAReservoirIsThatOfGsl)
609 for (
const unsigned long s : {0UL, 3UL, 0xffffffffUL})
611 gsl_rng *
r = gsl_rng_alloc(gsl_rng_mt19937);
614 for (
int i = 0; i < 2000; ++i)
617 else if (
const unsigned long j = gsl_rng_uniform_int(
r, i + 1); j < 16)
622 for (
int i = 0; i < 2000; ++i)
624 std::vector<int> got;
625 for (
const int x : sampler.get_sample())
627 EXPECT_EQ(got,
expected) <<
"seed " << s;
635TEST(RandomGenerators, TheSeedsOfABloomFilterAreTheNumbersOfGsl)
637 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
640TEST(RandomGenerators, TheTableOfJswHashHasTheNumbersOfGsl)
642 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
645TEST(RandomGenerators, TheSampleOfAReservoirIsThatOfGsl)
647 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
Karger's randomized min-cut algorithm.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Probabilistic set membership with Bloom filters.
size_t size_t int32_t * out
Bloom filter implementation.
Generic directed graph (digraph) wrapper template.
bool has_curr() const noexcept
Return true if the iterator has current item.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
void empty() noexcept
empty the list
Arc for graphs implemented with simple adjacency lists.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Karger's randomized minimum cut algorithm.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Represents a point with rectangular coordinates in a 2D plane.
Random directed graph (digraph) generator.
Random undirected graph generator.
Incremental reservoir sampler (Algorithm R).
void update(const T &val)
Process a new element from the stream.
uint64_t rng_range() const noexcept
RNG output range (max - min + 1).
void set_n_seen_for_testing(const size_t n)
Forcibly set the internal stream counter (for testing only).
size_t total_seen() const noexcept
Total elements processed so far.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
Minimal std::expected-style result type for C++20.
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.
Generator for uniformly random trees.
Common utilities for Euclidean graphs.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
auto reservoir_sample(Itor beg, Sent end, size_t k, unsigned long seed=static_cast< unsigned long >(std::chrono::high_resolution_clock::now().time_since_epoch().count()))
Convenience function to sample k elements from a range.
and
Check uniqueness with explicit hash + equality functors.
const unsigned Default_Hash_Seed
Gsl_Compatible_Rng * rand_gen
Internal RNG handle used by the helper functors in this header.
int num(Node *p)
The number of a node of a built graph.
Random graph generation utilities.
Random network flow graph generation.
Reservoir sampling for random selection from data streams.
Arc of graph implemented with double-linked adjacency lists.
Iterator on all arcs of a graph.
Simple integer point used as node payload by the helper functors in this header.
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Generic graph and digraph implementations.
Network flow graph structures.
Simple graph implementation with adjacency lists.