Skip to content

Implement Fibonacci Heap data structure #9239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8c3b8b3
Fix #9095 : Implement Fibonacci Heap data structure
Dhruv127 Oct 1, 2023
23a39fc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
ef2c1ee
Add missing doctests
Dhruv127 Oct 1, 2023
32aa637
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
06fb015
Ensured proper type annotations and removed unused imports. - Fixed p…
Dhruv127 Oct 1, 2023
dd6acd5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
f0ddb4e
Fixed line length issues for better readability. - Removed future imp…
Dhruv127 Oct 1, 2023
46a7191
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
5da268b
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
8cd50d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
044afb0
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
774ba12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
fee3f04
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
053b55f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
f969985
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
aef5090
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
4b16251
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
b554801
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
908891c
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
f08e189
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
e67a442
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
dab519f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
11d84fe
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
d864b47
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
9fc373d
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
0a897c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
b2df7cd
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
e421610
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
5076ab4
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
757daaf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
bb257cb
Update fibonacci_heap.py
Dhruv127 Oct 1, 2023
160f525
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions data_structures/heap/fibonacci_heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Reference: https://en.wikipedia.org/wiki/Fibonacci_heap

import math


class FibonacciTree:
"""
FibonacciTree represents a node in a Fibonacci heap.

Attributes:
key (int): The key value associated with the node.
children (list): A list of child nodes.
order (int): The number of children the node has.
"""

def __init__(self, key: int) -> None:
self.key = key
self.children: list['FibonacciTree'] = []
self.order = 0

def add_at_end(self, child_node: 'FibonacciTree') -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function add_at_end

"""
Adds a child node 'child_node' to the end of the children list.

Args:
child_node (FibonacciTree): The child node to add.
"""
self.children.append(child_node)
self.order = self.order + 1


class FibonacciHeap:
"""
FibonacciHeap represents a priority queue data structure with efficient
amortized time complexities for various operations.

Usage:
>>> heap = FibonacciHeap()
>>> heap.insert(5)
>>> heap.insert(3)
>>> heap.insert(7)
>>> heap.get_min()
3
>>> heap.extract_min()
3
>>> heap.get_min()
5

Attributes:
trees (list): A list of FibonacciTree objects.
least (FibonacciTree | None): A reference to the node with the minimum key.
count (int): The total number of nodes in the heap.
"""

def __init__(self) -> None:
self.trees: list['FibonacciTree'] = []
self.least: 'FibonacciTree' | None = None
self.count = 0

def insert(self, key: int) -> None:
"""
Inserts a new node with the given key into the Fibonacci heap.

Args:
key (int): The key to insert.
"""
new_tree = FibonacciTree(key)
self.trees.append(new_tree)
if (self.least is None or key < self.least.key):
self.least = new_tree
self.count = self.count + 1

def get_min(self) -> int | None:
"""
Returns the minimum key in the Fibonacci heap.

Returns:
int: The minimum key.
"""
if self.least is None:
return None
return self.least.key

def extract_min(self) -> int | None:
"""
Removes and returns the node with the minimum key from the Fibonacci heap.

Returns:
int: The minimum key.
"""
smallest = self.least
if smallest is not None:
for child in smallest.children:
if child is not None:
self.trees.append(child)
self.trees.remove(smallest)
if self.trees:
self.least = self.trees[0]
self.consolidate()
else:
self.least = None
self.count = self.count - 1
return smallest.key
return None

def consolidate(self) -> None:
"""
Consolidates trees in the Fibonacci heap to maintain the heap's structure.
"""
aux = (floor_log2(self.count) + 1) * [None]

while self.trees:
x = self.trees[0]
order = x.order
self.trees.remove(x)
while aux[order] is not None:
y = aux[order]
if x.key > y.key:
x, y = y, x
x.add_at_end(y)
aux[order] = None
order = order + 1
aux[order] = x

self.least = None
for k in aux:
if k is not None:
self.trees.append(k)
if (self.least is None or k.key < self.least.key):
self.least = k

def floor_log2(x: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/heap/fibonacci_heap.py, please provide doctest for the function floor_log2

Please provide descriptive name for the parameter: x

"""
Computes the floor of the base-2 logarithm of 'x'.

Args:
x (int): The input number.

Returns:
int: The floor of the base-2 logarithm of 'x'.
"""
return math.floor(math.log2(x)) if x > 0 else 0

# Doctest for floor_log2
if __name__ == "__main__":
import doctest
doctest.testmod()

if __name__ == "__main__":
import doctest
doctest.testmod()