orx-priority-queue
Priority queue traits, d-ary heap implementations having binary heap as a special case.
Traits
This crate defines two priority queue traits for (node, key) pairs with the following features:
- [
PriorityQueue<N, K>
]: providing basic priority queue functionalities. - [
PriorityQueueDecKey<N, K>
]: adds super powers which is achieved by being able to locate positions of nodes that already exists on the heap.
Separating more advanced PriorityQueueDecKey
from the basic queue is due to the fact that additional functionalities are often made available through usage of additional memory.
Benefits of PriorityQueueDecKey
Decrease-key, and related operations, are critical for certain algorithms where the key of a particular node is evaluated multiple times. Without the ability to update keys of nodes on the heap, space complexity of correspoinding algorithms increases exponentially.
Consider Dijkstra's shortest-path algorithm for instance. Space complexity of the algorithm would be O(n^2) with a PriorityQueue
where n is the number of nodes on the graph. This is due to the fact that label of each node might be evaluated n-1 times and consequently each node can be pushed to the queue n-1 times. As also noted in std::collections::BinaryHeap
documentation, this implementation isn't memory-efficient as it may leave duplicate nodes in the queue.
On the other hand, using a PriorityQueueDecKey
, space complexity of the algorithm will be kept as O(n): each node will enter the queue at most once; consequent evaluations of its label will be handled by decrease key operation.
Furthermore, the additional functionalities simplify the algorithm implementation pushing some of the complexity to the data structure. This becomes clear when the following shortest_path
implementation is compared to the corresponding std::collections::BinaryHeap
example. Note that it is almost a drop-dead substitution while providing a better space complexity, generic dary-heap options and a cleaner algorithm implementation.
use *;
// Each node is represented as a `usize`, for a shorter implementation.
// Dijkstra's shortest path algorithm.
// Start at `start` and use `dist` to track the current shortest distance
// to each node. This implementation isn't memory-efficient as it may leave duplicate
// nodes in the queue. It also uses `usize::MAX` as a sentinel value,
// for a simpler implementation.
// This is the directed graph we're going to use.
// The node numbers correspond to the different states,
// and the edge weights symbolize the cost of moving
// from one node to another.
// Note that the edges are one-way.
//
// 7
// +-----------------+
// | |
// v 1 2 | 2
// 0 -----> 1 -----> 3 ---> 4
// | ^ ^ ^
// | | 1 | |
// | | | 3 | 1
// +------> 2 -------+ |
// 10 | |
// +---------------+
//
// The graph is represented as an adjacency list where each index,
// corresponding to a node value, has a list of outgoing edges.
// Chosen for its efficiency.
let graph = vec!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Implementations
d-ary heap
The core d-ary heap is implemented thanks to const generics. Three structs are created from this core struct:
- [
DaryHeap<N, K, const D: usize>
] which implementsPriorityQueue<N, K>
to be preferred when the additional features are not required. - [
DaryHeapWithMap<N, K, const D: usize>
] whereN: Hash + Equal
implementsPriorityQueueDecKey<N, K>
. It is a combination of the d-ary heap and a hash-map to track positions of nodes. This might be considered as the default way to extend the heap to enable additional funcitonalities without requiring a linear search. - [
DaryHeapOfIndices<N, K, const D: usize>
] whereN: HasIndex
implementsPriorityQueueDecKey<N, K>
. This variant is and alternative to the hash-map implementation and is particularly useful in algorithms where nodes to be enqueued are sampled from a closed set with known elements and the size of the queue is likely to get close to total number of candidates.
Special traversal for d=2: binary-heap
const generics further allows to use special arithmetics for the special case where d=2; i.e., when d-ary heap is the binary heap. In particular, one addition/subtraction is avoided during the traversal through the tree.
However, overall performance of the queues depends on the use case, ratio of push an decrease-key operations, etc. Benchmarks will follow.
Example
use *;
clear
use *;
// d of the d-ary heap
const D: usize = 4;
test_priority_queue;
test_priority_queue;
test_priority_queue;
test_priority_queue_deckey;
test_priority_queue_deckey;
License
This library is licensed under MIT license. See LICENSE for details.