a priority queue on a \( d \)-ary min-heap of bounded capacity, \( d \) the parameter D
More...
|
| | HeapPriorityQueue (int n) |
| | the empty queue of capacity \( n \) (the PriorityQueue( n ) of the book)
|
| | HeapPriorityQueue (const HeapPriorityQueue &)=delete |
| | no copy: the handles point into the queue
|
| HeapPriorityQueue & | operator= (const HeapPriorityQueue &)=delete |
| | no copy: the handles point into the queue
|
| | HeapPriorityQueue (HeapPriorityQueue &&other) noexcept |
| | the items of other, which is left empty; its handles stay valid here
|
| HeapPriorityQueue & | operator= (HeapPriorityQueue &&other) noexcept |
| | the items of other, which is left empty, in place of these; its handles stay valid here
|
| | ~HeapPriorityQueue () |
| | the queue and its items destroyed
|
| bool | empty (void) const |
| | whether the queue has no item
|
| int | size (void) const |
| | the number of items, \( n \)
|
| int | capacity (void) const |
| | the largest number of items, \( c \)
|
| const T & | min (void) const |
| | the item of smallest priority, the root \( H[ 0 ] \)
|
| const P & | minPriority (void) const |
| | the smallest priority
|
| T | deleteMin (void) |
| | removes the item of smallest priority and returns it, the last leaf moved to the root and down, in \( O( d \log_d n ) \) time; the handle of the item removed is no longer valid
|
| Handle | insert (const T &x, const P &p) |
| | inserts \( x \) with the priority \( p \) and gives its handle, a new leaf moved up, in \( O( \log_d n ) \) time
|
| void | decrease (Handle y, const P &p) |
| | lowers to \( p \) the priority of the item of the handle \( y \), which moves up, in \( O( \log_d n ) \) time
|
| std::vector< P > | priorities (void) const |
| | the heap: the priorities, by their place
|
| 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, int D = 2>
requires ( D >= 2 )
class ADS::HeapPriorityQueue< T, P, D >
a priority queue on a \( d \)-ary min-heap of bounded capacity, \( d \) the parameter D
template<class T, std::totally_ordered P = int, int D = 2>
lowers to \( p \) the priority of the item of the handle \( y \), which moves up, in \( O( \log_d n ) \) time
In pseudocode (section 10.1.3):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{decrease}( y , p ) \cr
2 & \quad y.\mathit{priority} \gets p;\ \mathrm{up}( y.\mathit{pos}
)\quad \triangleright\ \mathrm{swap} \text{ keeps } \mathit{pos}
\text{ up to date}
\end{array}}
\]
- Precondition
- \( y \) is an item of this queue and \( p \) is not larger than its priority
- Postcondition
- \( y \) has the priority \( p \), at its place
template<class T, std::totally_ordered P = int, int D = 2>
removes the item of smallest priority and returns it, the last leaf moved to the root and down, in \( O( d \log_d n ) \) time; the handle of the item removed is no longer valid
In pseudocode (section 10.1.3), on the places \( H[ 0 .. n - 1 ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{deleteMin}() \cr
2 & \quad \mathit{dim} \gets n - 1;\ \mathrm{swap}( H , 0 , \mathit{dim}
)\quad \triangleright\ \text{the last leaf at the root} \cr
3 & \quad \mathrm{minHeapRestore}( H , 0 , \mathit{dim} );\ x \gets
\text{the item of } H[ \mathit{dim} ];\ n \gets \mathit{dim} \cr
4 & \quad \textbf{return}\ x \cr
5 & \textbf{procedure}\ \mathrm{minHeapRestore}( H , i , \mathit{dim}
)\quad \triangleright\ \text{the } d \text{ children of } i
\text{ are } d i + 1 .. d i + d \cr
6 & \quad \textbf{loop} \cr
7 & \qquad \mathit{min} \gets \text{the place of least priority among }
i \text{ and its children below } \mathit{dim} \cr
8 & \qquad \textbf{if}\ \mathit{min} = i\ \textbf{then return} \cr
9 & \qquad \mathrm{swap}( H , i , \mathit{min} );\ i \gets
\mathit{min}\quad \triangleright\ \text{one level down}
\end{array}}
\]
- Precondition
- the queue is not empty
- Postcondition
- one item less, and none of smaller priority than the one removed
template<class T, std::totally_ordered P = int, int D = 2>
inserts \( x \) with the priority \( p \) and gives its handle, a new leaf moved up, in \( O( \log_d n ) \) time
In pseudocode (section 10.1.3), \( \mathrm{parent}( i ) \) the parent of the place \( i \) and \( \mathrm{swap}() \) keeping the place of each item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{insert}( x , p ) \cr
2 & \quad h \gets \text{a new item } ( p , x ) \text{ at the place } n;
\ H[ n ] \gets h;\ n \gets n + 1 \cr
3 & \quad \mathrm{up}( n - 1 );\ \textbf{return}\ h \cr
4 & \textbf{procedure}\ \mathrm{up}( i )\quad \triangleright
\ \text{the item in } i
\text{ climbs while its parent has a larger priority} \cr
5 & \quad \textbf{while}\ i > 0\ \textbf{and}\ H[ i ].\mathit{priority}
< H[ \mathrm{parent}( i ) ].\mathit{priority}\ \textbf{do} \cr
6 & \qquad \mathrm{swap}( H , i , \mathrm{parent}( i ) );\ i \gets
\mathrm{parent}( i )
\end{array}}
\]
- Precondition
- the queue is not full
- Postcondition
- one item more, the handle at its place