pq

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2022 License: MIT Imports: 2 Imported by: 0

README

go-priority-queue Build Status Coverage Status Go Report Card GoDoc

A priority queue implementation on top of container/heap

Documentation

Overview

Package pq implements a priority queue data structure on top of container/heap. As an addition to regular operations, it allows an update of an items priority, allowing the queue to be used in graph search algorithms like Dijkstra's algorithm. Computational complexities of operations are mainly determined by container/heap. In addition, a map of items is maintained, allowing O(1) lookup needed for priority updates, which themselves are O(log n).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap interface {
	Len() int
	Insert(v interface{}, priority float64)
	Pop() (value interface{}, priority float64, err error)
	Peek() (value interface{}, priority float64, err error)
	UpdatePriority(x interface{}, newPriority float64)
}

type MaxHeap

type MaxHeap struct {
	// contains filtered or unexported fields
}

func NewMaxHeap

func NewMaxHeap() MaxHeap

func (*MaxHeap) Insert

func (p *MaxHeap) Insert(v interface{}, priority float64)

Insert inserts a new element into the queue. No action is performed on duplicate elements.

func (*MaxHeap) Len

func (p *MaxHeap) Len() int

func (*MaxHeap) Peek

func (p *MaxHeap) Peek() (value interface{}, priority float64, err error)

Peek returns the element with the highest priority from the queue. In case of an empty queue, an error is ret

func (*MaxHeap) Pop

func (p *MaxHeap) Pop() (value interface{}, priority float64, err error)

Pop removes the element with the highest priority from the queue and returns it. In case of an empty queue, an error is returned.

func (*MaxHeap) UpdatePriority

func (p *MaxHeap) UpdatePriority(x interface{}, newPriority float64)

UpdatePriority changes the priority of a given item. If the specified item is not present in the queue, no action is performed.

type MinHeap added in v0.0.3

type MinHeap struct {
	// contains filtered or unexported fields
}

MinHeap represents the queue

func NewMinHeap added in v0.0.3

func NewMinHeap() MinHeap

NewMinHeap initializes an empty priority queue.

func (*MinHeap) Insert added in v0.0.3

func (p *MinHeap) Insert(v interface{}, priority float64)

Insert inserts a new element into the queue. No action is performed on duplicate elements.

func (*MinHeap) Len added in v0.0.3

func (p *MinHeap) Len() int

Len returns the number of elements in the queue.

func (*MinHeap) Peek added in v0.0.3

func (p *MinHeap) Peek() (value interface{}, priority float64, err error)

Peek returns the element with the highest priority from the queue. In case of an empty queue, an error is ret

func (*MinHeap) Pop added in v0.0.3

func (p *MinHeap) Pop() (value interface{}, priority float64, err error)

Pop removes the element with the highest priority from the queue and returns it. In case of an empty queue, an error is returned.

func (*MinHeap) UpdatePriority added in v0.0.3

func (p *MinHeap) UpdatePriority(x interface{}, newPriority float64)

UpdatePriority changes the priority of a given item. If the specified item is not present in the queue, no action is performed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL