ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::PairingHeap< T, P, Aux, A > Class Template Reference

a priority queue on a pairing heap, two-pass, or auxiliary two-pass if Aux More...

#include <ADS/priority/PairingHeap.h>

Classes

struct  Node
 a node of the heap More...

Public types

using Item = T
 the type of the items
using Priority = P
 the type of the priorities
using Handle = Node *
 what insert() gives
using allocator_type = A
 the type of the allocator

Public member functions

 PairingHeap (void)=default
 the empty heap
 PairingHeap (const A &alloc)
 the empty heap, the blocks of its nodes from alloc
allocator_type get_allocator (void) const
 the allocator of the blocks of the nodes
 PairingHeap (const PairingHeap &)=delete
 no copy: the handles point into the heap
PairingHeap & operator= (const PairingHeap &)=delete
 no copy: the handles point into the heap
 PairingHeap (PairingHeap &&other) noexcept
 the items of other, which is left empty; its handles stay valid here
 ~PairingHeap ()
 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
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 linked with the root, or put in the list if Aux
T deleteMin (void)
 removes the item of smallest priority and returns it, its children joined by two passes; 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 \), cut with its subtree and linked with the root, or put in the list if Aux
T erase (Handle x)
 removes the item of the node \( x \), wherever it is, and returns it: the subtrees of its children joined by two passes take its place; the handle is no longer valid
void increase (Handle x, const P &p)
 raises to \( p \) the priority of the node \( x \), which keeps its handle: the node leaves the heap as in erase() and comes back as in insert(), alone, since its children may now be smaller than it
void update (Handle x, const P &p)
 sets the priority of the node \( x \) to \( p \), by decrease() or by increase()

Detailed description

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
class ADS::PairingHeap< T, P, Aux, A >

a priority queue on a pairing heap, two-pass, or auxiliary two-pass if Aux

Member typedef documentation

◆ allocator_type

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
using ADS::PairingHeap< T, P, Aux, A >::allocator_type = A

the type of the allocator

◆ Handle

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
using ADS::PairingHeap< T, P, Aux, A >::Handle = Node *

what insert() gives

◆ Item

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
using ADS::PairingHeap< T, P, Aux, A >::Item = T

the type of the items

◆ Priority

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
using ADS::PairingHeap< T, P, Aux, A >::Priority = P

the type of the priorities

Constructor and destructor documentation

◆ PairingHeap() [1/4]

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
ADS::PairingHeap< T, P, Aux, A >::PairingHeap ( void )
default

the empty heap

◆ PairingHeap() [2/4]

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
ADS::PairingHeap< T, P, Aux, A >::PairingHeap ( const A & alloc)
inlineexplicit

the empty heap, the blocks of its nodes from alloc

◆ PairingHeap() [3/4]

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
ADS::PairingHeap< T, P, Aux, A >::PairingHeap ( const PairingHeap< T, P, Aux, A > & )
delete

no copy: the handles point into the heap

◆ PairingHeap() [4/4]

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
ADS::PairingHeap< T, P, Aux, A >::PairingHeap ( PairingHeap< T, P, Aux, A > && other)
noexcept

the items of other, which is left empty; its handles stay valid here

◆ ~PairingHeap()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
ADS::PairingHeap< T, P, Aux, A >::~PairingHeap ( )

the heap and its nodes destroyed

Member function documentation

◆ decrease()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
void ADS::PairingHeap< T, P, Aux, A >::decrease ( Handle x,
const P & p )

lowers to \( p \) the priority of the node \( x \), cut with its subtree and linked with the root, or put in the list if Aux

In pseudocode (Fredman, Sedgewick, Sleator and Tarjan, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{decrease}( x , p ) \cr 2 & \quad x.\mathit{priority} \gets p \cr 3 & \quad \textbf{if}\ x \ne \mathit{root}\ \textbf{and} \ ( x \text{ is not a leftmost child, or } p < x.\mathit{parent}.\mathit{priority} )\ \textbf{then} \cr 4 & \qquad \text{cut } x \text{ with its subtree} \cr 5 & \qquad \textbf{if}\ \mathit{Aux}\ \textbf{then}\ \text{put } x \text{ in the list after } \mathit{root} \cr 6 & \qquad \textbf{else}\ \mathit{root} \gets \mathrm{link}( \mathit{root} , x ) \cr 7 & \quad \mathit{min} \gets x\ \text{if } \mathit{Aux} \text{ and } p < \mathit{min}.\mathit{priority} \text{, else } \mathit{root} \text{ if not } \mathit{Aux} \end{array}} \]

