38# include <gtest/gtest.h>
69 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
74 for (
int i = 10; i >= 1; --i)
153 auto node = heap.insert(42);
175 for (
int i = 100; i >= 1; --i)
184 for (
int i = 1; i <= 100; ++i)
193 for (
int i = 0; i < 10; ++i)
200 for (
int i = 0; i < 10; ++i)
208 std::string s =
"hello world";
271 int val = heap.extract_min();
291 std::vector<int>
input = {50, 20, 80, 10, 90, 30, 70, 40, 60, 100};
296 while (!heap.is_empty())
307 std::vector<int>
input = {5, 3, 5, 1, 3, 5, 1, 3};
312 while (!heap.is_empty())
327 heap.decrease_key(
nodes[0], 5);
336 heap.decrease_key(
nodes[1], 50);
345 heap.decrease_key(
nodes[0], 100);
351 auto node = heap.insert(50);
352 EXPECT_THROW(heap.decrease_key(node, 100), std::domain_error);
357 EXPECT_THROW(heap.decrease_key(
nullptr, 10), std::invalid_argument);
363 for (
int i = 1; i <= 20; ++i)
367 for (
int i = 0; i < 5; ++i)
371 auto node = heap.insert(100);
372 heap.decrease_key(node, 1);
380 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
381 for (
int i = 1; i <= 100; ++i)
382 nodes.push_back(heap.insert(i * 10));
385 for (
int i = 0; i < 20; ++i)
390 for (
size_t i = 50; i < 60 && i <
nodes.size(); ++i)
392 if (
nodes[i]->data > 5)
393 heap.decrease_key(
nodes[i],
static_cast<int>(i));
398 while (!heap.is_empty())
422 auto node = heap.insert(50);
423 auto result = heap.update_key(node, 30);
432 auto node = heap.insert(50);
435 auto result = heap.update_key(node, 80);
451 auto node = heap.insert(50);
452 auto result = heap.update_key(node, 50);
460 EXPECT_THROW(heap.update_key(
nullptr, 10), std::invalid_argument);
469 auto node = heap.insert(42);
470 heap.delete_node(node);
478 auto min_node = heap.insert(10);
481 heap.delete_node(min_node);
503 EXPECT_THROW(heap.delete_node(
nullptr), std::invalid_argument);
509 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
510 for (
int i = 1; i <= 50; ++i)
511 nodes.push_back(heap.insert(i));
514 for (
int i = 0; i < 10; ++i)
518 heap.delete_node(
nodes[30]);
519 heap.delete_node(
nodes[40]);
520 heap.delete_node(
nodes[25]);
524 while (!heap.is_empty())
541 (
void)heap.insert(5);
542 (
void)heap.insert(20);
543 (
void)heap.insert(15);
544 (
void)heap.insert(3);
557 (
void)heap.insert(1);
564 auto min_node = heap.get_min_node();
565 heap.delete_node(min_node);
575 int first = heap.extract_min();
576 int second = heap.extract_min();
603 heap.delete_node(heap.get_min_node());
614 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
615 for (
int i = 1; i <= 20; ++i)
616 nodes.push_back(heap.insert(i));
619 std::vector<size_t>
indices(20);
621 std::random_device rd;
622 std::mt19937 g(rd());
627 heap.delete_node(
nodes[idx]);
694 std::vector<int>
expected = {3, 5, 8, 10, 12, 15};
706 h1.merge(std::move(
h2));
716 for (
int i = 0; i < 1000; i += 2)
719 for (
int i = 1; i < 1000; i += 2)
730 for (
int i = 0; i < 1000; ++i)
794 for (
int i = 0; i < 100; ++i)
822 static_assert(std::is_same_v<Fibonacci_Heap<int>::value_type,
int>);
823 static_assert(std::is_same_v<Fibonacci_Heap<std::string>::value_type, std::string>);
913 constexpr int N = 100000;
915 for (
int i =
N; i >= 1; --i)
925 constexpr int N = 10000;
927 for (
int i =
N; i >= 1; --i)
930 for (
int i = 1; i <=
N; ++i)
939 std::multiset<int> reference;
940 std::random_device rd;
941 std::mt19937
gen(42);
942 std::uniform_int_distribution<>
dis(1, 10000);
944 constexpr int N = 10000;
946 for (
int i = 0; i <
N; ++i)
950 if (op == 0 || reference.empty())
955 reference.insert(val);
962 reference.erase(reference.begin());
977 reference.erase(reference.begin());
985 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
986 constexpr int N = 5000;
988 for (
int i = 0; i <
N; ++i)
992 for (
int i = 0; i <
N / 4; ++i)
997 for (
size_t i =
N / 4; i <
N; ++i)
999 if (
nodes[i]->data > counter)
1019 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1020 constexpr int N = 1000;
1022 for (
int i = 0; i <
N; ++i)
1026 for (
int i = 0; i <
N; i += 2)
1043 std::vector<Fibonacci_Heap<int>>
heaps(100);
1046 for (
int i = 0; i < 100; ++i)
1047 for (
int j = 0; j < 100; ++j)
1048 heaps[i].insert(i * 100 + j);
1051 for (
int i = 1; i < 100; ++i)
1057 int prev =
heaps[0].extract_min();
1058 while (!
heaps[0].is_empty())
1060 int curr =
heaps[0].extract_min();
1091 heap.
insert(std::numeric_limits<int>::max());
1093 heap.
insert(std::numeric_limits<int>::min());
1103 auto node = heap.
insert(42);
1119 for (
int i = 0; i < 100; ++i)
1122 heap.
insert(std::numeric_limits<int>::min() + i / 2);
1124 heap.
insert(std::numeric_limits<int>::max() - i / 2);
1142template <
typename T,
typename Compare>
1165 std::random_device rd;
1166 std::mt19937
gen(42);
1167 std::uniform_int_distribution<>
dis(-10000, 10000);
1169 for (
int i = 0; i < 1000; ++i)
1178 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1180 for (
int i = 0; i < 100; ++i)
1184 for (
int i = 0; i < 20; ++i)
1188 for (
size_t i = 30; i < 50; ++i)
1197 std::random_device rd;
1198 std::mt19937
gen(42);
1199 std::uniform_int_distribution<>
dis(1, 1000);
1201 for (
int i = 0; i < 500; ++i)
1222 for (
int i = 0; i < 1000; ++i)
1232 for (
int i = 0; i < 1000; ++i)
1237 for (
int i = 0; i < 1000; ++i)
1244 constexpr int N = 1000000;
1246 auto start = std::chrono::high_resolution_clock::now();
1249 for (
int i =
N; i >= 1; --i)
1252 auto after_insert = std::chrono::high_resolution_clock::now();
1257 auto after_extract = std::chrono::high_resolution_clock::now();
1259 auto insert_time = std::chrono::duration_cast<std::chrono::milliseconds>(
1261 auto extract_time = std::chrono::duration_cast<std::chrono::milliseconds>(
1264 std::cout <<
"Insert " <<
N <<
" elements: " <<
insert_time <<
" ms\n";
1265 std::cout <<
"Extract " <<
N <<
" elements: " <<
extract_time <<
" ms\n";
1282 return distance <
other.distance;
1287 std::vector<Fibonacci_Heap<DistNode>::Node *>
handles(100,
nullptr);
1290 for (
int v = 0; v < 100; ++v)
1292 int dist = (v == 0) ? 0 : std::numeric_limits<int>::max();
1297 std::random_device rd;
1298 std::mt19937
gen(42);
1299 std::uniform_int_distribution<>
dist_gen(1, 100);
1310 for (
int i = 0; i < 3; ++i)
1314 if (
handles[v] !=
nullptr &&
handles[v]->data.distance > u.distance + 10)
1317 pq.decrease_key(
handles[v], {v, u.distance + 10});
1339 auto cmp = [](
int a,
int b) {
return a > b; };
1343 (
void)heap.insert(30);
1344 (
void)heap.insert(20);
1358 auto node = heap.
insert(50);
1376 for (
int i = 1; i <= 10; ++i)
1384 auto node = heap.
insert(1000);
1402 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1405 for (
int i = 0; i < 15; ++i)
1409 for (
int i = 0; i < 4; ++i)
1445 for (
int i = 1; i <= 10; ++i)
1452 auto node = heap.
insert(500);
1463 auto node = heap.
insert(100);
1500 constexpr int N = 10000;
1501 for (
int i =
N; i >= 1; --i)
1505 for (
int i = 0; i <
N / 2; ++i)
1512 for (
int i =
N / 2 + 1; i <=
N; ++i)
1538#pragma GCC diagnostic push
1539#pragma GCC diagnostic ignored "-Wself-move"
1540 heap = std::move(heap);
1541#pragma GCC diagnostic pop
1550 auto n1 = heap.
insert(10);
1551 auto n2 = heap.
insert(20);
1595 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1598 for (
int i = 1; i <= 100; ++i)
1602 for (
int i = 0; i < 30; ++i)
1608 for (
size_t i = 50; i < 70; ++i)
1610 if (
nodes[i]->data > key)
1629 ::testing::InitGoogleTest(&
argc,
argv);
bool operator<(const Time &l, const Time &r)
T & insert(const T &item)
Insert a new item by copy.
DynList & swap(DynList &l) noexcept
Swap this with l.
Implementation of a Fibonacci Heap priority queue.
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.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
Fibonacci_Heap< int > heap
Fibonacci_Heap< int > heap
std::vector< Fibonacci_Heap< int >::Node * > nodes
void emplace(Args &&... args)
Appends a new element into the container by constructing it in-place with the given args.
Rectangular point in the plane.
bool operator<(const Point &other) const
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
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
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Represents a node in the Fibonacci Heap.
Fibonacci Heap implementation.