{"id":121,"date":"2026-11-13T12:31:56","date_gmt":"2026-11-13T04:31:56","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-fibonacci-heap-makes-decrease-key-cheap\/"},"modified":"2026-11-13T21:50:47","modified_gmt":"2026-11-13T13:50:47","slug":"how-a-fibonacci-heap-makes-decrease-key-cheap","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-fibonacci-heap-makes-decrease-key-cheap\/","title":{"rendered":"How a Fibonacci heap makes decrease-key cheap"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>Fibonacci heap<\/strong> is a forest of heap-ordered trees with a cached minimum root. Insert, merge, and decrease-key are <strong>amortized O(1)<\/strong>. Extract-min is <strong>amortized O(log n)<\/strong>. Decrease-key cuts the node from its parent and marks the parent. A second cut on an already marked parent <strong>cascades<\/strong>. The trees stay bushy enough that a root&#8217;s degree is O(log n), which is why extract-min can consolidate them into logarithmic time. Dijkstra with this heap is <strong>O(E + V log V)<\/strong>.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A binary heap spends O(log n) to decrease a key, and Dijkstra may decrease a key once per edge. The theoretical fix is a heap whose decrease-key does almost no work: cut the node out and hang it on the root list, next to the minimum. Unrestricted cuts produce huge degrees and a slow extract-min. The mark bit allows one child loss for free and cascades on the second loss, and that rule is what proves the degree bound.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The costs are amortized. One extract-min can rebuild the whole root list. A sequence of operations pays O(log n) per extract and O(1) per decrease on average, which is the bound Dijkstra&#8217;s analysis uses.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A sequence of n insert, merge, decrease-key, and extract-min operations, starting from empty, costs O(m + k log n) where k is the number of extracts and m is the length of the sequence, up to constant factors on the decreases and inserts. The minimum pointer makes find-min O(1). Extra memory is several pointers and a mark bit per node. The practical constant is large. A binary heap with duplicate entries usually beats a Fibonacci heap on real Dijkstra instances.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The O(E + V log V) Dijkstra bound is valid and is the main reason the structure appears in textbooks. It is not a recommendation to replace every binary heap. Cascading cut is required for the degree proof. Omitting the mark bit leaves the amortized analysis unproved and can degrade extract-min.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Keep a circular root list and a pointer to the root of minimum key. A new insert appends a single-node tree and updates the minimum pointer.<\/li>\n<li>Merge two heaps by concatenating root lists and keeping the smaller minimum pointer.<\/li>\n<li>Decrease a key. If the heap order breaks, cut the node from its parent, clear its mark, and add it to the root list. If the parent was unmarked, mark it. If the parent was marked, cut it too and cascade.<\/li>\n<li>Extract-min removes the minimum root and promotes its children to the root list. Then consolidate: among roots, link trees of equal degree until every degree is unique, as in a binomial heap, and reset the minimum pointer.<\/li>\n<li>Use the heap for Dijkstra only when the O(E + V log V) bound is the goal of the writeup. For running code, measure it against a binary heap.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The bounds are amortized, not worst-case per operation. A strict worst-case constant decrease-key needs a different heap. The structure does not make every graph algorithm linearithmic by substitution. Only algorithms whose bottleneck is decrease-key or merge, Dijkstra and Prim among them, inherit the new bound, and only in the asymptotic sense.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the mark bit is omitted<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Cut a node and never mark or cascade<\/td>\n<td>Degrees can grow too fast. Extract-min&#8217;s consolidation is then not logarithmic, and the Dijkstra bound is no longer proved.<\/td>\n<\/tr>\n<tr>\n<td>Quote worst-case O(1) decrease-key<\/td>\n<td>A cascade can cut a long path on one decrease. The O(1) figure is amortized over the sequence.<\/td>\n<\/tr>\n<tr>\n<td>Replace a binary heap in production Dijkstra without measuring<\/td>\n<td>Pointer overhead often loses to a binary heap of duplicates. The Fibonacci heap wins in the recurrence, not automatically on the clock.<\/td>\n<\/tr>\n<tr>\n<td>Consolidate on every insert<\/td>\n<td>That turns insert into an extract-style cost and throws away the amortized constant. Consolidation belongs to extract-min.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A practical priority queue remains a binary heap. A worst-case logarithmic mergeable heap is a binomial heap. The Fibonacci heap is the amortized structure behind the textbook Dijkstra bound.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment cuts a node whose decreased key breaks heap order, marks an unmarked parent, and cascades when the parent was already marked. Extract-min then consolidates equal degrees. After the minimum 1 leaves, the child holding 5 is decreased to 0, cut from its parent, and comes out next. It prints <code>1 0 2 3 4 6<\/code>.<\/p>\n\n<pre><code class=\"language-python\">class Node:\n    def __init__(self, key):\n        self.key = key\n        self.parent = None\n        self.children = []\n        self.mark = False\n        self.degree = 0\n\nclass Fib:\n    def __init__(self):\n        self.roots = []\n        self.minimum = None\n\n    def insert(self, key):\n        node = Node(key)\n        self.roots.append(node)\n        if self.minimum is None or key &lt; self.minimum.key:\n            self.minimum = node\n        return node\n\n    def cut(self, node, parent):\n        parent.children.remove(node)\n        parent.degree -= 1\n        node.parent = None\n        node.mark = False\n        self.roots.append(node)\n\n    def cascade(self, parent):\n        grand = parent.parent\n        if grand is None:\n            return\n        if not parent.mark:\n            parent.mark = True\n            return\n        self.cut(parent, grand)\n        self.cascade(grand)\n\n    def decrease(self, node, key):\n        node.key = key\n        parent = node.parent\n        if parent is not None and node.key &lt; parent.key:\n            self.cut(node, parent)\n            self.cascade(parent)\n        if node.key &lt; self.minimum.key:\n            self.minimum = node\n\n    def link(self, child, parent):\n        self.roots.remove(child)\n        child.parent = parent\n        child.mark = False\n        parent.children.append(child)\n        parent.degree += 1\n\n    def consolidate(self):\n        by_degree = {}\n        for node in self.roots[:]:\n            degree = node.degree\n            while degree in by_degree:\n                other = by_degree.pop(degree)\n                if node.key &gt; other.key:\n                    node, other = other, node\n                self.link(other, node)\n                degree = node.degree\n            by_degree[degree] = node\n        self.roots = list(by_degree.values())\n        self.minimum = min(self.roots, key=lambda n: n.key)\n\n    def extract_min(self):\n        winner = self.minimum\n        for child in winner.children:\n            child.parent = None\n            child.mark = False\n            self.roots.append(child)\n        self.roots.remove(winner)\n        if self.roots:\n            self.consolidate()\n        else:\n            self.minimum = None\n        return winner.key\n\nheap = Fib()\nnodes = [heap.insert(key) for key in (1, 2, 3, 4, 5, 6)]\nassert heap.extract_min() == 1\nchild = max((n for n in nodes if n.parent is not None), key=lambda n: n.key)\nassert child.key == 5 and child.parent.key == 4\nheap.decrease(child, 0)\nassert child.parent is None\nrest = []\nwhile heap.roots:\n    rest.append(heap.extract_min())\nassert rest == [0, 2, 3, 4, 6]\nprint(1, *rest)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Fibonacci heap is the reason some shortest-path proofs say O(E + V log V). It is rarely the queue in a running Dijkstra. A binary heap of duplicate entries is what libraries ship, because the pointer overhead of cascading cuts loses on ordinary graphs.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a Fibonacci heap in application code unless a measurement shows decrease-key dominating and the constant is acceptable. Keep it when the goal is the O(E + V log V) bound for Dijkstra or Prim, with cascading cuts included.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for a Fibonacci heap returns cascading cuts and the amortized constant decrease-key that the Dijkstra bound uses. <strong>A Fibonacci heap makes insert and decrease-key amortized O(1) by cascading cuts, and extract-min amortized O(log n), which is the heap behind Dijkstra&#8217;s O(E + V log V) bound.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: A Fibonacci heap is a forest of heap-ordered trees with a cached minimum root. Insert, merge, and decrease-key are amortized O(1). Extract-min is amortized O(log n). Decrease-key cuts the node from its parent and marks the parent. A second cut on an already marked parent cascades. The&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,35],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-heaps"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/121\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}