ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
HeapPriorityQueue.h File Reference

The PRIORITYQUEUE [see PriorityQueue.h] on a heap. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/memory/ObjectPool.h"
#include "ADS/priority/PriorityQueue.h"
#include <concepts>
#include <cstddef>
#include <memory>
#include <utility>
#include <vector>
#include "ADS/priority/impl/HeapPriorityQueue.tpp"

Go to the source code of this file.

Classes

class  ADS::HeapPriorityQueue< T, P, D >
 a priority queue on a \( d \)-ary min-heap of bounded capacity, \( d \) the parameter D More...
struct  ADS::HeapPriorityQueue< T, P, D >::PriorityItem
 the PRIORITYITEM of the book: priority, item, place in the vector More...

Namespaces

namespace  ADS

Detailed description

The PRIORITYQUEUE [see PriorityQueue.h] on a heap.

HeapPriorityQueue is the heap of section 10.1.3, a vector \( H[ 0 .. n - 1 ] \) of capacity \( c \) holding a \( d \)-ary tree that is full but for the rightmost leaves of the last level, each node not smaller than its parent: the children of the place \( i \) are \( d i + 1 , \ldots , d i + d \) and its parent is \( \lfloor ( i - 1 ) / d \rfloor \) (with \( d = 2 \), \( l( i ) = 2 i + 1 \), \( r( i ) = 2 i + 2 \) and \( p( i ) = \lfloor ( i - 1 ) / 2 \rfloor \)). insert() and decrease() move the item up, deleteMin() moves the last leaf down from the root [see minHeapRestore()], in \( O( \log_d n ) \) and \( O( d \log_d n ) \) time, the tree having \( \lceil \log_d ( ( d - 1 ) n + 1 ) \rceil \) levels; each item knows its place, kept by swap(), and so the handle finds it (exercise 10.6 for \( d > 2 \)). Beyond the book, the items come from an ObjectPool [see ObjectPool.h] of the queue, which takes back the item of deleteMin() for the next insert(), so that an item costs no call to the allocator and the items in use stay close in memory.

The contracts [see Contract.h]: min(), minPriority() and deleteMin() need a queue that is not empty, insert() one that is not full, and decrease() a handle of an item of this queue and a priority not larger than its own; the invariant, checked at level 3 in \( O( n ) \) time, is the order of the heap together with the places that the items remember. On a heap of more than 1024 items only the root and the last item are checked, so that a long sequence of operations at the level 3 stays linear. The operations are defined in HeapPriorityQueue.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 10.1, https://algoritmicamente.it/materiali/libro/capitoli/cap10.pdf
J. W. J. Williams, Algorithm 232: Heapsort, Communications of the ACM 7(6), 347-348, 1964, doi:10.1145/512274.3734138
D. B. Johnson, Priority queues with update and finding minimum spanning trees, Information Processing Letters 4(3), 53-57, 1975, doi:10.1016/0020-0190(75)90001-0
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa