Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Fibonacci_Heap< T, Compare > Class Template Reference

Implementation of a Fibonacci Heap priority queue. More...

#include <tpl_fibonacci_heap.H>

Inheritance diagram for Aleph::Fibonacci_Heap< T, Compare >:
[legend]
Collaboration diagram for Aleph::Fibonacci_Heap< T, Compare >:
[legend]

Classes

struct  Node
 Represents a node in the Fibonacci Heap. More...
 

Public Types

using value_type = T
 Type alias for the element type.
 
using key_compare = Compare
 Type alias for the comparison functor.
 
using handle_type = Node *
 Type alias for node handles.
 

Public Member Functions

 Fibonacci_Heap (Compare compare=Compare()) noexcept(std::is_nothrow_copy_constructible_v< Compare >)
 Default constructor.
 
template<typename... Args>
 Fibonacci_Heap (std::in_place_t, Args &&... args) noexcept(std::is_nothrow_constructible_v< Compare, Args &&... >)
 Constructor with in-place comparator construction.
 
 ~Fibonacci_Heap ()
 Destructor.
 
 Fibonacci_Heap (const Fibonacci_Heap &)=delete
 Copy constructor is disabled (use merge or manual copy)
 
Fibonacci_Heap & operator= (const Fibonacci_Heap &)=delete
 Copy assignment is disabled (use merge or manual copy)
 
 Fibonacci_Heap (Fibonacci_Heap &&other) noexcept(std::is_nothrow_move_constructible_v< Compare >)
 Move constructor.
 
Fibonacci_Heap & operator= (Fibonacci_Heap &&other) noexcept(std::is_nothrow_move_assignable_v< Compare > and std::is_nothrow_destructible_v< T >)
 Move assignment operator.
 
void swap (Fibonacci_Heap &other) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swaps contents with another heap.
 
Node * insert (const T &val)
 Inserts a new element (copy).
 
Node * insert (T &&val)
 Inserts a new element (move).
 
template<typename... Args>
Node * emplace (Args &&... args)
 Constructs and inserts an element in-place.
 
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.
 
T extract_min ()
 Extracts and returns the minimum element.
 
void decrease_key (Node *x, const T &k)
 Decreases the key of a node.
 
void decrease_key (Node *x, T &&k)
 Decreases the key of a node (move version).
 
Node * update_key (Node *x, const T &k)
 Updates the key of a node (increase or decrease).
 
void delete_node (Node *x)
 Deletes a specific node from the heap.
 
void merge (Fibonacci_Heap &other)
 Merges another heap into this one.
 
void merge (Fibonacci_Heap &&other)
 Merges another heap into this one (rvalue version).
 
bool is_empty () const noexcept
 Checks if the heap is empty.
 
size_t size () const noexcept
 Returns the number of elements in the heap.
 
void clear () noexcept(std::is_nothrow_destructible_v< T >)
 Removes all elements from the heap.
 
bool empty () const noexcept
 
void pop ()
 
const T & top () const
 
Compare key_comp () const
 Returns the comparison functor.
 
Compare & get_compare () noexcept
 Returns the comparison functor that orders the heap.
 

Private Member Functions

void consolidate ()
 Consolidates the root list after extract_min.
 
void cut (Node *x, Node *y)
 Cuts a node from its parent and adds it to the root list.
 
void cascading_cut (Node *y)
 Performs cascading cut operation.
 
void take_root_out (Node *z) noexcept
 Takes a root out of the root list, after putting its children in it as roots.
 
void put_root_back (Node *z, Node *min) noexcept
 Undoes take_root_out() after a comparison threw.
 
void delete_all_nodes (Node *node)
 Deletes all the nodes of the trees in a circular list.
 
void add_to_root_list (Node *node)
 Adds a node to the root list.
 
Node * insert_new_node (Node *node)
 

Static Private Member Functions

static void link (Node *y, Node *x)
 Links two trees of the same degree.
 

Private Attributes

Node * min_node = nullptr
 Pointer to the current minimum root.
 
size_t num_nodes = 0
 Number of elements stored in the heap.
 
Compare cmp
 Comparison functor used to order nodes.
 
std::vector< Node * > consolidate_array_
 Reusable scratch array for Fibonacci_Heap::consolidate().
 

Static Private Attributes

static constexpr size_t MAX_DEGREE = 128
 Maximum degree tracked during consolidation.
 

Related Symbols

