{"id":20842806,"url":"https://github.com/mudge/fibonacci_heap","last_synced_at":"2026-03-08T21:06:50.260Z","repository":{"id":56846295,"uuid":"138790802","full_name":"mudge/fibonacci_heap","owner":"mudge","description":"A Ruby implementation of the Fibonacci heap data structure.","archived":false,"fork":false,"pushed_at":"2018-07-15T16:09:18.000Z","size":55,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-08T18:53:38.188Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/fibonacci_heap","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mudge.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-26T20:39:29.000Z","updated_at":"2024-12-31T01:56:43.000Z","dependencies_parsed_at":"2022-09-09T01:22:41.430Z","dependency_job_id":null,"html_url":"https://github.com/mudge/fibonacci_heap","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Ffibonacci_heap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Ffibonacci_heap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Ffibonacci_heap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Ffibonacci_heap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudge","download_url":"https://codeload.github.com/mudge/fibonacci_heap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253160727,"owners_count":21863624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T01:25:33.751Z","updated_at":"2026-03-08T21:06:45.242Z","avatar_url":"https://github.com/mudge.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fibonacci Heap [![Build Status](https://travis-ci.org/mudge/fibonacci_heap.svg?branch=master)](https://travis-ci.org/mudge/fibonacci_heap)\n\nA Ruby implementation of the [Fibonacci heap](https://en.wikipedia.org/wiki/Fibonacci_heap) data structure ideal for use as a priority queue with [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra's_algorithm#Using_a_priority_queue).\n\n**Current version:** 0.2.0  \n**Supported Ruby versions:** 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1, 2.2, 2.3\n\n## Installation\n\n```\ngem install fibonacci_heap -v '~\u003e 0.2'\n```\n\nOr, in your `Gemfile`:\n\n```ruby\ngem 'fibonacci_heap', '~\u003e 0.2'\n```\n\n## Usage\n\n```ruby\nrequire 'fibonacci_heap'\n\nheap = FibonacciHeap::Heap.new\nfoo = FibonacciHeap::Node.new(1, 'foo')\nbar = FibonacciHeap::Node.new(0, 'bar')\nbaz = FibonacciHeap::Node.new(2, 'baz')\nheap.insert(foo)\nheap.insert(bar)\nheap.insert(baz)\nheap.pop\n#=\u003e #\u003cFibonacciHeap::Node key=0 value=\"bar\"\u003e\nheap.decrease_key(baz, 0)\nheap.pop\n#=\u003e #\u003cFibonacciHeap::Node key=0 value=\"baz\"\u003e\n```\n\n## API Documentation\n\n* [`FibonacciHeap::Heap`](#fibonacciheapheap)\n  * [`.new`](#fibonacciheapheapnew)\n  * [`#n`](#fibonacciheapheapn)\n  * [`#size`](#fibonacciheapheapn)\n  * [`#length`](#fibonacciheapheapn)\n  * [`#empty?`](#fibonacciheapheapempty)\n  * [`#min`](#fibonacciheapheapmin)\n  * [`#insert(x[, k])`](#fibonacciheapheapinsertx-k)\n  * [`#concat(h2)`](#fibonacciheapheapconcath2)\n  * [`#pop`](#fibonacciheapheappop)\n  * [`#decrease_key(x, k)`](#fibonacciheapheapdecrease_keyx-k)\n  * [`#delete(x)`](#fibonacciheapheapdeletex)\n  * [`#clear`](#fibonacciheapheapclear)\n* [`FibonacciHeap::Node`](#fibonacciheapnode)\n  * [`new(key[, value])`](#fibonacciheapnodenewkey-value)\n  * [`key`](#fibonacciheapnodekey)\n  * [`value`](#fibonacciheapnodevalue)\n* [`FibonacciHeap::InvalidKeyError`](#fibonacciheapinvalidkeyerror)\n\n### `FibonacciHeap::Heap`\n\nA Fibonacci Heap data structure.\n\nA \"mergeable heap\" that supports several operations that run in\nconstant amortized time. Structured as a collection of rooted trees\nthat are min-heap ordered.\n\n#### `FibonacciHeap::Heap.new`\n\n```ruby\nheap = FibonacciHeap::Heap.new\n#=\u003e #\u003cFibonacciHeap n=0 min=nil\u003e\n```\n\nReturn a new, empty [`FibonacciHeap::Heap`](#fibonacciheapheap) instance.\n\n#### `FibonacciHeap::Heap#n`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nheap.insert(FibonacciHeap::Node.new('foo'))\nheap.n\n#=\u003e 1\nheap.size\n#=\u003e 1\nheap.length\n#=\u003e 1\n```\n\nReturn the current number of nodes in the heap.\n\nAliased to `size` and `length`.\n\n#### `FibonacciHeap::Heap#empty?`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nheap.empty?\n#=\u003e true\n```\n\nReturns whether or not the heap is empty.\n\n#### `FibonacciHeap::Heap#min`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nheap.insert(FibonacciHeap::Node.new(1))\nheap.insert(FibonacciHeap::Node.new(2))\nheap.min\n#=\u003e #\u003cFibonacciHeap::Node key=1 value=1\u003e\n```\n\nReturn the smallest [`FibonacciHeap::Node`](#fibonacciheapnode) node in the heap as determined by the node's `key`.\n\nWill return `nil` if the heap is empty.\n\n#### `FibonacciHeap::Heap#insert(x[, k])`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nnode = FibonacciHeap::Node.new(1, 'foo')\nnode2 = FibonacciHeap::Node.new(0, 'bar')\nheap.insert(node)\n#=\u003e #\u003cFibonacciHeap::Node key=1 value=\"foo\"\u003e\nheap.insert(node2, 100)\n#=\u003e #\u003cFibonacciHeap::Node key=100 value=\"bar\"\u003e\n```\n\nInsert the given [`FibonacciHeap::Node`](#fibonacciheapnode) `x` into the heap with an optional key `k`.\n\nDefaults to using `x`'s existing `key` for `k`.\n\n#### `FibonacciHeap::Heap#concat(h2)`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nheap.insert(FibonacciHeap::Node.new(1, 'foo'))\nheap2 = FibonacciHeap::Heap.new\nheap2.insert(FibonacciHeap::Node.new(2, 'bar'))\n\nheap3 = heap.concat(heap2)\n#=\u003e #\u003cFibonacciHeap::Heap n=2 min=#\u003cFibonacciHeap::Node key=1 value=\"foo\"\u003e\u003e\n\nheap3.pop\n#=\u003e #\u003cFibonacciHeap::Node key=1 value=\"foo\"\u003e\nheap3.pop\n#=\u003e #\u003cFibonacciHeap::Node key=2 value=\"bar\"\u003e\n```\n\nUnite the given [`FibonacciHeap::Heap`](#fibonacciheapheap) `h2` with this one in a new [`FibonacciHeap::Heap`](#fibonacciheapheap).\n\nAs this will mutate both collections of rooted trees, attempting to use either the original heap or `h2` after `concat` has undefined behaviour.\n\n#### `FibonacciHeap::Heap#pop`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nheap.insert(FibonacciHeap::Node.new(1, 'foo'))\nheap.pop\n#=\u003e #\u003cFibonacciHeap::Node key=1 value=\"foo\"\u003e\n```\n\nRemove and return the smallest [`FibonacciHeap::Node`](#fibonacciheapnode) from the heap.\n\n#### `FibonacciHeap::Heap#decrease_key(x, k)`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nnode = FibonacciHeap::Node.new(1, 'foo')\nheap.insert(node)\nheap.decrease_key(node, 0)\n#=\u003e #\u003cFibonacciHeap::Node key=0 value=\"foo\"\u003e\n```\n\nDecrease the key of the given [`FibonacciHeap::Node`](#fibonacciheapnode) `x` to the new given key `k`.\n\nThe node must already be inserted into the heap and the key must be comparable.\n\nRaises a [`FibonacciHeap::InvalidKeyError`](#fibonacciheapinvalidkeyerror) if the new key is greater than the current key.\n\n#### `FibonacciHeap::Heap#delete(x)`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nnode = FibonacciHeap::Node.new(1, 'foo')\nheap.insert(node)\nheap.delete(node)\n#=\u003e #\u003cFibonacciHeap::Node key=-Infinity value=\"foo\"\u003e\n```\n\nDeletes the given [`FibonacciHeap::Node`](#fibonacciheapnode) `x` from the heap.\n\nThe node must already be inserted into the heap.\n\n#### `FibonacciHeap::Heap#clear`\n\n```ruby\nheap = FibonacciHeap::Heap.new\nheap.insert(FibonacciHeap::Node.new(1, 'foo'))\nheap.clear\n#=\u003e #\u003cFibonacciHeap::Heap n=0 min=nil\u003e\n```\n\nRemove all nodes from the heap, emptying it.\n\n### `FibonacciHeap::Node`\n\nA single node in a [`FibonacciHeap::Heap`](#fibonacciheapheap).\n\nUsed internally to form both min-heap ordered trees and circular, doubly linked lists.\n\n#### `FibonacciHeap::Node.new(key[, value])`\n\n```ruby\nnode = FibonacciHeap::Node.new(1)\n#=\u003e #\u003cFibonacciHeap::Node key=1 value=1\u003e\nnode = FibonacciHeap::Node.new(1, 'foo')\n#=\u003e #\u003cFibonacciHeap::Node key=1 value=\"foo\"\u003e\n```\n\nReturn a new [`FibonacciHeap::Node`](#fibonacciheapnode) with the given key `key` and an optional value `value`.\n\nDefaults to using the `key` as the value.\n\n#### `FibonacciHeap::Node#key`\n\n```ruby\nnode = FibonacciHeap::Node.new(1, 'foo')\nnode.key\n#=\u003e 1\n```\n\nReturn the current key of the node.\n\n#### `FibonacciHeap::Node#value`\n\n```ruby\nnode = FibonacciHeap::Node.new(1, 'foo')\nnode.value\n#=\u003e \"foo\"\n```\n\nReturn the current value of the node.\n\n### `FibonacciHeap::InvalidKeyError`\n\nRaised when attempting to decrease a key but the new key is greater than the current key.\n\n## How is this different from [PQueue](https://github.com/rubyworks/pqueue) or [algorithms](https://github.com/kanwei/algorithms)' [`Containers::PriorityQueue`](http://kanwei.github.io/algorithms/classes/Containers/PriorityQueue.html)?\n\nPQueue and `Containers::PriorityQueue` are also implementations of a Priority Queue but my specific use-case required the ability to alter the priority (really, the `key`) of arbitrary nodes _after_ insertion. PQueue allows you to customise the comparison of nodes but this is only done at insertion time. Similarly, `Containers::PriorityQueue` does not allow you to alter the priority of a specific node (you can delete a single node by its priority but any other node with the same priority might be deleted instead).\n\nFurthermore, as the [reference text](#references) for my implementation often relies on the user having references to the nodes in the heap (e.g. for [`decrease_key`](#fibonacciheapheapdecrease_keyx-k) and [`delete`](#fibonacciheapheapdeletex)), I wanted to make the notion of the [`Node`](#fibonacciheapnode) explicit in the API. By having the user initialize [`Node`](#fibonacciheapnode)s themselves, it then makes the passing of [`Node`](#fibonacciheapnode)s to methods such as [`insert`](#fibonacciheapheapinsertx-k), [`delete`](#fibonacciheapheapdeletex) and [`decrease_key`](#fibonacciheapheapdecrease_keyx-k) more consistent.\n\n## References\n\n* Cormen, T. H., Leiserson, C. E., Rivest, R. L. \u0026 Stein, C., [Introduction to Algorithms, Third Edition](https://mitpress.mit.edu/books/introduction-algorithms-third-edition).\n\n## License\n\nCopyright © 2018 Paul Mucur\n\nDistributed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Ffibonacci_heap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudge%2Ffibonacci_heap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Ffibonacci_heap/lists"}