Documentation
¶
Overview ¶
Package fibheap implements a Fibonacci heap. A Fibonacci heap is a data structure for priority queue operations, consisting of a collection of heap-ordered trees. The amortized time complexity of Fibonacci heap operations are as follows: fetching the minimum is Θ(1), extracting the minimum is O(log n), inserting is Θ(1), decreasing a key is Θ(1), and merging two heaps is Θ(1).
In our implementation, we do not additionally track the key value of each element. Therefore, users should be aware that they should not insert elements with the same key into the Fibonacci heap.
Index ¶
- type Element
- type Heap
- func (h *Heap[K, V]) Decreasing(x *Element[K, V], key K)
- func (h *Heap[K, V]) ExtractMin() *Element[K, V]
- func (h *Heap[K, V]) Insert(key K, value V) *Element[K, V]
- func (h *Heap[K, V]) Min() *Element[K, V]
- func (h *Heap[K, V]) Remove(x *Element[K, V], minimumKey K)
- func (h *Heap[K, V]) Size() int
- func (h *Heap[K, V]) Union(g *Heap[K, V]) *Heap[K, V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Element ¶
type Element[K constraints.Ordered, V any] struct { // The value stored with this element. Value V // contains filtered or unexported fields }
type Heap ¶
type Heap[K constraints.Ordered, V any] struct { // contains filtered or unexported fields }
Heap represents the fibonacci heap.
Example ¶
h := &fibheap.Heap[int, string]{} h.Insert(3, "three") h.Insert(2, "two") h.Insert(1, "one") min := h.ExtractMin() fmt.Println(min.Key(), min.Value) min = h.Min() fmt.Println(min.Key(), min.Value)
Output: 1 one 2 two
func (*Heap[K, V]) Decreasing ¶
Decreasing decreases the key of element with the minimum key with amortized running time Θ(1). If the new key k is larger or equal than the key of x, Decreasing does nothing.
Example ¶
h := &fibheap.Heap[int, string]{} list := []*fibheap.Element[int, string]{} list = append(list, h.Insert(5, "one")) list = append(list, h.Insert(6, "two")) list = append(list, h.Insert(7, "three")) h.Decreasing(list[0], 1) h.Decreasing(list[1], 2) h.Decreasing(list[2], 3) min := h.ExtractMin() fmt.Println(min.Key(), min.Value) min = h.ExtractMin() fmt.Println(min.Key(), min.Value) min = h.ExtractMin() fmt.Println(min.Key(), min.Value)
Output: 1 one 2 two 3 three
func (*Heap[K, V]) ExtractMin ¶
ExtractMin() fetches and removes the minimum key from the heap h with amortized running time O(log n)
func (*Heap[K, V]) Insert ¶
Insert inserts the key-value pair (key, value) to the heap h and returns the inserted element with amortized running time Θ(1)
func (*Heap[K, V]) Remove ¶
Remove removes the element x by given a key minimumKey which is smaller than any key in the heap h.
Example ¶
h := &fibheap.Heap[int, any]{} list := []*fibheap.Element[int, any]{} list = append(list, h.Insert(5, nil)) list = append(list, h.Insert(6, nil)) list = append(list, h.Insert(7, nil)) h.Remove(list[0], 0) h.Remove(list[1], 0) fmt.Println("size:", h.Size()) fmt.Println("min:", h.Min().Key())
Output: size: 3 min: 7