a priority queue on a Fibonacci heap, a collection of trees heap ordered whose roots are in a circular list
More...
|
| | FibonacciHeapPriorityQueue (void)=default |
| | the empty heap
|
| | FibonacciHeapPriorityQueue (const A &alloc) |
| | the empty queue, the blocks of its nodes from alloc
|
| allocator_type | get_allocator (void) const |
| | the allocator of the blocks of the nodes
|
| | FibonacciHeapPriorityQueue (const FibonacciHeapPriorityQueue &)=delete |
| | no copy: the handles point into the heap
|
| FibonacciHeapPriorityQueue & | operator= (const FibonacciHeapPriorityQueue &)=delete |
| | no copy: the handles point into the heap
|
| | FibonacciHeapPriorityQueue (FibonacciHeapPriorityQueue &&other) noexcept |
| | the items of other, which is left empty; its handles stay valid here
|
| | ~FibonacciHeapPriorityQueue () |
| | the heap and its nodes destroyed
|
| bool | empty (void) const |
| | whether the heap has no item
|
| int | size (void) const |
| | the number of items, \( n \)
|
| const T & | min (void) const |
| | the item of smallest priority, the least of the roots
|
| const P & | minPriority (void) const |
| | the smallest priority
|
| Handle | insert (const T &x, const P &p) |
| | inserts \( x \) with the priority \( p \) and gives its handle, a tree of one node among the roots, in \( O( 1 ) \) time
|
| T | deleteMin (void) |
| | removes the item of smallest priority and returns it, in \( O( \log n ) \) amortized time; the handle of the item is no longer valid
|
| void | decrease (Handle x, const P &p) |
| | lowers to \( p \) the priority of the node \( x \), in \( O( 1 ) \) amortized time
|
| const_iterator | begin (void) const |
| | the first item of the view
|
| const_iterator | end (void) const |
| | the end of the view
|
| const_iterator | cbegin (void) const |
| | the same as begin()
|
| const_iterator | cend (void) const |
| | the same as end()
|
template<class T, std::totally_ordered P = int, class A = HugePageAllocator< T >>
class ADS::FibonacciHeapPriorityQueue< T, P, A >
a priority queue on a Fibonacci heap, a collection of trees heap ordered whose roots are in a circular list
template<class T, std::totally_ordered P = int, class A = HugePageAllocator< T >>
lowers to \( p \) the priority of the node \( x \), in \( O( 1 ) \) amortized time
In pseudocode (Fredman and Tarjan, beyond the book), the cascading cut keeping a node of degree \( k \) with at least \( F_{k+2} \) descendants:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{decrease}( x , p ) \cr
2 & \quad x.\mathit{priority} \gets p;\ y \gets x.\mathit{parent} \cr
3 & \quad \textbf{if}\ y \ne \textbf{nil}\ \textbf{and}
\ x.\mathit{priority} < y.\mathit{priority}\ \textbf{then} \cr
4 & \qquad \mathrm{cut}( x , y );\ z \gets y \cr
5 & \qquad \textbf{while}\ z.\mathit{parent} \ne \textbf{nil}
\ \textbf{do}\quad \triangleright\ \text{the cascading cut} \cr
6 & \qquad\quad \textbf{if not}\ z.\mathit{mark}\ \textbf{then}
\ z.\mathit{mark} \gets \textbf{true};\ \textbf{break} \cr
7 & \qquad\quad w \gets z.\mathit{parent};\ \mathrm{cut}( z , w
);\ z \gets w \cr
8 & \quad \textbf{if}\ x.\mathit{priority} <
\mathit{min}.\mathit{priority}\ \textbf{then}\ \mathit{min} \gets
x \cr
9 & \textbf{procedure}\ \mathrm{cut}( x , y ) \cr
10 & \quad \text{remove } x \text{ from the children of } y;
\ y.\mathit{degree} \gets y.\mathit{degree} - 1 \cr
11 & \quad x.\mathit{parent} \gets \textbf{nil};\ x.\mathit{mark}
\gets \textbf{false};\ \text{add } x \text{ to the list of the}
\text{ roots}
\end{array}}
\]
- Precondition
- \( x \) is a node of this heap and \( p \) is not larger than its priority
- Postcondition
- \( x \) has the priority \( p \)
template<class T, std::totally_ordered P = int, class A = HugePageAllocator< T >>
removes the item of smallest priority and returns it, in \( O( \log n ) \) amortized time; the handle of the item is no longer valid
In pseudocode (Fredman and Tarjan, beyond the book), \( A \) the roots by degree, the degrees below 64:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{deleteMin}() \cr
2 & \quad z \gets \mathit{min};\ n \gets n - 1 \cr
3 & \quad \text{splice the list of the children of } z \text{ into}
\text{ the list of the roots} \cr
4 & \quad \text{remove } z \text{ from the list of the roots} \cr
5 & \quad \textbf{if}\ \text{the list is empty }\ \textbf{then}
\ \mathit{min} \gets \textbf{nil}\ \textbf{else}
\ \mathrm{consolidate}() \cr
6 & \quad \textbf{return}\ z.\mathit{value} \cr
7 & \textbf{procedure}\ \mathrm{consolidate}() \cr
8 & \quad A[ 0 .. 63 ] \gets \textbf{nil} \cr
9 & \quad \textbf{foreach}\ \text{root } y\ \textbf{do} \cr
10 & \qquad y.\mathit{parent} \gets \textbf{nil};\ y.\mathit{mark}
\gets \textbf{false};\ d \gets y.\mathit{degree} \cr
11 & \qquad \textbf{while}\ A[ d ] \ne \textbf{nil}\ \textbf{do} \cr
12 & \qquad\quad z \gets A[ d ];\ A[ d ] \gets \textbf{nil} \cr
13 & \qquad\quad \textbf{if}\ z.\mathit{priority} <
y.\mathit{priority}\ \textbf{then}\ \text{swap } y \text{ and } z
\cr
14 & \qquad\quad \text{make } z \text{ a child of } y \text{, not}
\text{ marked};\ d \gets d + 1;\ y.\mathit{degree} \gets d \cr
15 & \qquad A[ d ] \gets y \cr
16 & \quad \text{the roots} \gets \text{the trees of } A
\text{ in a circular list};\ \mathit{min} \gets \text{the least}
\text{ of them}
\end{array}}
\]
- Precondition
- the heap is not empty
- Postcondition
- one item less, none of smaller priority than the one removed
template<class T, std::totally_ordered P = int, class A = HugePageAllocator< T >>
inserts \( x \) with the priority \( p \) and gives its handle, a tree of one node among the roots, in \( O( 1 ) \) time
In pseudocode (Fredman and Tarjan, beyond the book), \( \mathit{min}
\) the root of least priority:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{insert}( x , p ) \cr
2 & \quad z \gets \text{a node } ( p , x ) \text{ of degree 0, not}
\text{ marked};\ n \gets n + 1 \cr
3 & \quad \textbf{if}\ \mathit{min} = \textbf{nil}\ \textbf{then}
\ \mathit{min} \gets z \cr
4 & \quad \textbf{else} \cr
5 & \qquad \text{add } z \text{ to the list of the roots} \cr
6 & \qquad \textbf{if}\ z.\mathit{priority} <
\mathit{min}.\mathit{priority}\ \textbf{then}\ \mathit{min} \gets
z \cr
7 & \quad \textbf{return}\ z
\end{array}}
\]
- Postcondition
- one item more