Documentation
¶
Overview ¶
Package prioqueue implements an efficient min and max priority queue using a binary heap encoded in a slice.
Example ¶
package main
import (
"fmt"
"math/rand"
"github.com/fgrosse/prioqueue"
)
func main() {
// The queue will be backed by a slice and knowing its size ahead of time
// avoids unnecessary allocations. If you don't know in advance how many
// items you want to push then you can set n to 0 or a negative number
// instead. In this case the slice used by the queue will start with the
// default slice capacity of Go.
n := 10
// We use a random number generator in this example to generate values.
rng := rand.New(rand.NewSource(42))
q := prioqueue.NewMaxHeap(n)
for i := 0; i < n; i++ {
// Every element we push and pop from the queue must have a unique identifier.
// It is the callers responsibility to ensure this uniqueness.
id := uint32(i)
prio := rng.Float32()
q.Push(id, prio)
}
// The queue will always return the highest priority element first.
for q.Len() > 0 {
id, prio := q.Pop()
fmt.Printf("%.2f (id %d)\n", prio, id)
}
}
Output: 0.81 (id 6) 0.65 (id 9) 0.60 (id 2) 0.38 (id 7) 0.38 (id 5) 0.38 (id 8) 0.37 (id 0) 0.21 (id 3) 0.07 (id 1) 0.04 (id 4)
Index ¶
- type Item
- type MaxHeap
- func (h *MaxHeap) Items() []*Item
- func (h *MaxHeap) Len() int
- func (h *MaxHeap) Pop() (id uint32, priority float32)
- func (h *MaxHeap) PopAndPush(item *Item)
- func (h *MaxHeap) PopItem() *Item
- func (h *MaxHeap) Push(id uint32, prio float32)
- func (h *MaxHeap) PushItem(item *Item)
- func (h *MaxHeap) Reset()
- func (h *MaxHeap) Top() (uint32, float32)
- func (h *MaxHeap) TopItem() *Item
- type MinHeap
- func (h *MinHeap) Items() []*Item
- func (h *MinHeap) Len() int
- func (h *MinHeap) Pop() (id uint32, priority float32)
- func (h *MinHeap) PopAndPush(item *Item)
- func (h *MinHeap) PopItem() *Item
- func (h *MinHeap) Push(id uint32, priority float32)
- func (h *MinHeap) PushItem(item *Item)
- func (h *MinHeap) Reset()
- func (h *MinHeap) Top() (id uint32, prio float32)
- func (h *MinHeap) TopItem() *Item
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MaxHeap ¶
type MaxHeap struct {
// contains filtered or unexported fields
}
MaxHeap implements a priority queue which allows to retrieve the highest priority element using a heap. Since the heap is maintained in form of a binary tree, it can efficiently be represented in the form of a list.
The priority queue has the following properties:
- items with high priority are dequeued before elements with lower priority
- the item at the root of the tree is the maximum among all items present in the binary heap. The same property is recursively true for all nodes in the tree.
Array representation ¶
The first element of the list is always the root node (R) of the binary tree. The two children of (R) are the next two elements in the list (A) & (B). (A) and (B) are immediately followed by the children of (A) and then the children of (B). This process continues for all nodes of the binary tree. Generally speaking, the parent of index i is at index (i-1)/2. The two children of index i are at (2*i)+1 and (2*i)+2.
Time Complexity
Push and Pop take O(log n) and Top() happens in constant time.
func NewMaxHeap ¶
NewMaxHeap returns a new MaxHeap instance which contains a pre-allocated backing array for the stored items. Usage of this function or setting a correct size is optional. If more items are inserted into the queue than there is space in the backing array, it grows automatically.
If you do not know the size in advance you can set the argument to 0 or a negative value.
func (*MaxHeap) Items ¶ added in v0.4.0
Items returns all elements that are currently in the queue. The caller should ignore the order in which elements are returned since this only reflects how the queue stores its items internally.
func (*MaxHeap) Pop ¶
Pop removes the item with the highest priority value from the queue and returns its ID and priority.
Note that while popping an element from the heap will also remove it from the queue but it will not release the memory in the backing array as long as the heap is still in use. See https://blog.golang.org/slices-intro#TOC_6.
func (*MaxHeap) PopAndPush ¶ added in v0.3.0
PopAndPush removes the item with the highest priority value and adds a new value to the heap in one operation. This is faster than two separate calls to Pop and Push.
func (*MaxHeap) Reset ¶
func (h *MaxHeap) Reset()
Reset is a fast way to empty the queue. Note that the underlying array will still be used by the heap which means that this function will not free up any memory. If you need to release memory, you have to create a new instance and let this one be taken care of by the garbage collection.
type MinHeap ¶
type MinHeap struct {
// contains filtered or unexported fields
}
MinHeap implements a priority queue which allows to retrieve the lowest priority element using a heap. Since the heap is maintained in form of a binary tree, it can efficiently be represented in the form of a list.
The priority queue has the following properties:
- items with low priority are dequeued before elements with higher priority
- the item at the root of the tree is the minimum among all items present in the binary heap. The same property is recursively true for all nodes in the tree.
Array representation ¶
The first element of the list is always the root node (R) of the binary tree. The two children of (R) are the next two elements in the list (A) & (B). (A) and (B) are immediately followed by the children of (A) and then the children of (B). This process continues for all nodes of the binary tree. Generally speaking, the parent of index i is at index (i-1)/2. The two children of index i are at (2*i)+1 and (2*i)+2.
Time Complexity
Push and Pop take O(log n) and Top() happens in constant time.
func NewMinHeap ¶
NewMinHeap returns a new MinHeap instance which contains a pre-allocated backing array for the stored items. Usage of this function or setting a correct size is optional. If more items are inserted into the queue than there is space in the backing array, it grows automatically.
If you do not know the size in advance you can set the argument to 0 or a negative value.
func (*MinHeap) Items ¶ added in v0.4.0
Items returns all elements that are currently in the queue. The caller should ignore the order in which elements are returned since this only reflects how the queue stores its items internally.
func (*MinHeap) Pop ¶
Pop removes the item with the lowest priority value from the queue and returns its ID and priority.
Note that while popping an element from the heap will also remove it from the queue but it will not release the memory in the backing array as long as the heap is still in use. See https://blog.golang.org/slices-intro#TOC_6.
func (*MinHeap) PopAndPush ¶ added in v0.3.0
PopAndPush removes the item with the lowest priority value and adds a new value to the heap in one operation. This is faster than two separate calls to Pop and Push.
func (*MinHeap) Reset ¶
func (h *MinHeap) Reset()
Reset is a fast way to empty the queue. Note that the underlying array will still be used by the heap which means that this function will not free up any memory. If you need to release memory, you have to create a new instance and let this one be taken care of by the garbage collection.
