|
| 1 | +# Bounded Priority queue |
| 2 | + |
| 3 | +A bounded priority queue is similar to a regular [priority queue](../Priority Queue/), except that there is a fixed upper bound on the number of elements that can be stored. When a new element is added to the queue while the queue is at capacity, the element with the highest priority value is ejected from the queue. |
| 4 | + |
| 5 | +## Example |
| 6 | + |
| 7 | +Suppose we have a bounded-priority-queue with maximum size 5 that has the following values and priorities: |
| 8 | + |
| 9 | +``` |
| 10 | +Value: [ A, B, C, D, E ] |
| 11 | +Priority: [ 0.1, 0.25, 1.33, 3.2, 4.6 ] |
| 12 | +``` |
| 13 | + |
| 14 | +Here, we consider the object with the lowest priority value to be the most important (so this is a *min-priority* queue). The larger the priority value, the less we care about the object. So `A` is more important than `B`, `B` is more important than `C`, and so on. |
| 15 | + |
| 16 | +Now we want to insert the element `F` with priority `0.4` into this bounded priority queue. Because the queue has maximum size 5, this will insert the element `F` but then evict the lowest-priority element (`E`), yielding the updated queue: |
| 17 | + |
| 18 | +``` |
| 19 | +Value: [ A, B, F, C, D ] |
| 20 | +Priority: [ 0.1, 0.25, 0.4, 1.33, 3.2 ] |
| 21 | +``` |
| 22 | + |
| 23 | +`F` is inserted between `B` and `C` because of its priority value. It's less important than `B` but more important than `C`. |
| 24 | + |
| 25 | +Suppose that we wish to insert the element `G` with priority 4.0 into this BPQ. Because `G`'s priority value is greater than the maximum-priority element in the queue, upon inserting `G` it will immediately be evicted. In other words, inserting an element into a BPQ with priority greater than the maximum-priority element of the BPQ has no effect. |
| 26 | + |
| 27 | +## Implementation |
| 28 | + |
| 29 | +While a [heap](../Heap/) may be a really simple implementation for a priority queue, a sorted [linked list](../Linked List/) allows for **O(k)** insertion and **O(1)** deletion, where **k** is the bounding number of elements. |
| 30 | + |
| 31 | +Here's how you could implement it in Swift: |
| 32 | + |
| 33 | +```swift |
| 34 | +public class BoundedPriorityQueue<T: Comparable> { |
| 35 | + private typealias Node = LinkedListNode<T> |
| 36 | + |
| 37 | + private(set) public var count = 0 |
| 38 | + private var head: Node? |
| 39 | + private var maxElements: Int |
| 40 | + |
| 41 | + public init(maxElements: Int) { |
| 42 | + self.maxElements = maxElements |
| 43 | + } |
| 44 | + |
| 45 | + public var isEmpty: Bool { |
| 46 | + return count == 0 |
| 47 | + } |
| 48 | + |
| 49 | + public func peek() -> T? { |
| 50 | + return head?.value |
| 51 | + } |
| 52 | +``` |
| 53 | + |
| 54 | +The `BoundedPriorityQueue` class contains a doubly linked list of `LinkedListNode` objects. Nothing special here yet. The fun stuff happens in the `enqueue()` method: |
| 55 | + |
| 56 | +```swift |
| 57 | + public func enqueue(value: T) { |
| 58 | + let newNode = Node(value: value) |
| 59 | + |
| 60 | + if head == nil { |
| 61 | + head = newNode |
| 62 | + count = 1 |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + var node = head |
| 67 | + if count == maxElements && newNode.value > node!.value { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + while (node!.next != nil) && (newNode.value < node!.value) { |
| 72 | + node = node!.next |
| 73 | + } |
| 74 | + |
| 75 | + if newNode.value < node!.value { |
| 76 | + newNode.next = node!.next |
| 77 | + newNode.previous = node |
| 78 | + |
| 79 | + if newNode.next != nil { /* TAIL */ |
| 80 | + newNode.next!.previous = newNode |
| 81 | + } |
| 82 | + node!.next = newNode |
| 83 | + } else { |
| 84 | + newNode.previous = node!.previous |
| 85 | + newNode.next = node |
| 86 | + if node!.previous == nil { /* HEAD */ |
| 87 | + head = newNode |
| 88 | + } else { |
| 89 | + node!.previous!.next = newNode |
| 90 | + } |
| 91 | + node!.previous = newNode |
| 92 | + } |
| 93 | + |
| 94 | + if count == maxElements { |
| 95 | + dequeue() |
| 96 | + } |
| 97 | + count += 1 |
| 98 | + } |
| 99 | +``` |
| 100 | + |
| 101 | +We first check if the queue already has the maximum number of elements. If so, and the new priority value is greater than the `head` element's priority value, then there is no room for this new element and we return without inserting it. |
| 102 | + |
| 103 | +If the new value is acceptable, then we search through the list to find the proper insertion ___location and update the `next` and `previous` pointers. |
| 104 | + |
| 105 | +Lastly, if the queue has now reached the maximum number of elements, then we `dequeue()` the one with the largest priority value. |
| 106 | + |
| 107 | +By keeping the most important element at the front of the list, it makes dequeueing very easy: |
| 108 | + |
| 109 | +```swift |
| 110 | + public func dequeue() -> T? { |
| 111 | + if count == 0 { |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + let retVal = head!.value |
| 116 | + |
| 117 | + if count == 1 { |
| 118 | + head = nil |
| 119 | + } else { |
| 120 | + head = head!.next |
| 121 | + head!.previous = nil |
| 122 | + } |
| 123 | + |
| 124 | + count -= 1 |
| 125 | + return retVal |
| 126 | + } |
| 127 | +``` |
| 128 | + |
| 129 | +This simply removes the `head` element from the list and returns it. |
| 130 | + |
| 131 | +*Written for Swift Algorithm Club by John Gill and Matthijs Hollemans* |
0 commit comments