Precondition
\( x \) is a node of this heap and \( p \) is not larger than its priority
Postcondition
\( x \) has the priority \( p \)

◆ deleteMin()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
T ADS::PairingHeap< T, P, Aux, A >::deleteMin ( void )

removes the item of smallest priority and returns it, its children joined by two passes; the handle of the item is no longer valid

In pseudocode (Fredman, Sedgewick, Sleator and Tarjan, beyond the book; the multipass pairing of the list of Stasko and Vitter if Aux):

\[\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 \textbf{if not}\ \mathit{Aux}\ \textbf{then} \ \mathit{root} \gets \mathrm{twoPass}( \text{the children of } z ) \cr 4 & \quad \textbf{else} \cr 5 & \qquad \textbf{if}\ z \ne \mathit{root}\ \textbf{then} \ \text{remove } z \text{ from the list} \cr 6 & \qquad \ell \gets \mathrm{multiPass}( \text{the list after } \mathit{root} ) \cr 7 & \qquad \mathit{root} \gets \text{the link of } \mathit{root} \text{ (if not } z \text{), } \mathrm{twoPass}( \text{the} \text{ children of } z ) \text{ and } \ell \cr 8 & \quad \mathit{min} \gets \mathit{root};\ \textbf{return} \ z.\mathit{value} \cr 9 & \textbf{function}\ \mathrm{twoPass}( t_1 , \ldots , t_k ) \cr 10 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ \lfloor k / 2 \rfloor\ \textbf{do}\ u_i \gets \mathrm{link}( t_{2 i - 1} , t_{2 i} )\quad \triangleright\ \text{from the left} \cr 11 & \quad \text{with } k \text{ odd, } u_{\lceil k / 2 \rceil} \gets t_k \cr 12 & \quad r \gets u_{\lceil k / 2 \rceil} \cr 13 & \quad \textbf{for}\ i \gets \lceil k / 2 \rceil - 1 \ \textbf{downto}\ 1\ \textbf{do}\ r \gets \mathrm{link}( r , u_i ) \quad \triangleright\ \text{from the right} \cr 14 & \quad \textbf{return}\ r \cr 15 & \textbf{function}\ \mathrm{multiPass}( t_1 , \ldots , t_k ) \cr 16 & \quad Q \gets \text{a queue of } t_1 , \ldots , t_k \cr 17 & \quad \textbf{while}\ |Q| > 1\ \textbf{do}\ \mathrm{enqueue}( Q , \mathrm{link}( \mathrm{dequeue}( Q ) , \mathrm{dequeue}( Q ) ) ) \cr 18 & \quad \textbf{return}\ \text{the tree of } Q \end{array}} \]

Precondition
the heap is not empty
Postcondition
one item less, none of smaller priority than the one removed

◆ empty()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
bool ADS::PairingHeap< T, P, Aux, A >::empty ( void ) const
inlinenodiscard

whether the heap has no item

◆ erase()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
T ADS::PairingHeap< T, P, Aux, A >::erase ( Handle x)

removes the item of the node \( x \), wherever it is, and returns it: the subtrees of its children joined by two passes take its place; the handle is no longer valid

In pseudocode (the delete of Fredman, Sedgewick, Sleator and Tarjan, beyond the book), \( \mathrm{twoPass}() \) that of deleteMin():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{erase}( x ) \cr 2 & \quad \textbf{if}\ x = \mathit{min}\ \textbf{then return} \ \mathrm{deleteMin}() \cr 3 & \quad t \gets \mathrm{twoPass}( \text{the children of } x ); \ n \gets n - 1 \cr 4 & \quad \textbf{if}\ x = \mathit{root}\ \textbf{then} \ \mathit{root} \gets t \text{, or the first tree of the list} \text{ if } t = \textbf{nil}\quad \triangleright\ \text{only if } \mathit{Aux} \cr 5 & \quad \textbf{else} \cr 6 & \qquad \text{cut } x \text{ from its list of siblings} \cr 7 & \qquad \textbf{if}\ t \ne \textbf{nil}\ \textbf{and} \ \mathit{Aux}\ \textbf{then}\ \text{put } t \text{ in the list } \text{after } \mathit{root} \cr 8 & \qquad \textbf{else if}\ t \ne \textbf{nil}\ \textbf{then} \ \mathit{root} \gets \mathrm{link}( \mathit{root} , t ); \ \mathit{min} \gets \mathit{root} \cr 9 & \quad \textbf{return}\ x.\mathit{value} \end{array}} \]