(Note that these are not member symbols.)

template<typename T , class Compare >
void swap (Fibonacci_Heap< T, Compare > &a, Fibonacci_Heap< T, Compare > &b) noexcept(noexcept(a.swap(b)))
 Swaps two Fibonacci heaps.
 

Detailed Description

template<typename T, class Compare = Aleph::less<T>>
requires StrictWeakOrder<Compare, T>
class Aleph::Fibonacci_Heap< T, Compare >

Implementation of a Fibonacci Heap priority queue.

A Fibonacci Heap is a collection of heap-ordered trees that supports a rich set of operations with excellent amortized time complexity. It is particularly well-suited for graph algorithms like Dijkstra's shortest path and Prim's minimum spanning tree where decrease-key operations are frequent.

Time Complexities (Amortized)

Operation Time Complexity
insert O(1)
get_min O(1)
extract_min O(log n)
decrease_key O(1)
delete_node O(log n)
merge O(1)
is_empty O(1)
size O(1)

Usage Example

auto n1 = heap.insert(10);
auto n2 = heap.insert(5);
auto n3 = heap.insert(15);
std::cout << heap.get_min(); // Outputs: 5
heap.decrease_key(n3, 3); // Decrease 15 to 3
std::cout << heap.get_min(); // Outputs: 3
int min = heap.extract_min(); // Returns 3, removes it
Implementation of a Fibonacci Heap priority queue.
const T & get_min() const
Returns the minimum element without removing it.
Node * insert(const T &val)
Inserts a new element (copy).
T extract_min()
Extracts and returns the minimum element.
void decrease_key(Node *x, const T &k)
Decreases the key of a node.
__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)
Definition gmpfrxx.h:4189

Implementation Notes

  • The heap maintains a circular doubly-linked list of root trees.
  • Each node can have multiple children organized as a circular list.
  • The "mark" field tracks whether a node has lost a child since becoming a non-root node (used for cascading cuts).
  • Copy operations are disabled; use move semantics or merge instead.
Template Parameters
TThe data type of elements stored in the heap.
CompareThe comparison functor (defaults to Aleph::less<T> for a min-heap). Use Aleph::greater<T> for a max-heap.
See also
BinHeap DynBinHeap

Definition at line 115 of file tpl_fibonacci_heap.H.

Member Typedef Documentation

◆ handle_type

template<typename T , class Compare = Aleph::less<T>>
using Aleph::Fibonacci_Heap< T, Compare >::handle_type = Node *

Type alias for node handles.

Definition at line 534 of file tpl_fibonacci_heap.H.

◆ key_compare

template<typename T , class Compare = Aleph::less<T>>
using Aleph::Fibonacci_Heap< T, Compare >::key_compare = Compare

Type alias for the comparison functor.

Definition at line 531 of file tpl_fibonacci_heap.H.

◆ value_type

template<typename T , class Compare = Aleph::less<T>>
using Aleph::Fibonacci_Heap< T, Compare >::value_type = T

Type alias for the element type.

Definition at line 528 of file tpl_fibonacci_heap.H.

Constructor & Destructor Documentation

◆ Fibonacci_Heap() [1/4]

template<typename T , class Compare = Aleph::less<T>>
Aleph::Fibonacci_Heap< T, Compare >::Fibonacci_Heap ( Compare  compare = Compare())
inlineexplicitnoexcept

Default constructor.

Creates an empty heap using a copy of compare to order keys.

Parameters
compareComparison functor (defaults to Compare()).

Definition at line 543 of file tpl_fibonacci_heap.H.

◆ Fibonacci_Heap() [2/4]

template<typename T , class Compare = Aleph::less<T>>
template<typename... Args>
Aleph::Fibonacci_Heap< T, Compare >::Fibonacci_Heap ( std::in_place_t  ,
Args &&...  args 
)
inlineexplicitnoexcept

Constructor with in-place comparator construction.

Allows constructing the comparator from arbitrary arguments. Use std::in_place as first argument to disambiguate.

Template Parameters
ArgsTypes of comparator constructor arguments
Parameters
argsArguments forwarded to Compare's constructor

Definition at line 561 of file tpl_fibonacci_heap.H.

◆ ~Fibonacci_Heap()

template<typename T , class Compare = Aleph::less<T>>
Aleph::Fibonacci_Heap< T, Compare >::~Fibonacci_Heap ( )
inline

Destructor.

