How to resolve the algorithm Priority queue step by step in the Nim programming language
How to resolve the algorithm Priority queue step by step in the Nim programming language
Table of Contents
Problem Statement
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.
Create a priority queue. The queue must support at least two operations:
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:
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Priority queue step by step in the Nim programming language
Source code in the nim programming language
type
PriElem[T] = tuple
data: T
pri: int
PriQueue[T] = object
buf: seq[PriElem[T]]
count: int
# first element not used to simplify indices
proc initPriQueue[T](initialSize = 4): PriQueue[T] =
result.buf.newSeq(initialSize)
result.buf.setLen(1)
result.count = 0
proc add[T](q: var PriQueue[T], data: T, pri: int) =
var n = q.buf.len
var m = n div 2
q.buf.setLen(n + 1)
# append at end, then up heap
while m > 0 and pri < q.buf[m].pri:
q.buf[n] = q.buf[m]
n = m
m = m div 2
q.buf[n] = (data, pri)
q.count = q.buf.len - 1
proc pop[T](q: var PriQueue[T]): PriElem[T] =
assert q.buf.len > 1
result = q.buf[1]
var qn = q.buf.len - 1
var n = 1
var m = 2
while m < qn:
if m + 1 < qn and q.buf[m].pri > q.buf[m+1].pri:
inc m
if q.buf[qn].pri <= q.buf[m].pri:
break
q.buf[n] = q.buf[m]
n = m
m = m * 2
q.buf[n] = q.buf[qn]
q.buf.setLen(q.buf.len - 1)
q.count = q.buf.len - 1
var p = initPriQueue[string]()
p.add("Clear drains", 3)
p.add("Feed cat", 4)
p.add("Make tea", 5)
p.add("Solve RC tasks", 1)
p.add("Tax return", 2)
while p.count > 0:
echo p.pop()
import HeapQueue
var pq = newHeapQueue[(int, string)]()
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.len() > 0:
echo pq.pop()
import tables
var
pq = initTable[int, string]()
proc main() =
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")
for i in countUp(1,5):
if pq.hasKey(i):
echo i, ": ", pq[i]
pq.del(i)
main()
You may also check:How to resolve the algorithm Teacup rim text step by step in the Julia programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Rascal programming language
You may also check:How to resolve the algorithm Function composition step by step in the VBScript programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Lua programming language
You may also check:How to resolve the algorithm Color of a screen pixel step by step in the AutoHotkey programming language