You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Priority queue traits and high performance d-ary heap implementations.
8
9
9
-
> **no-std**: This crate supports **no-std**; however, *std* is added as a default feature. Please include with **no-default-features** for no-std use cases: `cargo add orx-priority-queue --no-default-features`.
10
+
> **no-std**: This crate supports **no-std**; however, _std_ is added as a default feature. Please include with **no-default-features** for no-std use cases: `cargo add orx-priority-queue --no-default-features`.
10
11
11
12
## A. Priority Queue Traits
12
13
@@ -19,8 +20,9 @@ See [DecreaseKey](https://github.com/orxfun/orx-priority-queue/blob/main/docs/De
19
20
## B. d-ary Heap Implementations
20
21
21
22
d-ary implementations are generalizations of the binary heap; i.e., binary heap is a special case where `D=2`. It is advantageous to have a parametrized d; as for instance, in the benchmarks defined here, `D=4` outperforms `D=2`.
22
-
* With a large d: number of per level comparisons increases while the tree depth becomes smaller.
23
-
* With a small d: each level requires fewer comparisons while the tree with the same number of nodes is deeper.
23
+
24
+
- With a large d: number of per level comparisons increases while the tree depth becomes smaller.
25
+
- With a small d: each level requires fewer comparisons while the tree with the same number of nodes is deeper.
24
26
25
27
Further, three categories of d-ary heap implementations are introduced.
26
28
@@ -32,8 +34,8 @@ This is the basic d-ary heap implementing `PriorityQueue`. It is the default cho
32
34
33
35
This is a d-ary heap paired up with a positions array and implements `PriorityQueueDecKey`.
34
36
35
-
* It requires the nodes to implement `HasIndex` trait which is nothing but `fn index(&self) -> usize`. Note that `usize`, `u64`, etc., already implements `HasIndex`.
36
-
* Further, it requires to know the maximum index that is expected to enter the queue. In other words, candidates are expected to come from a closed set.
37
+
- It requires the nodes to implement `HasIndex` trait which is nothing but `fn index(&self) -> usize`. Note that `usize`, `u64`, etc., already implements `HasIndex`.
38
+
- Further, it requires to know the maximum index that is expected to enter the queue. In other words, candidates are expected to come from a closed set.
37
39
38
40
Once these conditions are satisfied, it **performs significantly faster** than the alternative decrease key queues.
39
41
@@ -51,30 +53,29 @@ This is the most general decrease-key queue that provides the open-set flexibili
51
53
52
54
In addition, queue implementations are provided in this crate for the following external data structures:
53
55
54
-
*`std::collections::BinaryHeap<(N, K)>` implements only `PriorityQueue<N, K>`,
55
-
*`priority_queue:PriorityQueue<N, K>` implements both `PriorityQueue<N, K>` and `PriorityQueueDecKey<N, K>`
56
-
* requires `--features impl_priority_queue`
57
-
* or `--features impl_all`
56
+
-`std::collections::BinaryHeap<(N, K)>` implements only `PriorityQueue<N, K>`,
57
+
-`priority_queue:PriorityQueue<N, K>` implements both `PriorityQueue<N, K>` and `PriorityQueueDecKey<N, K>`
58
+
- requires `--features impl_priority_queue`
59
+
- or `--features impl_all`
58
60
59
61
This allows to use all the queue implementations interchangeably and pick the one fitting best to the use case.
60
62
61
63
### Performance & Benchmarks
62
64
63
-
*You may find the details of the benchmarks at [benches](https://github.com/orxfun/orx-priority-queue/blob/main/benches) folder.*
65
+
_You may find the details of the benchmarks at [benches](https://github.com/orxfun/orx-priority-queue/blob/main/benches) folder._
The table above summarizes the benchmark results of basic operations on basic queues, and queues allowing decrease key operations.
68
70
69
-
* In the first benchmark, we repeatedly call `push` and `pop` operations on a queue while maintaining an average length of 100000:
70
-
* We observe that `BinaryHeap` (`DaryHeap<_, _, 2>`) performs almost the same as the standard binary heap.
71
-
* Experiments on different values of d shows that `QuaternaryHeap` (D=4) outperforms both binary heaps.
72
-
* Further increasing D to 8 does not improve performance.
73
-
* Finally, we repeat the experiments with `BinaryHeap` and `QuaternaryHeap` using the specialized [`push_then_pop`](https://docs.rs/orx-priority-queue/latest/orx_priority_queue/trait.PriorityQueue.html#tymethod.push_then_pop) operation. Note that this operation further doubles the performance, and hence, should be used whenever it fits the use case.
74
-
* In the second benchmark, we add [`decrease_key_or_push`](https://docs.rs/orx-priority-queue/latest/orx_priority_queue/trait.PriorityQueueDecKey.html#method.decrease_key_or_push) calls to the operations. Standard binary heap is excluded since it cannot implement `PriorityQueueDecKey`.
75
-
* We observe that `DaryHeapOfIndices` significantly outperforms other decrease key queues.
76
-
* Among `BinaryHeapOfIndices` and `QuaternaryHeapOfIndices`, the latter with D=4 again performs better.
77
-
71
+
- In the first benchmark, we repeatedly call `push` and `pop` operations on a queue while maintaining an average length of 100000:
72
+
- We observe that `BinaryHeap` (`DaryHeap<_, _, 2>`) performs almost the same as the standard binary heap.
73
+
- Experiments on different values of d shows that `QuaternaryHeap` (D=4) outperforms both binary heaps.
74
+
- Further increasing D to 8 does not improve performance.
75
+
- Finally, we repeat the experiments with `BinaryHeap` and `QuaternaryHeap` using the specialized [`push_then_pop`](https://docs.rs/orx-priority-queue/latest/orx_priority_queue/trait.PriorityQueue.html#tymethod.push_then_pop) operation. Note that this operation further doubles the performance, and hence, should be used whenever it fits the use case.
76
+
- In the second benchmark, we add [`decrease_key_or_push`](https://docs.rs/orx-priority-queue/latest/orx_priority_queue/trait.PriorityQueueDecKey.html#method.decrease_key_or_push) calls to the operations. Standard binary heap is excluded since it cannot implement `PriorityQueueDecKey`.
77
+
- We observe that `DaryHeapOfIndices` significantly outperforms other decrease key queues.
78
+
- Among `BinaryHeapOfIndices` and `QuaternaryHeapOfIndices`, the latter with D=4 again performs better.
You may see below two implementations of the Dijkstra's shortest path algorithm: one using a `PriorityQueue` and the other with a `PriorityQueueDecKey`. Please note the following:
177
178
178
-
* Priority queue traits allow us to be generic over queues. Therefore, we are able to implement the algorithm once that works for any queue implementation.
179
-
* The second implementation with a decrease key queue pushes some of the bookkeeping to the queue, and arguably leads to a cleaner algorithm implementation.
179
+
- Priority queue traits allow us to be generic over queues. Therefore, we are able to implement the algorithm once that works for any queue implementation.
180
+
- The second implementation with a decrease key queue pushes some of the bookkeeping to the queue, and arguably leads to a cleaner algorithm implementation.
0 commit comments