Deallocates all nodes. Runs in O(n) time.

Definition at line 573 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::clear().

◆ Fibonacci_Heap() [3/4]

template<typename T , class Compare = Aleph::less<T>>
Aleph::Fibonacci_Heap< T, Compare >::Fibonacci_Heap ( const Fibonacci_Heap< T, Compare > &  )
delete

Copy constructor is disabled (use merge or manual copy)

◆ Fibonacci_Heap() [4/4]

template<typename T , class Compare = Aleph::less<T>>
Aleph::Fibonacci_Heap< T, Compare >::Fibonacci_Heap ( Fibonacci_Heap< T, Compare > &&  other)
inlinenoexcept

Move constructor.

Transfers ownership of all nodes from other to this heap. The source heap becomes empty.

Parameters
otherThe heap to move from

Definition at line 592 of file tpl_fibonacci_heap.H.

Member Function Documentation

◆ add_to_root_list()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::add_to_root_list ( Node *  node)
inlineprivate

Adds a node to the root list.

The keys are compared before the node enters the list, so that if the comparison throws the heap is as it was.

Parameters
nodeThe node to add (must not be in any list)

Definition at line 485 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::Node::data, Aleph::Fibonacci_Heap< T, Compare >::Node::left, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, and Aleph::Fibonacci_Heap< T, Compare >::Node::right.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::insert_new_node().

◆ cascading_cut()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::cascading_cut ( Node *  y)
inlineprivate

Performs cascading cut operation.

If y has lost a child before (mark == true), cut it from its parent and recursively check the parent. This maintains the amortized O(1) decrease_key complexity.

Parameters
yThe node to potentially cut

Definition at line 345 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cut(), Aleph::Fibonacci_Heap< T, Compare >::Node::mark, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, and y.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::decrease_key(), Aleph::Fibonacci_Heap< T, Compare >::decrease_key(), Aleph::Fibonacci_Heap< T, Compare >::delete_node(), Aleph::Fibonacci_Heap< T, Compare >::put_root_back(), and Aleph::Fibonacci_Heap< T, Compare >::update_key().

◆ clear()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::clear ( )
inlinenoexcept

◆ consolidate()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::consolidate ( )
inlineprivate

Consolidates the root list after extract_min.

Ensures no two roots have the same degree by linking trees of equal degree until all roots have distinct degrees.

It allocates nothing: extract_min() and delete_node() reserve consolidate_array_ before they change the heap. If the comparison throws, every tree is still in the root list and min_node is one of its roots, perhaps not the minimum.

Definition at line 231 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_, Aleph::Fibonacci_Heap< T, Compare >::Node::data, Aleph::Fibonacci_Heap< T, Compare >::Node::degree, Aleph::Fibonacci_Heap< T, Compare >::Node::left, Aleph::Fibonacci_Heap< T, Compare >::link(), Aleph::Fibonacci_Heap< T, Compare >::MAX_DEGREE, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, Aleph::Fibonacci_Heap< T, Compare >::Node::right, and y.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::delete_node(), Aleph::Fibonacci_Heap< T, Compare >::extract_min(), and Aleph::Fibonacci_Heap< T, Compare >::update_key().

◆ cut()

◆ decrease_key() [1/2]

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::decrease_key ( Node *  x,
const T &  k 
)
inline

Decreases the key of a node.

Decreases the key of node x to the new value k. If this violates the heap property with x's parent, x is cut and added to the root list, potentially triggering cascading cuts.

Parameters
xHandle to the node (returned by insert/emplace)
kThe new key value (must be <= current key)
Exceptions
std::invalid_argumentif x is nullptr
std::domain_errorif k > current key
anythingthat the comparison throws. Then the heap is as it was, and x has its old key.
anythingthat the assignment of the key throws. Then the heap is as it was but for the key of x, which is as the assignment left it: if that is not its old key, x may be out of order.
Note
Amortized O(1) time complexity

Definition at line 798 of file tpl_fibonacci_heap.H.

References ah_domain_error_if, ah_invalid_argument_if, Aleph::and, Aleph::Fibonacci_Heap< T, Compare >::cascading_cut(), Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::cut(), Aleph::Fibonacci_Heap< T, Compare >::Node::data, k, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, and y.

Referenced by demo_fibonacci_heap(), demo_performance_comparison(), ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::put_arc(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), and Aleph::Fibonacci_Heap< T, Compare >::update_key().

