Priority queue
You are encouraged to solve this task according to the task description, using any language you may know.
A priority queue is somewhat similar to a queue, with an important distinction: each item is added to a priority queue with a priority level, and will be later removed from the queue with the highest priority element first. That is, the items are (conceptually) stored in the queue in priority order instead of in insertion order.
Task: Create a priority queue. The queue must support at least two operations:
- Insertion. An element is added to the queue with a priority (a numeric value).
- Top item removal. Deletes the element or one of the elements with the current top priority and return it.
Optionally, other operations may be defined, such as peeking (find what current top priority/top element is), merging (combining two priority queues into one), etc.
To test your implementation, insert a number of elements into the queue, each with some random priority. Then dequeue them sequentially; now the elements should be sorted by priority. You can use the following task/priority items as input data:
Priority Task 3 Clear drains 4 Feed cat 5 Make tea 1 Solve RC tasks 2 Tax return
The implementation should try to be efficient. A typical implementation has O(log n) insertion and extraction time, where n is the number of items in the queue. You may choose to impose certain limits such as small range of allowed priority levels, limited capacity, etc. If so, discuss the reasons behind it.
Ada
Ada 2012 includes container classes for priority queues.
<lang Ada>with Ada.Containers.Synchronized_Queue_Interfaces; with Ada.Containers.Unbounded_Priority_Queues; with Ada.Strings.Unbounded;
procedure Priority_Queues is
use Ada.Containers; use Ada.Strings.Unbounded; type Queue_Element is record Priority : Natural; Content : Unbounded_String; end record; function Get_Priority (Element : Queue_Element) return Natural is begin return Element.Priority; end Get_Priority; function Before (Left, Right : Natural) return Boolean is begin return Left > Right; end Before; package String_Queues is new Synchronized_Queue_Interfaces (Element_Type => Queue_Element); package String_Priority_Queues is new Unbounded_Priority_Queues (Queue_Interfaces => String_Queues, Queue_Priority => Natural);
My_Queue : String_Priority_Queues.Queue;
begin
My_Queue.Enqueue (New_Item => (Priority => 3, Content => To_Unbounded_String ("Clear drains"))); My_Queue.Enqueue (New_Item => (Priority => 4, Content => To_Unbounded_String ("Feed cat"))); My_Queue.Enqueue (New_Item => (Priority => 5, Content => To_Unbounded_String ("Make tea"))); My_Queue.Enqueue (New_Item => (Priority => 1, Content => To_Unbounded_String ("Solve RC tasks"))); My_Queue.Enqueue (New_Item => (Priority => 2, Content => To_Unbounded_String ("Tax return")));
declare Element : Queue_Element; begin while My_Queue.Current_Use > 0 loop My_Queue.Dequeue (Element => Element); Ada.Text_IO.Put_Line (Natural'Image (Element.Priority) & " => " & To_String (Element.Content)); end loop; end;
end Priority_Queues;</lang>
output:
Axiom
Axiom already has a heap domain for ordered sets. We define a domain for ordered key-entry pairs and then define a priority queue using the heap domain over the pairs: <lang Axiom>)abbrev Domain ORDKE OrderedKeyEntry OrderedKeyEntry(Key:OrderedSet,Entry:SetCategory): Exports == Implementation where
Exports == OrderedSet with construct: (Key,Entry) -> % elt: (%,"key") -> Key elt: (%,"entry") -> Entry Implementation == add Rep := Record(k:Key,e:Entry) x,y: % construct(a,b) == construct(a,b)$Rep @ % elt(x,"key"):Key == (x@Rep).k elt(x,"entry"):Entry == (x@Rep).e x < y == x.key < y.key x = y == x.key = y.key hash x == hash(x.key) if Entry has CoercibleTo OutputForm then coerce(x):OutputForm == bracket [(x.key)::OutputForm,(x.entry)::OutputForm]
)abbrev Domain PRIORITY PriorityQueue S ==> OrderedKeyEntry(Key,Entry) PriorityQueue(Key:OrderedSet,Entry:SetCategory): Exports == Implementation where
Exports == PriorityQueueAggregate S with heap : List S -> % setelt: (%,Key,Entry) -> Entry Implementation == Heap(S) add setelt(x:%,key:Key,entry:Entry) == insert!(construct(key,entry)$S,x) entry</lang>For an example:<lang Axiom>pq := empty()$PriorityQueue(Integer,String)
pq(3):="Clear drains"; pq(4):="Feed cat"; pq(5):="Make tea"; pq(1):="Solve RC tasks"; pq(2):="Tax return"; [extract!(pq) for i in 1..#pq]</lang>Output:<lang Axiom>
[[5,"Make tea"], [4,"Feed cat"], [3,"Clear drains"], [2,"Tax return"], [1,"Solve RC tasks"]] Type: List(OrderedKeyEntry(Integer,String))</lang>
C
Using a dynamic array as a binary heap. Stores integer priority and a void data pointer. There's no limit on heap size besides integer overflow, although a very large heap will cause a lot of page faults. Supports insert, extraction, peeking at top element, merging and clearing. <lang c>#include <stdio.h>
- include <stdlib.h>
typedef struct { void * data; int pri; } q_elem_t; typedef struct { q_elem_t *buf; int n, alloc; } pri_queue_t, *pri_queue;
- define priq_purge(q) (q)->n = 1
- define priq_size(q) ((q)->n - 1)
/* first element in array not used to simplify indices */ pri_queue priq_new(int size) { if (size < 4) size = 4;
pri_queue q = malloc(sizeof(pri_queue_t)); q->buf = malloc(sizeof(q_elem_t) * size); q->alloc = size; q->n = 1;
return q; }
void priq_push(pri_queue q, void *data, int pri) { q_elem_t *b; int n, m;
if (q->n >= q->alloc) { q->alloc *= 2; b = q->buf = realloc(q->buf, sizeof(q_elem_t) * q->alloc); } else b = q->buf;
n = q->n++; /* append at end, then up heap */ while ((m = n / 2) && pri < b[m].pri) { b[n] = b[m]; n = m; } b[n].data = data; b[n].pri = pri; }
/* remove top item. returns 0 if empty. *pri can be null. */ void * priq_pop(pri_queue q, int *pri) { void *out; if (q->n == 1) return 0;
q_elem_t *b = q->buf;
out = b[1].data; if (pri) *pri = b[1].pri;
/* pull last item to top, then down heap. */ --q->n;
int n = 1, m; while ((m = n * 2) < q->n) { if (m + 1 < q->n && b[m].pri > b[m + 1].pri) m++;
if (b[q->n].pri <= b[m].pri) break; b[n] = b[m]; n = m; }
b[n] = b[q->n]; if (q->n < q->alloc / 2 && q->n >= 16) q->buf = realloc(q->buf, (q->alloc /= 2) * sizeof(b[0]));
return out; }
/* get the top element without removing it from queue */ void* priq_top(pri_queue q, int *pri) { if (q->n == 1) return 0; if (pri) *pri = q->buf[1].pri; return q->buf[1].data; }
/* this is O(n log n), but probably not the best */ void priq_combine(pri_queue q, pri_queue q2) { int i; q_elem_t *e = q2->buf + 1;
for (i = q2->n - 1; i >= 1; i--, e++) priq_push(q, e->data, e->pri); priq_purge(q2); }
int main() { int i, p; const char *c, *tasks[] ={ "Clear drains", "Feed cat", "Make tea", "Solve RC tasks", "Tax return" }; int pri[] = { 3, 4, 5, 1, 2 };
/* make two queues */ pri_queue q = priq_new(0), q2 = priq_new(0);
/* push all 5 tasks into q */ for (i = 0; i < 5; i++) priq_push(q, tasks[i], pri[i]);
/* pop them and print one by one */ while ((c = priq_pop(q, &p))) printf("%d: %s\n", p, c);
/* put a million random tasks in each queue */ for (i = 0; i < 1 << 20; i++) { p = rand() / ( RAND_MAX / 5 ); priq_push(q, tasks[p], pri[p]);
p = rand() / ( RAND_MAX / 5 ); priq_push(q2, tasks[p], pri[p]); }
printf("\nq has %d items, q2 has %d items\n", priq_size(q), priq_size(q2));
/* merge q2 into q; q2 is empty */ priq_combine(q, q2); printf("After merge, q has %d items, q2 has %d items\n", priq_size(q), priq_size(q2));
/* pop q until it's empty */ for (i = 0; (c = priq_pop(q, 0)); i++); printf("Popped %d items out of q\n", i);
return 0; }</lang>output<lang>1: Solve RC tasks 2: Tax return 3: Clear drains 4: Feed cat 5: Make tea
q has 1048576 items, q2 has 1048576 items After merge, q has 2097152 items, q2 has 0 items Popped 2097152 items out of q</lang>
C++
The C++ standard library contains the std::priority_queue
opaque data structure. It implements a max-heap.
<lang cpp>#include <iostream>
- include <string>
- include <queue>
- include <utility>
int main() {
std::priority_queue<std::pair<int, std::string> > pq; pq.push(std::make_pair(3, "Clear drains")); pq.push(std::make_pair(4, "Feed cat")); pq.push(std::make_pair(5, "Make tea")); pq.push(std::make_pair(1, "Solve RC tasks")); pq.push(std::make_pair(2, "Tax return"));
while (!pq.empty()) { std::cout << pq.top().first << ", " << pq.top().second << std::endl; pq.pop(); }
return 0;
}</lang>
output:
5, Make tea 4, Feed cat 3, Clear drains 2, Tax return 1, Solve RC tasks
Alternately, you can use a pre-existing container of yours and use the heap operations to manipulate it:
<lang cpp>#include <iostream>
- include <string>
- include <vector>
- include <algorithm>
- include <utility>
int main() {
std::vector<std::pair<int, std::string> > pq; pq.push_back(std::make_pair(3, "Clear drains")); pq.push_back(std::make_pair(4, "Feed cat")); pq.push_back(std::make_pair(5, "Make tea")); pq.push_back(std::make_pair(1, "Solve RC tasks"));
// heapify std::make_heap(pq.begin(), pq.end());
// enqueue pq.push_back(std::make_pair(2, "Tax return")); std::push_heap(pq.begin(), pq.end());
while (!pq.empty()) { // peek std::cout << pq[0].first << ", " << pq[0].second << std::endl; // dequeue std::pop_heap(pq.begin(), pq.end()); pq.pop_back(); }
return 0;
}</lang>
output:
5, Make tea 4, Feed cat 3, Clear drains 2, Tax return 1, Solve RC tasks
C#
<lang C#> using System;
namespace PriorityQueue {
class Program { static void Main(string[] args) { PriorityQueue PQ = new PriorityQueue(); PQ.push(3, "Clear drains"); PQ.push(4, "Feed cat"); PQ.push(5, "Make tea"); PQ.push(1, "Solve RC tasks"); PQ.push(2, "Tax return");
while (!PQ.Empty) { var Val = PQ.pop(); Console.WriteLine(Val[0] + " : " + Val[1]); } Console.ReadKey(); } }
class PriorityQueue { private System.Collections.SortedList PseudoQueue;
public bool Empty { get { return PseudoQueue.Count == 0; } }
public PriorityQueue() { PseudoQueue = new System.Collections.SortedList(); }
public void push(object Priority, object Value) { PseudoQueue.Add(Priority, Value); }
public object[] pop() { object[] ReturnValue = { null, null }; if (PseudoQueue.Count > 0) { ReturnValue[0] = PseudoQueue.GetKey(0); ReturnValue[1] = PseudoQueue.GetByIndex(0);
PseudoQueue.RemoveAt(0); } return ReturnValue; } }
} </lang>
CoffeeScript
<lang coffeescript> PriorityQueue = ->
# Use closure style for object creation (so no "new" required). # Private variables are toward top. h = [] better = (a, b) -> h[a].priority < h[b].priority swap = (a, b) -> [h[a], h[b]] = [h[b], h[a]] sift_down = -> max = h.length n = 0 while n < max c1 = 2*n + 1 c2 = c1 + 1 best = n best = c1 if c1 < max and better(c1, best) best = c2 if c2 < max and better(c2, best) return if best == n swap n, best n = best sift_up = -> n = h.length - 1 while n > 0 parent = Math.floor((n-1) / 2) return if better parent, n swap n, parent n = parent # now return the public interface, which is an object that only # has functions on it self = size: -> h.length
push: (priority, value) -> elem = priority: priority value: value h.push elem sift_up() pop: -> throw Error("cannot pop from empty queue") if h.length == 0 value = h[0].value last = h.pop() if h.length > 0 h[0] = last sift_down() value
- test
do ->
pq = PriorityQueue() pq.push 3, "Clear drains" pq.push 4, "Feed cat" pq.push 5, "Make tea" pq.push 1, "Solve RC tasks" pq.push 2, "Tax return"
while pq.size() > 0 console.log pq.pop() # test high performance for n in [1..100000] priority = Math.random() pq.push priority, priority v = pq.pop() console.log "First random element was #{v}" while pq.size() > 0 new_v = pq.pop() throw Error "Queue broken" if new_v < v v = new_v console.log "Final random element was #{v}"
</lang>
output
<lang> > coffee priority_queue.coffee Solve RC tasks Tax return Clear drains Feed cat Make tea First random element was 0.00002744467929005623 Final random element was 0.9999718656763434 </lang>
D
<lang d>import std.stdio, std.container, std.array, std.typecons;
void main() {
alias tuple T; auto heap = heapify([T(3, "Clear drains"), T(4, "Feed cat"), T(5, "Make tea"), T(1, "Solve RC tasks"), T(2, "Tax return")]);
while (!heap.empty) { writeln(heap.front); heap.removeFront(); }
}</lang>
- Output:
Tuple!(int,string)(5, "Make tea") Tuple!(int,string)(4, "Feed cat") Tuple!(int,string)(3, "Clear drains") Tuple!(int,string)(2, "Tax return") Tuple!(int,string)(1, "Solve RC tasks")
Factor
Factor has priority queues implemented in the library: documentation is available at http://docs.factorcode.org/content/article-heaps.html (or by typing "heaps" help interactively in the listener). <lang factor><min-heap> [ {
{ 3 "Clear drains" } { 4 "Feed cat" } { 5 "Make tea" } { 1 "Solve RC tasks" } { 2 "Tax return" } } swap heap-push-all
] [
[ print ] slurp-heap
] bi</lang>
output: <lang factor>Solve RC tasks Tax return Clear drains Feed cat Make tea</lang>
Fortran
<lang Fortran>module priority_queue_mod implicit none
type node
character (len=100) :: task integer :: priority
end type
type queue
type(node), allocatable :: buf(:) integer :: n = 0
contains
procedure :: top procedure :: enqueue procedure :: siftdown
end type
contains
subroutine siftdown(this, a)
class (queue) :: this integer :: a, parent, child associate (x => this%buf) parent = a do while(parent*2 <= this%n) child = parent*2 if (child + 1 <= this%n) then if (x(child+1)%priority > x(child)%priority ) then child = child +1 end if end if if (x(parent)%priority < x(child)%priority) then x([child, parent]) = x([parent, child]) parent = child else exit end if end do end associate
end subroutine
function top(this) result (res)
class(queue) :: this type(node) :: res res = this%buf(1) this%buf(1) = this%buf(this%n) this%n = this%n - 1 call this%siftdown(1)
end function
subroutine enqueue(this, priority, task)
class(queue), intent(inout) :: this integer :: priority character(len=*) :: task type(node) :: x type(node), allocatable :: tmp(:) integer :: i x%priority = priority x%task = task this%n = this%n +1 if (.not.allocated(this%buf)) allocate(this%buf(1)) if (size(this%buf)<this%n) then allocate(tmp(2*size(this%buf))) tmp(1:this%n-1) = this%buf call move_alloc(tmp, this%buf) end if this%buf(this%n) = x i = this%n do i = i / 2 if (i==0) exit call this%siftdown(i) end do
end subroutine end module
program main
use priority_queue_mod
type (queue) :: q type (node) :: x
call q%enqueue(3, "Clear drains") call q%enqueue(4, "Feed cat") call q%enqueue(5, "Make Tea") call q%enqueue(1, "Solve RC tasks") call q%enqueue(2, "Tax return")
do while (q%n >0) x = q%top() print "(g0,a,a)", x%priority, " -> ", trim(x%task) end do
end program
! Output: ! 5 -> Make Tea ! 4 -> Feed cat ! 3 -> Clear drains ! 2 -> Tax return ! 1 -> Solve RC tasks </lang>
Go
Go's standard library contains the container/heap
package, which which provides operations to operate as a heap any data structure that contains the Push
, Pop
, Len
, Less
, and Swap
methods.
<lang go>package main
import (
"fmt" "container/heap"
)
type Task struct {
priority int name string
}
type TaskPQ []Task
func (self TaskPQ) Len() int { return len(self) } func (self TaskPQ) Less(i, j int) bool {
return self[i].priority < self[j].priority
} func (self TaskPQ) Swap(i, j int) { self[i], self[j] = self[j], self[i] } func (self *TaskPQ) Push(x interface{}) { *self = append(*self, x.(Task)) } func (self *TaskPQ) Pop() (popped interface{}) {
popped = (*self)[len(*self)-1] *self = (*self)[:len(*self)-1] return
}
func main() {
pq := &TaskPQ{{3, "Clear drains"}, {4, "Feed cat"}, {5, "Make tea"}, {1, "Solve RC tasks"}}
// heapify heap.Init(pq)
// enqueue heap.Push(pq, Task{2, "Tax return"})
for pq.Len() != 0 { // dequeue fmt.Println(heap.Pop(pq)) }
}</lang>
output:
{1 Solve RC tasks} {2 Tax return} {3 Clear drains} {4 Feed cat} {5 Make tea}
Haskell
One of the best Haskell implementations of priority queues (of which there are many) is pqueue, which implements a binomial heap. <lang haskell>import Data.PQueue.Prio.Min
main = print (toList (fromList [(3, "Clear drains"),(4, "Feed cat"),(5, "Make tea"),(1, "Solve RC tasks"), (2, "Tax return")]))</lang>
Alternatively, a homemade min heap implementation: <lang haskell>data MinHeap a = Nil | MinHeap { v::a, cnt::Int, l::MinHeap a, r::MinHeap a } deriving (Show, Eq)
hPush :: (Ord a) => a -> MinHeap a -> MinHeap a hPush x Nil = MinHeap {v = x, cnt = 1, l = Nil, r = Nil} hPush x h = if x < vv -- insert element, try to keep the tree balanced then if hLength (l h) <= hLength (r h) then MinHeap { v=x, cnt=cc, l=hPush vv ll, r=rr } else MinHeap { v=x, cnt=cc, l=ll, r=hPush vv rr } else if hLength (l h) <= hLength (r h) then MinHeap { v=vv, cnt=cc, l=hPush x ll, r=rr } else MinHeap { v=vv, cnt=cc, l=ll, r=hPush x rr } where (vv, cc, ll, rr) = (v h, 1 + cnt h, l h, r h)
hPop :: (Ord a) => MinHeap a -> (a, MinHeap a) hPop h = (v h, pq) where -- just pop, heed not the tree balance pq | l h == Nil = r h | r h == Nil = l h | v (l h) <= v (r h) = let (vv,hh) = hPop (l h) in MinHeap {v = vv, cnt = hLength hh + hLength (r h), l = hh, r = r h} | otherwise = let (vv,hh) = hPop (r h) in MinHeap {v = vv, cnt = hLength hh + hLength (l h), l = l h, r = hh}
hLength :: (Ord a) => MinHeap a -> Int hLength Nil = 0 hLength h = cnt h
hFromList :: (Ord a) => [a] -> MinHeap a hFromList = foldl (flip hPush) Nil
hToList :: (Ord a) => MinHeap a -> [a] hToList = unfoldr f where
f Nil = Nothing f h = Just $ hPop h
main = mapM_ print $ hToList $ hFromList [ (3, "Clear drains"), (4, "Feed cat"), (5, "Make tea"), (1, "Solve RC tasks"), (2, "Tax return")]</lang>
Icon and Unicon
This solution uses classes provided by the UniLib package. Heap is an implementation of a priority queue and Closure is used to allow the queue to order lists based on their first element. The solution only works in Unicon. <lang Unicon>import Utils # For Closure class import Collections # For Heap (dense priority queue) class
procedure main()
pq := Heap(, Closure("[]",Arg,1) ) pq.add([3, "Clear drains"]) pq.add([4, "Feed cat"]) pq.add([5, "Make tea"]) pq.add([1, "Solve RC tasks"]) pq.add([2, "Tax return"])
while task := pq.get() do write(task[1]," -> ",task[2])
end </lang> Output when run:
1 -> Solve RC tasks 2 -> Tax return 3 -> Clear drains 4 -> Feed cat 5 -> Make tea
J
Implementation:
<lang j>coclass 'priorityQueue'
PRI=: QUE=:
insert=:4 :0
p=. PRI,x q=. QUE,y assert. p -:&$ q assert. 1 = #$q ord=: \: p QUE=: ord { q PRI=: ord { p i.0 0
)
topN=:3 :0
assert y<:#PRI r=. y{.QUE PRI=: y}.PRI QUE=: y}.QUE r
)</lang>
Efficiency is obtained by batching requests. Size of batch for insert is determined by size of arguments. Size of batch for topN is its right argument.
Example:
<lang j> Q=: conew'priorityQueue'
3 4 5 1 2 insert__Q 'clear drains';'feed cat';'make tea';'solve rc task';'tax return' >topN__Q 1
make tea
>topN__Q 4
feed cat clear drains tax return solve rc task</lang>
Java
Java has a PriorityQueue
class. It requires either the elements implement Comparable
, or you give it a custom Comparator
to compare the elements.
<lang java>import java.util.PriorityQueue;
class Task implements Comparable<Task> {
final int priority; final String name;
public Task(int p, String n) { priority = p; name = n; }
public String toString() { return priority + ", " + name; }
public int compareTo(Task other) { return priority < other.priority ? -1 : priority > other.priority ? 1 : 0; }
public static final void main(String[] args) { PriorityQueue<Task> pq = new PriorityQueue<Task>(); pq.add(new Task(3, "Clear drains")); pq.add(new Task(4, "Feed cat")); pq.add(new Task(5, "Make tea")); pq.add(new Task(1, "Solve RC tasks")); pq.add(new Task(2, "Tax return"));
while (!pq.isEmpty()) System.out.println(pq.remove()); }
}</lang>
output:
1, Solve RC tasks 2, Tax return 3, Clear drains 4, Feed cat 5, Make tea
Mathematica
<lang mathematica>push = Function[{queue, priority, item},
queue = SortBy[Append[queue, {priority, item}], First], HoldFirst];
pop = Function[queue,
If[Length@queue == 0, Null, With[{item = queue-1, 2}, queue = Most@queue; item]], HoldFirst];
peek = Function[queue,
If[Length@queue == 0, Null, Max[queueAll, 1]], HoldFirst];
merge = Function[{queue1, queue2},
SortBy[Join[queue1, queue2], First], HoldAll];</lang>
Example:
<lang mathematica>queue = {}; push[queue, 3, "Clear drains"]; push[queue, 4, "Feed cat"]; push[queue, 5, "Make tea"]; push[queue, 1, "Solve RC tasks"]; push[queue, 2, "Tax return"]; Print[peek[queue]]; Print[pop[queue]]; queue1 = {}; push[queue1, 6, "Drink tea"]; Print[merge[queue, queue1]];</lang>
Output:
5 Make tea {{1,Solve RC tasks},{2,Tax return},{3,Clear drains},{4,Feed cat},{6,Drink tea}}
Maxima
<lang maxima>/* Naive implementation using a sorted list of pairs [key, [item[1], ..., item[n]]]. The key may be any number (integer or not). Items are extracted in FIFO order. */
defstruct(pqueue(q = []))$
/* Binary search */
find_key(q, p) := block(
[i: 1, j: length(q), k, c], if j = 0 then false elseif (c: q[i][1]) >= p then (if c = p then i else false) elseif (c: q[j][1]) <= p then (if c = p then j else false) else catch( while j >= i do ( k: quotient(i + j, 2), if (c: q[k][1]) = p then throw(k) elseif c < p then i: k + 1 else j: k - 1 ), false )
)$
pqueue_push(pq, x, p) := block(
[q: pq@q, k], k: find_key(q, p), if integerp(k) then q[k][2]: endcons(x, q[k][2]) else pq@q: sort(cons([p, [x]], q)), 'done
)$
pqueue_pop(pq) := block(
[q: pq@q, v, x], if emptyp(q) then 'fail else ( p: q[1][1], v: q[1][2], x: v[1], if length(v) > 1 then q[1][2]: rest(v) else pq@q: rest(q), x )
)$
pqueue_print(pq) := block([t], while (t: pqueue_pop(pq)) # 'fail do disp(t))$
/* An example */
a: new(pqueue)$
pqueue_push(a, "take milk", 4)$ pqueue_push(a, "take eggs", 4)$ pqueue_push(a, "take wheat flour", 4)$ pqueue_push(a, "take salt", 4)$ pqueue_push(a, "take oil", 4)$ pqueue_push(a, "carry out crepe recipe", 5)$ pqueue_push(a, "savour !", 6)$ pqueue_push(a, "add strawberry jam", 5 + 1/2)$ pqueue_push(a, "call friends", 5 + 2/3)$ pqueue_push(a, "go to the supermarket and buy food", 3)$ pqueue_push(a, "take a shower", 2)$ pqueue_push(a, "get dressed", 2)$ pqueue_push(a, "wake up", 1)$ pqueue_push(a, "serve cider", 5 + 3/4)$ pqueue_push(a, "buy also cider", 3)$
pqueue_print(a); "wake up" "take a shower" "get dressed" "go to the supermarket and buy food" "buy also cider" "take milk" "take butter" "take flour" "take salt" "take oil" "carry out recipe" "add strawberry jam" "call friends" "serve cider" "savour !"</lang>
Objective-C
The priority queue used in this example is not actually written in Objective-C. It is part of Apple's (C-based) Core Foundation library, which is included with in Cocoa on Mac OS X and iOS. Its interface is a C function interface, which makes the code very ugly. Core Foundation is not included in GNUStep or other Objective-C APIs.
<lang objc>#import <Foundation/Foundation.h>
const void *PQRetain(CFAllocatorRef allocator, const void *ptr) {
return [(id)ptr retain];
} void PQRelease(CFAllocatorRef allocator, const void *ptr) {
[(id)ptr release];
} CFComparisonResult PQCompare(const void *ptr1, const void *ptr2, void *unused) {
return [(id)ptr1 compare:(id)ptr2];
}
@interface Task : NSObject {
int priority; NSString *name;
} - (id)initWithPriority:(int)p andName:(NSString *)n; - (NSComparisonResult)compare:(Task *)other; @end
@implementation Task - (id)initWithPriority:(int)p andName:(NSString *)n {
if ((self = [super init])) { priority = p; name = [n copy]; } return self;
} - (void)dealloc {
[name release]; [super dealloc];
} - (NSString *)description {
return [NSString stringWithFormat:@"%d, %@", priority, name];
} - (NSComparisonResult)compare:(Task *)other {
if (priority == other->priority) return NSOrderedSame; else if (priority < other->priority) return NSOrderedAscending; else return NSOrderedDescending;
} @end
int main (int argc, const char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFBinaryHeapCallBacks callBacks = {0, PQRetain, PQRelease, NULL, PQCompare}; CFBinaryHeapRef pq = CFBinaryHeapCreate(NULL, 0, &callBacks, NULL);
CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:3 andName:@"Clear drains"] autorelease]); CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:4 andName:@"Feed cat"] autorelease]); CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:5 andName:@"Make tea"] autorelease]); CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:1 andName:@"Solve RC tasks"] autorelease]); CFBinaryHeapAddValue(pq, [[[Task alloc] initWithPriority:2 andName:@"Tax return"] autorelease]);
while (CFBinaryHeapGetCount(pq) != 0) { Task *task = (id)CFBinaryHeapGetMinimum(pq); NSLog(@"%@", task); CFBinaryHeapRemoveMinimumValue(pq); }
CFRelease(pq);
[pool drain]; return 0;
} </lang>
log:
2011-08-22 07:46:19.250 Untitled[563:903] 1, Solve RC tasks 2011-08-22 07:46:19.255 Untitled[563:903] 2, Tax return 2011-08-22 07:46:19.256 Untitled[563:903] 3, Clear drains 2011-08-22 07:46:19.257 Untitled[563:903] 4, Feed cat 2011-08-22 07:46:19.258 Untitled[563:903] 5, Make tea
OCaml
Holger Arnold's OCaml base library provides a PriorityQueue module.
<lang ocaml>module PQ = Base.PriorityQueue
let () =
let tasks = [ 3, "Clear drains"; 4, "Feed cat"; 5, "Make tea"; 1, "Solve RC tasks"; 2, "Tax return"; ] in let pq = PQ.make (fun (prio1, _) (prio2, _) -> prio1 > prio2) in List.iter (PQ.add pq) tasks; while not (PQ.is_empty pq) do let _, task = PQ.first pq in PQ.remove_first pq; print_endline task done</lang>
testing:
$ ocaml -I +pcre pcre.cma base.cma pq.ml Make tea Feed cat Clear drains Tax return Solve RC tasks
Although OCaml's standard library does not have a dedicated priority queue structure, one can (for most purposes) use the built-in Set data structure as a priority queue, as long as no two elements compare equal (since Set does not allow duplicate elements). This is the case here since no two tasks should have the same name. Note that Set is a functional, persistent data structure, so we derive new priority queues from the old ones functionally, rather than modifying them imperatively; the complexity is still O(log n).
<lang ocaml>module PQSet = Set.Make
(struct type t = int * string (* pair of priority and task name *) let compare = compare end);;
let () =
let tasks = [ 3, "Clear drains"; 4, "Feed cat"; 5, "Make tea"; 1, "Solve RC tasks"; 2, "Tax return"; ] in let pq = List.fold_right PQSet.add tasks PQSet.empty in let rec aux pq' = if not (PQSet.is_empty pq') then begin let prio, name as task = PQSet.min_elt pq' in Printf.printf "%d, %s\n" prio name; aux (PQSet.remove task pq') end in aux pq</lang>
testing:
$ ocaml pq.ml 1, Solve RC tasks 2, Tax return 3, Clear drains 4, Feed cat 5, Make tea
Perl
There are a few implementations on CPAN. Following uses Heap::Priority
[1]
<lang perl>use 5.10.0;
use strict;
use Heap::Priority;
my $h = new Heap::Priority;
$h->highest_first(); # higher or lower number is more important $h->add(@$_) for ["Clear drains", 3], ["Feed cat", 4], ["Make tea", 5], ["Solve RC tasks", 1], ["Tax return", 2];
say while ($_ = $h->pop);</lang>output<lang>Make tea Feed cat Clear drains Tax return Solve RC tasks</lang>
Perl 6
This is a rather simple implementation. It requires the priority to be a positive integer value, with lower values being higher priority. There isn't a hard limit on how many priority levels you can have, though more than a few dozen is probably not practical.
The tasks are stored internally as an array of FIFO buffers, so multiple tasks of the same priority level will be returned in the order they were stored.
<lang perl6>class PriorityQueue {
has @!tasks is rw;
method insert ( Int $priority where { $priority >= 0 }, $task ) { @!tasks[$priority] //= []; @!tasks[$priority].push: $task; }
method get { @!tasks.first({$^_}).shift }
method is_empty { !?@!tasks.first({$^_}) }
}
my $pq = PriorityQueue.new;
for (
3, 'Clear drains', 4, 'Feed cat', 5, 'Make tea', 9, 'Sleep', 3, 'Check email', 1, 'Solve RC tasks', 9, 'Exercise', 2, 'Do taxes'
) -> $priority, $task {
$pq.insert( $priority, $task );
}
say $pq.get until $pq.is_empty;</lang>
Output:
Solve RC tasks Do taxes Clear drains Check email Feed cat Make tea Sleep Exercise
PHP
PHP's SplPriorityQueue
class implements a max-heap. PHP also separately has SplHeap
, SplMinHeap
, and SplMaxHeap
classes.
<lang php><?php
$pq = new SplPriorityQueue;
$pq->insert('Clear drains', 3); $pq->insert('Feed cat', 4); $pq->insert('Make tea', 5); $pq->insert('Solve RC tasks', 1); $pq->insert('Tax return', 2);
// This line causes extract() to return both the data and priority (in an associative array), // Otherwise it would just return the data $pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
while (!$pq->isEmpty()) {
print_r($pq->extract());
} ?></lang>
Output:
Array ( [data] => Make tea [priority] => 5 ) Array ( [data] => Feed cat [priority] => 4 ) Array ( [data] => Clear drains [priority] => 3 ) Array ( [data] => Tax return [priority] => 2 ) Array ( [data] => Solve RC tasks [priority] => 1 )
The difference between SplHeap
and SplPriorityQueue
is that SplPriorityQueue
takes the data and the priority as two separate arguments, and the comparison is only made on the priority; whereas SplHeap
takes only one argument (the element), and the comparison is made on that directly. In all of these classes it is possible to provide a custom comparator by subclassing the class and overriding its compare
method.
<lang php><?php
$pq = new SplMinHeap;
$pq->insert(array(3, 'Clear drains')); $pq->insert(array(4, 'Feed cat')); $pq->insert(array(5, 'Make tea')); $pq->insert(array(1, 'Solve RC tasks')); $pq->insert(array(2, 'Tax return'));
while (!$pq->isEmpty()) {
print_r($pq->extract());
} ?></lang>
Output:
Array ( [0] => 1 [1] => Solve RC tasks ) Array ( [0] => 2 [1] => Tax return ) Array ( [0] => 3 [1] => Clear drains ) Array ( [0] => 4 [1] => Feed cat ) Array ( [0] => 5 [1] => Make tea )
PicoLisp
The following implementation imposes no limits. It uses a binary tree for storage. The priority levels may be numeric, or of any other type. <lang PicoLisp># Insert item into priority queue (de insertPQ (Queue Prio Item)
(idx Queue (cons Prio Item) T) )
- Remove and return top item from priority queue
(de removePQ (Queue)
(cdar (idx Queue (peekPQ Queue) NIL)) )
- Find top element in priority queue
(de peekPQ (Queue)
(let V (val Queue) (while (cadr V) (setq V @) ) (car V) ) )
- Merge second queue into first
(de mergePQ (Queue1 Queue2)
(balance Queue1 (sort (conc (idx Queue1) (idx Queue2)))) )</lang>
Test: <lang PicoLisp># Two priority queues (off Pq1 Pq2)
- Insert into first queue
(insertPQ 'Pq1 3 '(Clear drains)) (insertPQ 'Pq1 4 '(Feed cat))
- Insert into second queue
(insertPQ 'Pq2 5 '(Make tea)) (insertPQ 'Pq2 1 '(Solve RC tasks)) (insertPQ 'Pq2 2 '(Tax return))
- Merge second into first queue
(mergePQ 'Pq1 'Pq2)
- Remove and print all items from first queue
(while Pq1
(println (removePQ 'Pq1)) )</lang>
Output:
(Solve RC tasks) (Tax return) (Clear drains) (Feed cat) (Make tea)
Prolog
SWI-Prolog has a library heaps.pl, written by Lars Buitinck that implements priority queues.
Informations here : http://www.swi-prolog.org/pldoc/doc/swi/library/heaps.pl
Example of use : <lang Prolog>priority-queue :- TL0 = [3-'Clear drains', 4-'Feed cat'],
% we can create a priority queue from a list list_to_heap(TL0, Heap0),
% alternatively we can start from an empty queue % get from empty_heap/1.
% now we add the other elements add_to_heap(Heap0, 5, 'Make tea', Heap1), add_to_heap(Heap1, 1, 'Solve RC tasks', Heap2), add_to_heap(Heap2, 2, 'Tax return', Heap3),
% we list the content of the heap: heap_to_list(Heap3, TL1), writeln('Content of the queue'), maplist(writeln, TL1), nl,
% now we retrieve the minimum-priority pair get_from_heap(Heap3, Priority, Key, Heap4), format('Retrieve top of the queue : Priority ~w, Element ~w~n', [Priority, Key]), nl,
% we list the content of the heap: heap_to_list(Heap4, TL2), writeln('Content of the queue'), maplist(writeln, TL2). </lang> The output :
1 ?- priority-queue. Content of the queue 1-Solve RC tasks 2-Tax return 3-Clear drains 4-Feed cat 5-Make tea Retrieve top of the queue : Priority 1, Element Solve RC tasks Content of the queue 2-Tax return 3-Clear drains 4-Feed cat 5-Make tea true.
PureBasic
The priority queue is implemented using a binary heap array and a map. The map stores the elements of a given priority in a FIFO list. Priorities can be any signed 32 value. <lang purebasic>Structure taskList
List description.s() ;implements FIFO queue
EndStructure
Structure task
*tl.tList ;pointer to a list of task descriptions Priority.i ;tasks priority, lower value has more priority
EndStructure
Structure priorityQueue
maxHeapSize.i ;increases as needed heapItemCount.i ;number of elements currently in heap Array heap.task(0) ;elements hold FIFO queues ordered by priorities, lowest first map heapMap.taskList() ;holds lists of tasks with the same priority that are FIFO queues
EndStructure
Procedure insertPQ(*PQ.priorityQueue, description.s, p)
If FindMapElement(*PQ\heapMap(), Str(p)) LastElement(*PQ\heapMap()\description()) AddElement(*PQ\heapMap()\description()) *PQ\heapMap()\description() = description Else Protected *tl.taskList = AddMapElement(*PQ\heapMap(), Str(p)) AddElement(*tl\description()) *tl\description() = description Protected pos = *PQ\heapItemCount *PQ\heapItemCount + 1 If *PQ\heapItemCount > *PQ\maxHeapSize Select *PQ\maxHeapSize Case 0 *PQ\maxHeapSize = 128 Default *PQ\maxHeapSize * 2 EndSelect Redim *PQ\heap.task(*PQ\maxHeapSize) EndIf While pos > 0 And p < *PQ\heap((pos - 1) / 2)\Priority *PQ\heap(pos) = *PQ\heap((pos - 1) / 2) pos = (pos - 1) / 2 Wend *PQ\heap(pos)\tl = *tl *PQ\heap(pos)\Priority = p EndIf
EndProcedure
Procedure.s removePQ(*PQ.priorityQueue)
Protected *tl.taskList = *PQ\heap(0)\tl, description.s FirstElement(*tl\description()) description = *tl\description() If ListSize(*tl\description()) > 1 DeleteElement(*tl\description()) Else DeleteMapElement(*PQ\heapMap(), Str(*PQ\heap(0)\Priority)) *PQ\heapItemCount - 1 *PQ\heap(0) = *PQ\heap(*PQ\heapItemCount) Protected pos Repeat Protected child1 = 2 * pos + 1 Protected child2 = 2 * pos + 2 If child1 >= *PQ\heapItemCount Break EndIf Protected smallestChild If child2 >= *PQ\heapItemCount smallestChild = child1 ElseIf *PQ\heap(child1)\Priority <= *PQ\heap(child2)\Priority smallestChild = child1 Else smallestChild = child2 EndIf If (*PQ\heap(smallestChild)\Priority >= *PQ\heap(pos)\Priority) Break EndIf Swap *PQ\heap(pos)\tl, *PQ\heap(smallestChild)\tl Swap *PQ\heap(pos)\Priority, *PQ\heap(smallestChild)\Priority pos = smallestChild ForEver EndIf ProcedureReturn description
EndProcedure
Procedure isEmptyPQ(*PQ.priorityQueue) ;returns 1 if empty, otherwise returns 0
If *PQ\heapItemCount ProcedureReturn 0 EndIf ProcedureReturn 1
EndProcedure
If OpenConsole()
Define PQ.priorityQueue insertPQ(PQ, "Clear drains", 3) insertPQ(PQ, "Answer Phone 1", 8) insertPQ(PQ, "Feed cat", 4) insertPQ(PQ, "Answer Phone 2", 8) insertPQ(PQ, "Make tea", 5) insertPQ(PQ, "Sleep", 9) insertPQ(PQ, "Check email", 3) insertPQ(PQ, "Solve RC tasks", 1) insertPQ(PQ, "Answer Phone 3", 8) insertPQ(PQ, "Exercise", 9) insertPQ(PQ, "Answer Phone 4", 8) insertPQ(PQ, "Tax return", 2) While Not isEmptyPQ(PQ) PrintN(removePQ(PQ)) Wend Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() CloseConsole()
EndIf</lang> Sample output:
Solve RC tasks Tax return Clear drains Check email Feed cat Make tea Answer Phone 1 Answer Phone 2 Answer Phone 3 Answer Phone 4 Sleep Exercise
Python
Using PriorityQueue
Python has the class queue.PriorityQueue in its standard library.
The data structures in the "queue" module are synchronized multi-producer, multi-consumer queues for multi-threaded use. They can however handle this task: <lang python>>>> import queue >>> pq = queue.PriorityQueue() >>> for item in ((3, "Clear drains"), (4, "Feed cat"), (5, "Make tea"), (1, "Solve RC tasks"), (2, "Tax return")): pq.put(item)
>>> while not pq.empty():
print(pq.get_nowait())
(1, 'Solve RC tasks')
(2, 'Tax return')
(3, 'Clear drains')
(4, 'Feed cat')
(5, 'Make tea')
>>> </lang>
- Help text for queue.PriorityQueue
<lang python>>>> import queue >>> help(queue.PriorityQueue) Help on class PriorityQueue in module queue:
class PriorityQueue(Queue)
| Variant of Queue that retrieves open entries in priority order (lowest first). | | Entries are typically tuples of the form: (priority number, data). | | Method resolution order: | PriorityQueue | Queue | builtins.object | | Methods inherited from Queue: | | __init__(self, maxsize=0) | | empty(self) | Return True if the queue is empty, False otherwise (not reliable!). | | This method is likely to be removed at some point. Use qsize() == 0 | as a direct substitute, but be aware that either approach risks a race | condition where a queue can grow before the result of empty() or | qsize() can be used. | | To create code that needs to wait for all queued tasks to be | completed, the preferred technique is to use the join() method. | | full(self) | Return True if the queue is full, False otherwise (not reliable!). | | This method is likely to be removed at some point. Use qsize() >= n | as a direct substitute, but be aware that either approach risks a race | condition where a queue can shrink before the result of full() or | qsize() can be used. | | get(self, block=True, timeout=None) | Remove and return an item from the queue. | | If optional args 'block' is true and 'timeout' is None (the default), | block if necessary until an item is available. If 'timeout' is | a positive number, it blocks at most 'timeout' seconds and raises | the Empty exception if no item was available within that time. | Otherwise ('block' is false), return an item if one is immediately | available, else raise the Empty exception ('timeout' is ignored | in that case). | | get_nowait(self) | Remove and return an item from the queue without blocking. | | Only get an item if one is immediately available. Otherwise | raise the Empty exception. | | join(self) | Blocks until all items in the Queue have been gotten and processed. | | The count of unfinished tasks goes up whenever an item is added to the | queue. The count goes down whenever a consumer thread calls task_done() | to indicate the item was retrieved and all work on it is complete. | | When the count of unfinished tasks drops to zero, join() unblocks. | | put(self, item, block=True, timeout=None) | Put an item into the queue. | | If optional args 'block' is true and 'timeout' is None (the default), | block if necessary until a free slot is available. If 'timeout' is | a positive number, it blocks at most 'timeout' seconds and raises | the Full exception if no free slot was available within that time. | Otherwise ('block' is false), put an item on the queue if a free slot | is immediately available, else raise the Full exception ('timeout' | is ignored in that case). | | put_nowait(self, item) | Put an item into the queue without blocking. | | Only enqueue the item if a free slot is immediately available. | Otherwise raise the Full exception. | | qsize(self) | Return the approximate size of the queue (not reliable!). | | task_done(self) | Indicate that a formerly enqueued task is complete. | | Used by Queue consumer threads. For each get() used to fetch a task, | a subsequent call to task_done() tells the queue that the processing | on the task is complete. | | If a join() is currently blocking, it will resume when all items | have been processed (meaning that a task_done() call was received | for every item that had been put() into the queue). | | Raises a ValueError if called more times than there were items | placed in the queue. | | ---------------------------------------------------------------------- | Data descriptors inherited from Queue: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
>>> </lang>
Using heapq
Python has the heapq module in its standard library.
Although one can use the heappush method to add items individually to a heap similar to the method used in the PriorityQueue example above, we will instead transform the list of items into a heap in one go then pop them off one at a time as before. <lang python>>>> from heapq import heappush, heappop, heapify >>> items = [(3, "Clear drains"), (4, "Feed cat"), (5, "Make tea"), (1, "Solve RC tasks"), (2, "Tax return")] >>> heapify(items) >>> while items: print(heappop(items))
(1, 'Solve RC tasks')
(2, 'Tax return')
(3, 'Clear drains')
(4, 'Feed cat')
(5, 'Make tea')
>>> </lang>
- Help text for module heapq
<lang python>>>> help('heapq') Help on module heapq:
NAME
heapq - Heap queue algorithm (a.k.a. priority queue).
DESCRIPTION
Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all k, counting elements from 0. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is that a[0] is always its smallest element. Usage: heap = [] # creates an empty heap heappush(heap, item) # pushes a new item on the heap item = heappop(heap) # pops the smallest item from the heap item = heap[0] # smallest item on the heap without popping it heapify(x) # transforms list into a heap, in-place, in linear time item = heapreplace(heap, item) # pops and returns smallest item, and adds # new item; the heap size is unchanged Our API differs from textbook heap algorithms as follows: - We use 0-based indexing. This makes the relationship between the index for a node and the indexes for its children slightly less obvious, but is more suitable since Python uses 0-based indexing. - Our heappop() method returns the smallest item, not the largest. These two make it possible to view the heap as a regular Python list without surprises: heap[0] is the smallest item, and heap.sort() maintains the heap invariant!
FUNCTIONS
heapify(...) Transform list into a heap, in-place, in O(len(heap)) time. heappop(...) Pop the smallest item off the heap, maintaining the heap invariant. heappush(...) Push item onto heap, maintaining the heap invariant. heappushpop(...) Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush() followed by a separate call to heappop(). heapreplace(...) Pop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement: if item > heap[0]: item = heapreplace(heap, item) merge(*iterables) Merge multiple sorted inputs into a single sorted output. Similar to sorted(itertools.chain(*iterables)) but returns a generator, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest). >>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25])) [0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25] nlargest(n, iterable, key=None) Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n] nsmallest(n, iterable, key=None) Find the n smallest elements in a dataset. Equivalent to: sorted(iterable, key=key)[:n]
DATA
__about__ = 'Heap queues\n\n[explanation by François Pinard]\n\nH... t... __all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge', '...
FILE
c:\python32\lib\heapq.py
>>> </lang>
R
Using closures: <lang R>PriorityQueue <- function() {
keys <<- values <<- NULL insert <- function(key, value) { temp <- c(keys, key) ord <- order(temp) keys <<- temp[ord] values <<- c(values, list(value))[ord] } pop <- function() { head <- values1 values <<- values[-1] keys <<- keys[-1] return(head) } empty <- function() length(keys) == 0 list(insert = insert, pop = pop, empty = empty)
}
pq <- PriorityQueue() pq$insert(3, "Clear drains") pq$insert(4, "Feed cat") pq$insert(5, "Make tea") pq$insert(1, "Solve RC tasks") pq$insert(2, "Tax return") while(!pq$empty()) {
print(pq$pop())
}</lang>With output:<lang R>[1] "Solve RC tasks" [1] "Tax return" [1] "Clear drains" [1] "Feed cat" [1] "Make tea"</lang>A similar implementation using R5 classes:<lang R>PriorityQueue <-
setRefClass("PriorityQueue", fields = list(keys = "numeric", values = "list"), methods = list( insert = function(key,value) { temp <- c(keys,key) ord <- order(temp) keys <<- temp[ord] values <<- c(values,list(value))[ord] }, pop = function() { head <- values1 keys <<- keys[-1] values <<- values[-1] return(head) }, empty = function() length(keys) == 0 ))</lang>The only change in the example would be in the instantiation:<lang R>pq <- PriorityQueue$new()</lang>
Racket
This solution implements priority queues on top of heaps. <lang racket>
- lang racket
(require data/heap)
(define pq (make-heap (λ(x y) (<= (second x) (second y)))))
(define (insert! x pri)
(heap-add! pq (list pri x)))
(define (remove-min!)
(begin0 (first (heap-min pq)) (heap-remove-min! pq)))
(insert! 3 "Clear drains") (insert! 4 "Feed cat") (insert! 5 "Make tea") (insert! 1 "Solve RC tasks") (insert! 2 "Tax return")
(remove-min!) (remove-min!) (remove-min!) (remove-min!) (remove-min!) </lang> Output: <lang racket> "Solve RC tasks" "Tax return" "Clear drains" "Feed cat" "Make tea" </lang>
Ruby
A naive, inefficient implementation <lang ruby>class PriorityQueueNaive
def initialize @q = Hash.new { |h, k| h[k] = []} @priorities = [] end
def push(priority, item) @q[priority] << item @priorities = @q.keys.sort end
def pop p = @priorities[0] item = @q[p].shift if @q[p].empty? @q.delete(p) @priorities.shift end item end
def peek if not empty? @q[@priorities[0]][0] end end
def empty? @priorities.empty? end
def inspect @q.inspect end
end
test = [
[6, "drink tea"], [3, "Clear drains"], [4, "Feed cat"], [5, "Make tea"], [6, "eat biscuit"], [1, "Solve RC tasks"], [2, "Tax return"],
]
pq = PriorityQueueNaive.new test.each {|pr, str| pq.push(pr, str) } until pq.empty?
puts pq.pop
end</lang> outputs
Solve RC tasks Tax return Clear drains Feed cat Make tea drink tea eat biscuit
Run BASIC
<lang runbasic>sqliteconnect #mem, ":memory:"
- mem execute("CREATE TABLE queue (priority float,descr text)")
' -------------------------------------------------------------- ' Insert items into the que ' --------------------------------------------------------------
- mem execute("INSERT INTO queue VALUES (3,'Clear drains')")
- mem execute("INSERT INTO queue VALUES (4,'Feed cat')")
- mem execute("INSERT INTO queue VALUES (5,'Make tea')")
- mem execute("INSERT INTO queue VALUES (1,'Solve RC tasks')")
- mem execute("INSERT INTO queue VALUES (2,'Tax return')")
'--------------- insert priority between 4 and 5 -----------------
- mem execute("INSERT INTO queue VALUES (4.5,'My Special Project')")
what$ = " -------------- Find first priority ---------------------" mem$ = "SELECT * FROM queue ORDER BY priority LIMIT 1" gosub [getQueue]
what$ = " -------------- Find last priority ---------------------" mem$ = "SELECT * FROM queue ORDER BY priority desc LIMIT 1" gosub [getQueue]
what$ = " -------------- Delete Highest Priority ---------------------" mem$ = "DELETE FROM queue WHERE priority = (select max(q.priority) FROM queue as q)"
- mem execute(mem$)
what$ = " -------------- List Priority Sequence ---------------------" mem$ = "SELECT * FROM queue ORDER BY priority" gosub [getQueue] end
[getQueue]
print what$
- mem execute(mem$)
rows = #mem ROWCOUNT() print "Priority Description" for i = 1 to rows #row = #mem #nextrow() priority = #row priority() descr$ = #row descr$() print priority;" ";descr$ next i RETURN</lang> outputs
-------------- Find first priority --------------------- Priority Description 1.0 Solve RC tasks -------------- Find last priority --------------------- Priority Description 5.0 Make tea -------------- List Priority Sequence --------------------- Priority Description 1.0 Solve RC tasks 2.0 Tax return 3.0 Clear drains 4.0 Feed cat 4.5 My Special Project
Scala
Scala has a class PriorityQueue in its standard library. <lang scala>import scala.collection.mutable.PriorityQueue case class Task(prio:Int, text:String) extends Ordered[Task] {
def compare(that: Task)=that.prio compare this.prio
}
//test var q=PriorityQueue[Task]() ++ Seq(Task(3, "Clear drains"), Task(4, "Feed cat"),
Task(5, "Make tea"), Task(1, "Solve RC tasks"), Task(2, "Tax return"))
while(q.nonEmpty) println(q dequeue)</lang> Output:
Task(1,Solve RC tasks) Task(2,Tax return) Task(3,Clear drains) Task(4,Feed cat) Task(5,Make tea)
Instead of deriving the class from Ordering an implicit conversion could be provided. <lang scala>case class Task(prio:Int, text:String) implicit def taskOrdering=new Ordering[Task] {
def compare(t1:Task, t2:Task):Int=t2.prio compare t1.prio
}</lang>
Standard ML
Note: this is a max-heap
<lang sml>structure TaskPriority = struct
type priority = int val compare = Int.compare type item = int * string val priority : item -> int = #1
end
structure PQ = LeftPriorityQFn (TaskPriority)
let
val tasks = [ (3, "Clear drains"), (4, "Feed cat"), (5, "Make tea"), (1, "Solve RC tasks"), (2, "Tax return")] val pq = foldr PQ.insert PQ.empty tasks (* or val pq = PQ.fromList tasks *) fun aux pq' = case PQ.next pq' of NONE => () | SOME ((prio, name), pq) => ( print (Int.toString prio ^ ", " ^ name ^ "\n"); aux pq )
in
aux pq
end</lang>
testing:
5, Make tea 4, Feed cat 3, Clear drains 2, Tax return 1, Solve RC tasks
Tcl
<lang tcl>package require struct::prioqueue
set pq [struct::prioqueue] foreach {priority task} {
3 "Clear drains" 4 "Feed cat" 5 "Make tea" 1 "Solve RC tasks" 2 "Tax return"
} {
# Insert into the priority queue $pq put $task $priority
}
- Drain the queue, in priority-sorted order
while {[$pq size]} {
# Remove the front-most item from the priority queue puts [$pq get]
}</lang> Which produces this output:
Make tea Feed cat Clear drains Tax return Solve RC tasks