38# include <gtest/gtest.h>
70 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
75 for (
int i = 10; i >= 1; --i)
154 auto node = heap.insert(42);
176 for (
int i = 100; i >= 1; --i)
185 for (
int i = 1; i <= 100; ++i)
194 for (
int i = 0; i < 10; ++i)
201 for (
int i = 0; i < 10; ++i)
209 std::string s =
"hello world";
272 int val = heap.extract_min();
292 std::vector<int>
input = {50, 20, 80, 10, 90, 30, 70, 40, 60, 100};
297 while (!heap.is_empty())
308 std::vector<int>
input = {5, 3, 5, 1, 3, 5, 1, 3};
313 while (!heap.is_empty())
328 heap.decrease_key(
nodes[0], 5);
337 heap.decrease_key(
nodes[1], 50);
346 heap.decrease_key(
nodes[0], 100);
352 auto node = heap.insert(50);
353 EXPECT_THROW(heap.decrease_key(node, 100), std::domain_error);
358 EXPECT_THROW(heap.decrease_key(
nullptr, 10), std::invalid_argument);
364 for (
int i = 1; i <= 20; ++i)
368 for (
int i = 0; i < 5; ++i)
372 auto node = heap.insert(100);
373 heap.decrease_key(node, 1);
381 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
382 for (
int i = 1; i <= 100; ++i)
383 nodes.push_back(heap.insert(i * 10));
386 for (
int i = 0; i < 20; ++i)
391 for (
size_t i = 50; i < 60 && i <
nodes.size(); ++i)
393 if (
nodes[i]->data > 5)
394 heap.decrease_key(
nodes[i],
static_cast<int>(i));
399 while (!heap.is_empty())
402 for (
size_t i = 1; i <
extracted.size(); ++i)
423 auto node = heap.insert(50);
424 auto result = heap.update_key(node, 30);
433 auto node = heap.insert(50);
436 auto result = heap.update_key(node, 80);
452 auto node = heap.insert(50);
453 auto result = heap.update_key(node, 50);
461 EXPECT_THROW(heap.update_key(
nullptr, 10), std::invalid_argument);
470 auto node = heap.insert(42);
471 heap.delete_node(node);
479 auto min_node = heap.insert(10);
482 heap.delete_node(min_node);
491 auto middle = heap.insert(20);
504 EXPECT_THROW(heap.delete_node(
nullptr), std::invalid_argument);
510 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
511 for (
int i = 1; i <= 50; ++i)
512 nodes.push_back(heap.insert(i));
515 for (
int i = 0; i < 10; ++i)
519 heap.delete_node(
nodes[30]);
520 heap.delete_node(
nodes[40]);
521 heap.delete_node(
nodes[25]);
525 while (!heap.is_empty())
532 for (
size_t i = 1; i <
extracted.size(); ++i)
541 (
void)heap.insert(10);
542 (
void)heap.insert(5);
543 (
void)heap.insert(20);
544 (
void)heap.insert(15);
545 (
void)heap.insert(3);
558 (
void)heap.insert(1);
565 auto min_node = heap.get_min_node();
566 heap.delete_node(min_node);
576 int first = heap.extract_min();
577 int second = heap.extract_min();
604 heap.delete_node(heap.get_min_node());
615 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
616 for (
int i = 1; i <= 20; ++i)
617 nodes.push_back(heap.insert(i));
620 std::vector<size_t>
indices(20);
622 std::random_device rd;
623 std::mt19937 g(rd());
628 heap.delete_node(
nodes[idx]);
692 while (!
h1.is_empty())
695 std::vector<int>
expected = {3, 5, 8, 10, 12, 15};
707 h1.merge(std::move(
h2));
717 for (
int i = 0; i < 1000; i += 2)
720 for (
int i = 1; i < 1000; i += 2)
728 while (!
h1.is_empty())
731 for (
int i = 0; i < 1000; ++i)
795 for (
int i = 0; i < 100; ++i)
823 static_assert(std::is_same_v<Fibonacci_Heap<int>::value_type,
int>);
824 static_assert(std::is_same_v<Fibonacci_Heap<std::string>::value_type, std::string>);
914 constexpr int N = 100000;
916 for (
int i =
N; i >= 1; --i)
926 constexpr int N = 10000;
928 for (
int i =
N; i >= 1; --i)
931 for (
int i = 1; i <=
N; ++i)
940 std::multiset<int> reference;
941 std::random_device rd;
942 std::mt19937
gen(42);
943 std::uniform_int_distribution<>
dis(1, 10000);
945 constexpr int N = 10000;
947 for (
int i = 0; i <
N; ++i)
951 if (op == 0 || reference.empty())
956 reference.insert(val);
962 int ref_min = *reference.begin();
963 reference.erase(reference.begin());
977 int ref_min = *reference.begin();
978 reference.erase(reference.begin());
986 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
987 constexpr int N = 5000;
989 for (
int i = 0; i <
N; ++i)
993 for (
int i = 0; i <
N / 4; ++i)
998 for (
size_t i =
N / 4; i <
N; ++i)
1020 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1021 constexpr int N = 1000;
1023 for (
int i = 0; i <
N; ++i)
1027 for (
int i = 0; i <
N; i += 2)
1038 for (
size_t i = 0; i <
extracted.size(); ++i)
1044 std::vector<Fibonacci_Heap<int>>
heaps(100);
1047 for (
int i = 0; i < 100; ++i)
1048 for (
int j = 0; j < 100; ++j)
1049 heaps[i].insert(i * 100 + j);
1052 for (
int i = 1; i < 100; ++i)
1058 int prev =
heaps[0].extract_min();
1059 while (!
heaps[0].is_empty())
1061 int curr =
heaps[0].extract_min();
1092 heap.
insert(std::numeric_limits<int>::max());
1094 heap.
insert(std::numeric_limits<int>::min());
1104 auto node = heap.
insert(42);
1120 for (
int i = 0; i < 100; ++i)
1123 heap.
insert(std::numeric_limits<int>::min() + i / 2);
1125 heap.
insert(std::numeric_limits<int>::max() - i / 2);
1143template <
typename T,
typename Compare>
1154 for (
size_t i = 1; i <
extracted.size(); ++i)
1166 std::random_device rd;
1167 std::mt19937
gen(42);
1168 std::uniform_int_distribution<>
dis(-10000, 10000);
1170 for (
int i = 0; i < 1000; ++i)
1179 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1181 for (
int i = 0; i < 100; ++i)
1185 for (
int i = 0; i < 20; ++i)
1189 for (
size_t i = 30; i < 50; ++i)
1198 std::random_device rd;
1199 std::mt19937
gen(42);
1200 std::uniform_int_distribution<>
dis(1, 1000);
1202 for (
int i = 0; i < 500; ++i)
1223 for (
int i = 0; i < 1000; ++i)
1233 for (
int i = 0; i < 1000; ++i)
1238 for (
int i = 0; i < 1000; ++i)
1245 constexpr int N = 1000000;
1247 auto start = std::chrono::high_resolution_clock::now();
1250 for (
int i =
N; i >= 1; --i)
1253 auto after_insert = std::chrono::high_resolution_clock::now();
1258 auto after_extract = std::chrono::high_resolution_clock::now();
1260 auto insert_time = std::chrono::duration_cast<std::chrono::milliseconds>(
1262 auto extract_time = std::chrono::duration_cast<std::chrono::milliseconds>(
1265 std::cout <<
"Insert " <<
N <<
" elements: " <<
insert_time <<
" ms\n";
1266 std::cout <<
"Extract " <<
N <<
" elements: " <<
extract_time <<
" ms\n";
1283 return distance <
other.distance;
1288 std::vector<Fibonacci_Heap<DistNode>::Node *>
handles(100,
nullptr);
1291 for (
int v = 0; v < 100; ++v)
1293 int dist = (v == 0) ? 0 : std::numeric_limits<int>::max();
1298 std::random_device rd;
1299 std::mt19937
gen(42);
1300 std::uniform_int_distribution<>
dist_gen(1, 100);
1302 while (!
pq.is_empty())
1311 for (
int i = 0; i < 3; ++i)
1315 if (
handles[v] !=
nullptr &&
handles[v]->data.distance > u.distance + 10)
1318 pq.decrease_key(
handles[v], {v, u.distance + 10});
1340 auto cmp = [](
int a,
int b) {
return a > b; };
1344 (
void)heap.insert(30);
1345 (
void)heap.insert(20);
1359 auto node = heap.
insert(50);
1377 for (
int i = 1; i <= 10; ++i)
1385 auto node = heap.
insert(1000);
1403 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1406 for (
int i = 0; i < 15; ++i)
1410 for (
int i = 0; i < 4; ++i)
1446 for (
int i = 1; i <= 10; ++i)
1453 auto node = heap.
insert(500);
1464 auto node = heap.
insert(100);
1501 constexpr int N = 10000;
1502 for (
int i =
N; i >= 1; --i)
1506 for (
int i = 0; i <
N / 2; ++i)
1513 for (
int i =
N / 2 + 1; i <=
N; ++i)
1539#pragma GCC diagnostic push
1540#pragma GCC diagnostic ignored "-Wself-move"
1541 heap = std::move(heap);
1542#pragma GCC diagnostic pop
1551 auto n1 = heap.
insert(10);
1552 auto n2 = heap.
insert(20);
1596 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1599 for (
int i = 1; i <= 100; ++i)
1603 for (
int i = 0; i < 30; ++i)
1609 for (
size_t i = 50; i < 70; ++i)
1611 if (
nodes[i]->data > key)
1630 ::testing::InitGoogleTest(&
argc,
argv);
bool operator<(const Time &l, const Time &r)
Implementation of a Fibonacci Heap priority queue.
void swap(Fibonacci_Heap &other) noexcept
Swaps contents with another heap.
const T & get_min() const
Returns the minimum element without removing it.
Node * get_min_node() const noexcept
Returns a pointer to the minimum node.
Compare key_comp() const
Returns the comparison functor.
void clear() noexcept(std::is_nothrow_destructible_v< T >)
Removes all elements from the heap.
Node * insert(const T &val)
Inserts a new element (copy).
void merge(Fibonacci_Heap &other)
Merges another heap into this one.
Node * emplace(Args &&... args)
Constructs and inserts an element in-place.
size_t size() const noexcept
Returns the number of elements in the heap.
T extract_min()
Extracts and returns the minimum element.
void delete_node(Node *x)
Deletes a specific node from the heap.
Node * update_key(Node *x, const T &k)
Updates the key of a node (increase or decrease).
void decrease_key(Node *x, const T &k)
Decreases the key of a node.
bool is_empty() const noexcept
Checks if the heap is empty.
Represents a point with rectangular coordinates in a 2D plane.
Minimal std::expected-style result type for C++20.
Fibonacci_Heap< int > heap
Fibonacci_Heap< int > heap
std::vector< Fibonacci_Heap< int >::Node * > nodes
bool verify_heap_property(Fibonacci_Heap< T, Compare > &heap)
TEST_F(FibonacciHeapTest, InsertSingleElement)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynArray< Graph::Node * > nodes
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
size_t size(Node *root) noexcept
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
Represents a node in the Fibonacci Heap.
bool operator<(const Point &other) const
Fibonacci Heap implementation.