◆ decrease_key() [2/2]

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::decrease_key ( Node *  x,
T &&  k 
)
inline

Decreases the key of a node (move version).

Parameters
xHandle to the node
kThe new key value (moved into node)
Exceptions
std::invalid_argumentif x is nullptr
std::domain_errorif k > current key
anythingthat the comparison or the assignment of the key throw, as the copy version; if the comparison throws, k is not moved from.

Definition at line 833 of file tpl_fibonacci_heap.H.

References ah_domain_error_if, ah_invalid_argument_if, Aleph::and, Aleph::Fibonacci_Heap< T, Compare >::cascading_cut(), Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::cut(), Aleph::Fibonacci_Heap< T, Compare >::Node::data, k, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, and y.

◆ delete_all_nodes()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::delete_all_nodes ( Node *  node)
inlineprivate

Deletes all the nodes of the trees in a circular list.

Used by clear() for O(n) cleanup instead of O(n log n). Before deleting a node, it puts the children of the node in the list, right after it, so it needs no recursion: a tree can be as high as the heap is large.

Parameters
nodeA node of the list, or nullptr

Definition at line 453 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::Node::child, Aleph::Fibonacci_Heap< T, Compare >::Node::left, Aleph::next(), and Aleph::Fibonacci_Heap< T, Compare >::Node::right.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::clear().

◆ delete_node()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::delete_node ( Node *  x)
inline

Deletes a specific node from the heap.

Removes the node x from the heap regardless of its position.

Parameters
xHandle to the node to delete
Exceptions
std::invalid_argumentif x is nullptr
anythingthat the comparison throws. Then x is still in the heap, which has the elements that it had, the minimum among them, and every handle is valid; its trees may be linked otherwise.
Note
Amortized O(log n) time complexity
Warning
After this call, if it does not throw, x is invalid and must not be used.

Definition at line 955 of file tpl_fibonacci_heap.H.

References ah_invalid_argument_if, Aleph::Fibonacci_Heap< T, Compare >::cascading_cut(), Aleph::Fibonacci_Heap< T, Compare >::consolidate(), Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_, Aleph::Fibonacci_Heap< T, Compare >::cut(), Aleph::Fibonacci_Heap< T, Compare >::MAX_DEGREE, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::num_nodes, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, Aleph::Fibonacci_Heap< T, Compare >::put_root_back(), and Aleph::Fibonacci_Heap< T, Compare >::take_root_out().

Referenced by TEST(), TEST(), TEST(), TEST(), and TEST().

◆ emplace()

template<typename T , class Compare = Aleph::less<T>>
template<typename... Args>
Node * Aleph::Fibonacci_Heap< T, Compare >::emplace ( Args &&...  args)
inline

Constructs and inserts an element in-place.

Constructs the element directly in the node using the provided arguments, avoiding unnecessary copies or moves.

Template Parameters
ArgsTypes of constructor arguments
Parameters
argsArguments to forward to T's constructor
Returns
A handle to the created node
Exceptions
anythingthat the comparison throws. Then the heap is as it was.
Note
Amortized O(1) time complexity

Definition at line 704 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::insert_new_node().

Referenced by TEST(), and TEST_F().

◆ empty()

template<typename T , class Compare = Aleph::less<T>>
bool Aleph::Fibonacci_Heap< T, Compare >::empty ( ) const
inlinenoexcept

◆ extract_min()

template<typename T , class Compare = Aleph::less<T>>
T Aleph::Fibonacci_Heap< T, Compare >::extract_min ( )
inline

Extracts and returns the minimum element.

Removes the minimum element from the heap and returns it. The heap is restructured (consolidated) after removal.

Returns
The minimum element (moved out)
Exceptions
std::underflow_errorif the heap is empty
anythingthat the comparison throws. Then the heap has the elements that it had, the minimum among them, and every handle is valid; its trees may be linked otherwise.
Note
Amortized O(log n) time complexity

Definition at line 751 of file tpl_fibonacci_heap.H.

References ah_underflow_error_if, Aleph::Fibonacci_Heap< T, Compare >::consolidate(), Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_, Aleph::Fibonacci_Heap< T, Compare >::Node::data, Aleph::Fibonacci_Heap< T, Compare >::is_empty(), Aleph::Fibonacci_Heap< T, Compare >::MAX_DEGREE, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::num_nodes, Aleph::Fibonacci_Heap< T, Compare >::put_root_back(), and Aleph::Fibonacci_Heap< T, Compare >::take_root_out().

