Giter Club home page Giter Club logo

go-fibheap's Introduction

High-Efficiency Fibonacci Heap

GoRef Go Report Card GitHub go.mod Go version GitHub License

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.

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.

We compared our package with Workiva/go-datastructures.

goos: linux
goarch: amd64
pkg: github.com/ksw2000/go-fibheap
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
                       │  baseline.txt  │               ours.txt               │
                       │     sec/op     │    sec/op     vs base                │
HeapExtractMin100-8       122.21µ ± 12%   59.62µ ± 16%  -51.21% (p=0.000 n=10)
HeapExtractMin1000-8      6533.6µ ± 11%   804.0µ ± 11%  -87.69% (p=0.000 n=10)
HeapExtractMin10000-8    172.212m ± 10%   8.747m ± 11%  -94.92% (p=0.000 n=10)
HeapExtractMin100000-8   12685.2m ±  2%   135.1m ±  5%  -98.93% (p=0.000 n=10)
geomean                    36.34m         2.744m        -92.45%

                       │  baseline.txt   │               ours.txt               │
                       │      B/op       │     B/op      vs base                │
HeapExtractMin100-8         87.59Ki ± 0%   12.44Ki ± 0%  -85.80% (p=0.000 n=10)
HeapExtractMin1000-8       8408.9Ki ± 0%   140.5Ki ± 0%  -98.33% (p=0.000 n=10)
HeapExtractMin10000-8     817.852Mi ± 0%   1.678Mi ± 0%  -99.79% (p=0.000 n=10)
HeapExtractMin100000-8   77056.52Mi ± 0%   19.84Mi ± 0%  -99.97% (p=0.000 n=10)
geomean                     81.57Mi        497.0Ki       -99.40%

                       │ baseline.txt │              ours.txt               │
                       │  allocs/op   │  allocs/op   vs base                │
HeapExtractMin100-8        298.0 ± 0%    199.0 ± 0%  -33.22% (p=0.000 n=10)
HeapExtractMin1000-8      2.998k ± 0%   1.999k ± 0%  -33.32% (p=0.000 n=10)
HeapExtractMin10000-8     30.01k ± 0%   20.00k ± 0%  -33.36% (p=0.000 n=10)
HeapExtractMin100000-8    300.5k ± 0%   200.0k ± 0%  -33.45% (p=0.000 n=10)
geomean                   9.474k        6.316k       -33.34%

The part of code for testing benchmark.

// ours
func benchmarkHeapExtractMin(n int) {
	h := &Heap[float64, struct{}]{}
	for i := 0; i < n; i++ {
		h.Insert(float64(i), struct{}{})
	}
	for i := 0; i < n; i++ {
		h.ExtractMin()
	}
}

// github.com/Workiva/go-datastructures/fibheap
func benchmarkHeapExtractMin(n int) {
	h := heap.NewFloatFibHeap()
	for i := 0; i < n; i++ {
		h.Enqueue(float64(i))
	}
	for i := 0; i < n; i++ {
		h.DequeueMin()
	}
}

func BenchmarkHeapExtractMin100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		benchmarkHeapExtractMin(100)
	}
}

Example

package main

import (
	"fmt"

	"github.com/ksw2000/go-fibheap"
)

func main() {
	h := &fibheap.Heap[int, string]{}
	nodes := make([]*fibheap.Element[int, string], 10)
	for i := range nodes {
		nodes[i] = h.Insert(i, fmt.Sprint(i))
	}
	// remove the nodes by given a key smaller
	// than all the key in the heap
	h.Remove(nodes[0], -100)
	h.Remove(nodes[2], -100)
	h.Remove(nodes[4], -100)
	h.Remove(nodes[6], -100)
	h.Remove(nodes[8], -100)

	// remove the element with minimum key
	m := h.ExtractMin()
	fmt.Printf("key: %d, val: %s\n", m.Key(), m.Value)
	// key: 1, val: 1

	// or you can fetch the minimum key without removing it
	m = h.Min()
	fmt.Printf("key: %d, val: %s\n", m.Key(), m.Value)
	// key: 3, val: 3

	// you can also decrease the key of elements in the heap
	h.Decreasing(nodes[5], 0)
	m = h.Min()
	fmt.Printf("key: %d, val: %s\n", m.Key(), m.Value)
	// key: 0, val: 5
}

go-fibheap's People

Contributors

ksw2000 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.