Precondition
\( x \) is a node of this heap
Postcondition
one item less

◆ get_allocator()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
allocator_type ADS::PairingHeap< T, P, Aux, A >::get_allocator ( void ) const
inlinenodiscard

the allocator of the blocks of the nodes

◆ increase()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
void ADS::PairingHeap< T, P, Aux, A >::increase ( Handle x,
const P & p )

raises to \( p \) the priority of the node \( x \), which keeps its handle: the node leaves the heap as in erase() and comes back as in insert(), alone, since its children may now be smaller than it

In pseudocode (beyond the book), \( \mathrm{detach}( x ) \) the steps 2 to 8 of erase() that leave the node out and keep it:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{increase}( x , p ) \cr 2 & \quad \mathrm{detach}( x );\ x.\mathit{priority} \gets p \cr 3 & \quad \text{link } x \text{ with } \mathit{root} \text{, or put} \text{ it in the list if } \mathit{Aux} \text{, as } \mathrm{insert}() \text{ does} \end{array}} \]

Precondition
\( x \) is a node of this heap and \( p \) is not smaller than its priority
Postcondition
\( x \) has the priority \( p \)

◆ insert()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
Handle ADS::PairingHeap< T, P, Aux, A >::insert ( const T & x,
const P & p )

inserts \( x \) with the priority \( p \) and gives its handle, a tree of one node linked with the root, or put in the list if Aux

In pseudocode (Fredman, Sedgewick, Sleator and Tarjan, beyond the book; the list of Stasko and Vitter if Aux):

\[\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{ with no child}; \ n \gets n + 1 \cr 3 & \quad \textbf{if}\ \mathit{root} = \textbf{nil}\ \textbf{then} \ \mathit{root} \gets z;\ \mathit{min} \gets z \cr 4 & \quad \textbf{else if}\ \mathit{Aux}\ \textbf{then} \cr 5 & \qquad \text{put } z \text{ in the list after } \mathit{root} \cr 6 & \qquad \textbf{if}\ p < \mathit{min}.\mathit{priority} \ \textbf{then}\ \mathit{min} \gets z \cr 7 & \quad \textbf{else}\ \mathit{root} \gets \mathrm{link}( \mathit{root} , z );\ \mathit{min} \gets \mathit{root} \cr 8 & \quad \textbf{return}\ z \cr 9 & \textbf{function}\ \mathrm{link}( a , b ) \cr 10 & \quad \textbf{if}\ b.\mathit{priority} < a.\mathit{priority} \ \textbf{then}\ \text{swap } a \text{ and } b \cr 11 & \quad \text{make } b \text{ the leftmost child of } a \cr 12 & \quad \textbf{return}\ a \end{array}} \]

Postcondition
one item more

◆ min()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
const T & ADS::PairingHeap< T, P, Aux, A >::min ( void ) const
nodiscard

the item of smallest priority

Precondition
the heap is not empty

◆ minPriority()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
const P & ADS::PairingHeap< T, P, Aux, A >::minPriority ( void ) const
nodiscard

the smallest priority

Precondition
the heap is not empty

◆ operator=()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
PairingHeap & ADS::PairingHeap< T, P, Aux, A >::operator= ( const PairingHeap< T, P, Aux, A > & )
delete

no copy: the handles point into the heap

◆ size()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
int ADS::PairingHeap< T, P, Aux, A >::size ( void ) const
inlinenodiscard

the number of items, \( n \)

◆ update()

template<class T, std::totally_ordered P = int, bool Aux = false, class A = HugePageAllocator< T >>
void ADS::PairingHeap< T, P, Aux, A >::update ( Handle x,
const P & p )

sets the priority of the node \( x \) to \( p \), by decrease() or by increase()

Precondition
\( x \) is a node of this heap
Postcondition
\( x \) has the priority \( p \)

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