Referenced by demo_fibonacci_heap(), demo_max_heap(), demo_performance_comparison(), ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::get_min_arc(), Aleph::Fibonacci_Heap< T, Compare >::pop(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), and verify_heap_property().

◆ get_compare()

template<typename T , class Compare = Aleph::less<T>>
Compare & Aleph::Fibonacci_Heap< T, Compare >::get_compare ( )
inlinenoexcept

Returns the comparison functor that orders the heap.

Changing it while the heap has elements may leave the heap out of order.

Returns
Reference to the comparison functor

Definition at line 1129 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cmp.

Referenced by ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::get_distance(), ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::put_arc(), and TEST().

◆ get_min()

template<typename T , class Compare = Aleph::less<T>>
const T & Aleph::Fibonacci_Heap< T, Compare >::get_min ( ) const
inline

Returns the minimum element without removing it.

Returns
Const reference to the minimum element
Exceptions
std::underflow_errorif the heap is empty
Note
O(1) time complexity

Definition at line 717 of file tpl_fibonacci_heap.H.

References ah_underflow_error_if, Aleph::Fibonacci_Heap< T, Compare >::Node::data, Aleph::Fibonacci_Heap< T, Compare >::is_empty(), and Aleph::Fibonacci_Heap< T, Compare >::min_node.

