Fibonacci Heap
Fibonacci Heap
Overview
A Fibonacci Heap is a collection of min-heap-ordered trees that supports amortized O(1) insert, merge, and decrease-key operations. It achieves better theoretical bounds than binary heaps for certain algorithms like Dijkstra's shortest path.
Structure
A Fibonacci Heap consists of:
- Root List: Circular doubly linked list of tree roots
- Trees: Min-heap ordered trees of varying structures
- Min Pointer: Points to the minimum element in the root list
- Degree Tracking: Each node tracks the number of children
Fibonacci Heap Structure:
Root List (circular DLL)
┌─────────────────────────┐
│ ↓
○────[15]────○────[3]────○────[18]────○
↑ ↑ ↑
│ │ │
│ Min Pointer ─────────┘
│
└─[6]──○──[12]──○──[9]
│ │
○ ○
│
[7] [10]
Node Structure
class FibonacciNode:
"""Node in Fibonacci Heap."""
def __init__(self, key):
self.key = key
self.degree = 0 # Number of children
self.marked = False # Used for decrease-key
self.parent = None
self.child = None # Pointer to one child
self.left = None # Sibling in doubly linked list
self.right = None # Sibling in doubly linked list
Operations
Insert O(1)
New nodes are inserted at the root list's beginning (O(1)):
insert(H, x):
x.degree = 0
x.parent = null
x.child = null
x.marked = false
# Add to root list
if H.min == null:
x.left = x
x.right = x
H.min = x
else:
x.right = H.min
x.left = H.min.left
H.min.left.right = x
H.min.left = x
if x.key < H.min.key:
H.min = x
H.n++ # Increment node count