Referenced by demo_fibonacci_heap(), demo_max_heap(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Aleph::Fibonacci_Heap< T, Compare >::top().

◆ get_min_node()

template<typename T , class Compare = Aleph::less<T>>
Node * Aleph::Fibonacci_Heap< T, Compare >::get_min_node ( ) const
inlinenoexcept

Returns a pointer to the minimum node.

Useful when you need the handle rather than just the value.

Returns
Pointer to the minimum node, or nullptr if empty
Note
O(1) time complexity

Definition at line 732 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::min_node.

Referenced by ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::empty(), ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::get_min_arc(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ insert() [1/2]

template<typename T , class Compare = Aleph::less<T>>
Node * Aleph::Fibonacci_Heap< T, Compare >::insert ( const T &  val)
inline

Inserts a new element (copy).

Creates a new node with a copy of val and adds it to the heap.

Parameters
valThe value to insert
Returns
A handle (pointer) to the created node for later operations
Exceptions
anythingthat the comparison throws. Then the heap is as it was.
Note
Amortized O(1) time complexity
The return value can be safely ignored if you don't need the handle for decrease_key or delete_node operations.

Definition at line 667 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::insert_new_node().

Referenced by demo_fibonacci_heap(), demo_max_heap(), demo_performance_comparison(), ArcFibonacciHeap< GT, Distance, Access_Heap_Node >::put_arc(), FibonacciHeapWithDataTest::SetUp(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ insert() [2/2]

template<typename T , class Compare = Aleph::less<T>>
Node * Aleph::Fibonacci_Heap< T, Compare >::insert ( T &&  val)
inline

Inserts a new element (move).

Creates a new node by moving val into it.

Parameters
valThe value to insert (will be moved)
Returns
A handle to the created node
Exceptions
anythingthat the comparison throws. Then the heap is as it was.
Note
Amortized O(1) time complexity

Definition at line 684 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::insert_new_node().

◆ insert_new_node()

◆ is_empty()

◆ key_comp()

template<typename T , class Compare = Aleph::less<T>>
Compare Aleph::Fibonacci_Heap< T, Compare >::key_comp ( ) const
inline

Returns the comparison functor.

Returns
Copy of the comparison functor

Definition at line 1116 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cmp.

Referenced by TEST().

◆ link()

template<typename T , class Compare = Aleph::less<T>>
static void Aleph::Fibonacci_Heap< T, Compare >::link ( Node *  y,
Node *  x 
)
inlinestaticprivate

Links two trees of the same degree.

Makes y a child of x. Assumes both are roots and x has the smaller key.

Parameters
yThe node to become a child
xThe node to become the parent

Definition at line 194 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::Node::child, Aleph::Fibonacci_Heap< T, Compare >::Node::degree, Aleph::Fibonacci_Heap< T, Compare >::Node::left, Aleph::Fibonacci_Heap< T, Compare >::Node::right, and y.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::consolidate().

◆ merge() [1/2]

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::merge ( Fibonacci_Heap< T, Compare > &&  other)
inline

Merges another heap into this one (rvalue version).

Parameters
otherTemporary heap whose contents will be transferred.
Note
This overload simply forwards to the lvalue reference version, enabling idioms such as merge(Fibonacci_Heap{...}).

Definition at line 1050 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::merge().

◆ merge() [2/2]

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::merge ( Fibonacci_Heap< T, Compare > &  other)
inline

Merges another heap into this one.

All nodes from other are transferred to this heap. The other heap becomes empty after this operation.

Parameters
otherThe heap to merge (will be emptied)
Exceptions
anythingthat the comparison throws. Then both heaps are as they were.
Note
O(1) time complexity
Warning
Both heaps must use compatible comparison functors.

Definition at line 1005 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::Node::data, Aleph::Fibonacci_Heap< T, Compare >::is_empty(), Aleph::Fibonacci_Heap< T, Compare >::Node::left, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::num_nodes, and Aleph::Fibonacci_Heap< T, Compare >::Node::right.

Referenced by demo_fibonacci_heap(), Aleph::Fibonacci_Heap< T, Compare >::merge(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ operator=() [1/2]

template<typename T , class Compare = Aleph::less<T>>
Fibonacci_Heap & Aleph::Fibonacci_Heap< T, Compare >::operator= ( const Fibonacci_Heap< T, Compare > &  )
delete

Copy assignment is disabled (use merge or manual copy)

◆ operator=() [2/2]

template<typename T , class Compare = Aleph::less<T>>
Fibonacci_Heap & Aleph::Fibonacci_Heap< T, Compare >::operator= ( Fibonacci_Heap< T, Compare > &&  other)
inlinenoexcept

Move assignment operator.

Clears this heap and transfers ownership from other.

Parameters
otherThe heap to move from
Returns
Reference to this heap
Exceptions
anythingthat the destructor of T, in clear(), or the move assignment of the comparison throws. If the comparison throws, this heap is empty and other keeps its elements.

Definition at line 614 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::clear(), Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_, Aleph::Fibonacci_Heap< T, Compare >::min_node, and Aleph::Fibonacci_Heap< T, Compare >::num_nodes.

◆ pop()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::pop ( )
inline

◆ put_root_back()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::put_root_back ( Node *  z,
Node *  min 
)
inlineprivatenoexcept

Undoes take_root_out() after a comparison threw.

Puts the node back in the root list, in which consolidate() leaves every tree when the comparison throws, and makes the minimum the node that was it before. A consolidation may have left that node under one of an equivalent key: then it is cut from it as decrease_key() cuts a node, so the one that loses it is marked, or cut too if it was. So the heap has the elements and the minimum that it had, though its trees may be others.

Parameters
zThe node that take_root_out() took out
minThe node that was the minimum, which may be z

Definition at line 422 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cascading_cut(), Aleph::Fibonacci_Heap< T, Compare >::cut(), Aleph::Fibonacci_Heap< T, Compare >::Node::left, min(), Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, and Aleph::Fibonacci_Heap< T, Compare >::Node::right.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::delete_node(), Aleph::Fibonacci_Heap< T, Compare >::extract_min(), and Aleph::Fibonacci_Heap< T, Compare >::update_key().

◆ size()

template<typename T , class Compare = Aleph::less<T>>
size_t Aleph::Fibonacci_Heap< T, Compare >::size ( ) const
inlinenoexcept

Returns the number of elements in the heap.

Returns
Number of elements
Note
O(1) time complexity

Definition at line 1074 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::num_nodes.

Referenced by demo_fibonacci_heap(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ swap()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::swap ( Fibonacci_Heap< T, Compare > &  other)
inlinenoexcept

Swaps contents with another heap.

Exchanges all nodes between this heap and other in O(1) time.

Parameters
otherThe heap to swap with
Exceptions
anythingthat the swap of the comparisons throws: the swap that is_nothrow_swappable looks at, the one of Compare if it has one, else std::swap. Then the nodes stay where they were.

Definition at line 643 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::num_nodes, and Aleph::Fibonacci_Heap< T, Compare >::swap().

Referenced by Aleph::Fibonacci_Heap< T, Compare >::swap(), TEST(), TEST(), TEST_F(), and TEST_F().

◆ take_root_out()

template<typename T , class Compare = Aleph::less<T>>
void Aleph::Fibonacci_Heap< T, Compare >::take_root_out ( Node *  z)
inlineprivatenoexcept

Takes a root out of the root list, after putting its children in it as roots.

min_node is left on any other root, or null if there is none. The node keeps its data and is left without children.

Parameters
zThe root to take out

Definition at line 372 of file tpl_fibonacci_heap.H.

References Aleph::Fibonacci_Heap< T, Compare >::Node::child, Aleph::Fibonacci_Heap< T, Compare >::Node::degree, Aleph::Fibonacci_Heap< T, Compare >::Node::left, Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, and Aleph::Fibonacci_Heap< T, Compare >::Node::right.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::delete_node(), Aleph::Fibonacci_Heap< T, Compare >::extract_min(), and Aleph::Fibonacci_Heap< T, Compare >::update_key().

◆ top()

template<typename T , class Compare = Aleph::less<T>>
const T & Aleph::Fibonacci_Heap< T, Compare >::top ( ) const
inline

◆ update_key()

template<typename T , class Compare = Aleph::less<T>>
Node * Aleph::Fibonacci_Heap< T, Compare >::update_key ( Node *  x,
const T &  k 
)
inline

Updates the key of a node (increase or decrease).

Unlike decrease_key, this method handles both increases and decreases. For increases, the node leaves its tree and goes back to the heap as a root with the new key, as if it were deleted and inserted again, but it is the same node: no node is allocated.

Parameters
xHandle to the node
kThe new key value
Returns
Handle to the node, which is x
Exceptions
std::invalid_argumentif x is nullptr
anythingthat the comparison throws. Then x has its old key, and the heap has the elements that it had, the minimum among them, and every handle is valid; if the key increases, its trees may be linked otherwise.
anythingthat the copy of the key throws. If the key increases, the copy is made before the heap changes, which is then as it was, and it is moved into the node at the end, or copied if the key has no move assignment: the above holds if that assignment does not throw. If the key decreases, see decrease_key().
std::bad_allocif the key increases and there is no memory for the array of the consolidation, which is reserved once, before the heap changes.
Note
O(1) amortized if decreasing, O(log n) amortized if increasing

Definition at line 885 of file tpl_fibonacci_heap.H.

References ah_invalid_argument_if, Aleph::Fibonacci_Heap< T, Compare >::cascading_cut(), Aleph::Fibonacci_Heap< T, Compare >::cmp, Aleph::Fibonacci_Heap< T, Compare >::consolidate(), Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_, Aleph::Fibonacci_Heap< T, Compare >::cut(), Aleph::Fibonacci_Heap< T, Compare >::Node::data, Aleph::Fibonacci_Heap< T, Compare >::decrease_key(), k, Aleph::Fibonacci_Heap< T, Compare >::MAX_DEGREE, min(), Aleph::Fibonacci_Heap< T, Compare >::min_node, Aleph::Fibonacci_Heap< T, Compare >::Node::parent, Aleph::Fibonacci_Heap< T, Compare >::put_root_back(), and Aleph::Fibonacci_Heap< T, Compare >::take_root_out().

Referenced by TEST(), TEST(), TEST(), and TEST().

Friends And Related Symbol Documentation

◆ swap()

template<typename T , class Compare >
void swap ( Fibonacci_Heap< T, Compare > &  a,
Fibonacci_Heap< T, Compare > &  b 
)
related

Swaps two Fibonacci heaps.

Definition at line 1141 of file tpl_fibonacci_heap.H.

Member Data Documentation

◆ cmp

◆ consolidate_array_

template<typename T , class Compare = Aleph::less<T>>
std::vector<Node *> Aleph::Fibonacci_Heap< T, Compare >::consolidate_array_
private

◆ MAX_DEGREE

template<typename T , class Compare = Aleph::less<T>>
constexpr size_t Aleph::Fibonacci_Heap< T, Compare >::MAX_DEGREE = 128
staticconstexprprivate

Maximum degree tracked during consolidation.

The degree of any Fibonacci heap root is bounded by log_phi(n). The value 128 comfortably covers heaps larger than 2^64 elements.

Definition at line 172 of file tpl_fibonacci_heap.H.

Referenced by Aleph::Fibonacci_Heap< T, Compare >::consolidate(), Aleph::Fibonacci_Heap< T, Compare >::delete_node(), Aleph::Fibonacci_Heap< T, Compare >::extract_min(), and Aleph::Fibonacci_Heap< T, Compare >::update_key().

◆ min_node

◆ num_nodes


The documentation for this class was generated from the following file: