mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
Compare commits
4 Commits
a71d17a265
...
369994d334
Author | SHA1 | Date | |
---|---|---|---|
|
369994d334 | ||
|
fe95a31fdf | ||
|
2e35261d09 | ||
|
a21a624005 |
|
@ -1,3 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
|
||||
class Node:
|
||||
"""A node in the Fibonacci heap.
|
||||
|
||||
|
@ -27,12 +30,12 @@ class Node:
|
|||
True
|
||||
"""
|
||||
|
||||
def __init__(self, key):
|
||||
self.key = key
|
||||
def __init__(self, key: float | None) -> None:
|
||||
self.key = key or None
|
||||
self.degree = 0
|
||||
self.marked = False
|
||||
self.parent = None
|
||||
self.child = None
|
||||
self.parent = Node(None)
|
||||
self.child = Node(None)
|
||||
self.left = self
|
||||
self.right = self
|
||||
|
||||
|
@ -69,11 +72,11 @@ class FibonacciHeap:
|
|||
3
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.min_node = None
|
||||
def __init__(self) -> None:
|
||||
self.min_node = Node(None)
|
||||
self.total_nodes = 0
|
||||
|
||||
def insert(self, key):
|
||||
def insert(self, key: float | None) -> Node:
|
||||
"""Insert a new key into the heap.
|
||||
|
||||
Args:
|
||||
|
@ -105,7 +108,8 @@ class FibonacciHeap:
|
|||
self.total_nodes += 1
|
||||
return new_node
|
||||
|
||||
def _insert_into_circular_list(self, base_node, node_to_insert):
|
||||
@staticmethod
|
||||
def _insert_into_circular_list(base_node: Node, node_to_insert: Node) -> Node:
|
||||
"""Insert node into circular linked list.
|
||||
|
||||
Args:
|
||||
|
@ -127,7 +131,7 @@ class FibonacciHeap:
|
|||
>>> node2.left == node1
|
||||
True
|
||||
"""
|
||||
if base_node is None:
|
||||
if base_node.key is None:
|
||||
return node_to_insert
|
||||
|
||||
node_to_insert.right = base_node.right
|
||||
|
@ -136,7 +140,7 @@ class FibonacciHeap:
|
|||
base_node.right = node_to_insert
|
||||
return base_node
|
||||
|
||||
def extract_min(self):
|
||||
def extract_min(self) -> float | None:
|
||||
"""Remove and return the minimum key from the heap.
|
||||
|
||||
This operation removes the node with the minimum key from the heap,
|
||||
|
@ -166,7 +170,7 @@ class FibonacciHeap:
|
|||
the Fibonacci heap properties after removal of the minimum node.
|
||||
"""
|
||||
if self.min_node is None:
|
||||
return None
|
||||
return Node(None).key
|
||||
|
||||
min_node = self.min_node
|
||||
|
||||
|
@ -176,7 +180,7 @@ class FibonacciHeap:
|
|||
while True:
|
||||
next_child = current_child.right
|
||||
self._insert_into_circular_list(self.min_node, current_child)
|
||||
current_child.parent = None
|
||||
current_child.parent.key = None
|
||||
if current_child == last_child:
|
||||
break
|
||||
current_child = next_child
|
||||
|
@ -185,7 +189,7 @@ class FibonacciHeap:
|
|||
min_node.right.left = min_node.left
|
||||
|
||||
if min_node == min_node.right:
|
||||
self.min_node = None
|
||||
self.min_node.key = None
|
||||
else:
|
||||
self.min_node = min_node.right
|
||||
self._consolidate()
|
||||
|
@ -193,7 +197,7 @@ class FibonacciHeap:
|
|||
self.total_nodes -= 1
|
||||
return min_node.key
|
||||
|
||||
def _consolidate(self):
|
||||
def _consolidate(self) -> None:
|
||||
"""Consolidate the heap after removing the minimum node.
|
||||
|
||||
This internal method maintains the Fibonacci heap properties by combining
|
||||
|
@ -212,7 +216,7 @@ class FibonacciHeap:
|
|||
called directly from outside the class.
|
||||
"""
|
||||
max_degree = int(self.total_nodes**0.5) + 1
|
||||
degree_table = [None] * max_degree
|
||||
degree_table = [Node(None)] * max_degree
|
||||
|
||||
roots = []
|
||||
if self.min_node:
|
||||
|
@ -235,7 +239,7 @@ class FibonacciHeap:
|
|||
other_root.left.right = other_root.right
|
||||
other_root.right.left = other_root.left
|
||||
|
||||
if root_node.child is None:
|
||||
if root_node.child.key is None:
|
||||
root_node.child = other_root
|
||||
other_root.right = other_root
|
||||
other_root.left = other_root
|
||||
|
@ -246,19 +250,19 @@ class FibonacciHeap:
|
|||
root_node.degree += 1
|
||||
other_root.marked = False
|
||||
|
||||
degree_table[current_degree] = None
|
||||
degree_table[current_degree] = Node(None)
|
||||
current_degree += 1
|
||||
|
||||
degree_table[current_degree] = root_node
|
||||
|
||||
self.min_node = None
|
||||
self.min_node.key = None
|
||||
for degree in range(max_degree):
|
||||
if degree_table[degree] is not None and (
|
||||
self.min_node is None or (degree_table[degree].key < self.min_node.key)
|
||||
self.min_node is None or (degree_table[degree] < self.min_node.key)
|
||||
):
|
||||
self.min_node = degree_table[degree]
|
||||
|
||||
def decrease_key(self, node, new_key):
|
||||
def decrease_key(self, node: Node, new_key: float | None) -> None:
|
||||
"""Decrease the key value of a given node.
|
||||
|
||||
This operation updates the key of a node to a new, smaller value and
|
||||
|
@ -271,7 +275,7 @@ class FibonacciHeap:
|
|||
|
||||
Example:
|
||||
>>> heap = FibonacciHeap()
|
||||
>>> node = heap.insert(5)
|
||||
>>> node1 = heap.insert(5)
|
||||
>>> heap.decrease_key(node, 3)
|
||||
>>> node.key
|
||||
3
|
||||
|
@ -289,14 +293,14 @@ class FibonacciHeap:
|
|||
node.key = new_key
|
||||
parent_node = node.parent
|
||||
|
||||
if parent_node is not None and node.key < parent_node.key:
|
||||
if parent_node.key is not None and node.key < parent_node.key:
|
||||
self._cut(node, parent_node)
|
||||
self._cascading_cut(parent_node)
|
||||
|
||||
if node.key < self.min_node.key:
|
||||
self.min_node = node
|
||||
|
||||
def _cut(self, child_node, parent_node):
|
||||
def _cut(self, child_node: Node, parent_node: Node) -> None:
|
||||
"""Cut a node from its parent and add it to the root list.
|
||||
|
||||
This is a helper method used in decrease_key operations. When a node's key
|
||||
|
@ -313,7 +317,7 @@ class FibonacciHeap:
|
|||
outside the class.
|
||||
"""
|
||||
if child_node.right == child_node:
|
||||
parent_node.child = None
|
||||
parent_node.child = Node(None)
|
||||
else:
|
||||
parent_node.child = child_node.right
|
||||
child_node.right.left = child_node.left
|
||||
|
@ -322,10 +326,10 @@ class FibonacciHeap:
|
|||
parent_node.degree -= 1
|
||||
|
||||
self._insert_into_circular_list(self.min_node, child_node)
|
||||
child_node.parent = None
|
||||
child_node.parent = Node(None)
|
||||
child_node.marked = False
|
||||
|
||||
def _cascading_cut(self, current_node):
|
||||
def _cascading_cut(self, current_node: Node) -> None:
|
||||
"""Perform cascading cut operation.
|
||||
|
||||
Args:
|
||||
|
@ -338,7 +342,7 @@ class FibonacciHeap:
|
|||
self._cut(current_node, parent_node)
|
||||
self._cascading_cut(parent_node)
|
||||
|
||||
def delete(self, node):
|
||||
def delete(self, node: Node) -> None:
|
||||
"""Delete a node from the heap.
|
||||
|
||||
This operation removes a given node from the heap by first decreasing
|
||||
|
@ -365,7 +369,7 @@ class FibonacciHeap:
|
|||
self.decrease_key(node, float("-inf"))
|
||||
self.extract_min()
|
||||
|
||||
def find_min(self):
|
||||
def find_min(self) -> float | None:
|
||||
"""Return the minimum key without removing it from the heap.
|
||||
|
||||
This operation provides quick access to the minimum key in the heap
|
||||
|
@ -382,9 +386,9 @@ class FibonacciHeap:
|
|||
>>> heap.find_min()
|
||||
3
|
||||
"""
|
||||
return self.min_node.key if self.min_node else None
|
||||
return self.min_node.key if self.min_node else Node(None).key
|
||||
|
||||
def is_empty(self):
|
||||
def is_empty(self) -> bool:
|
||||
"""Check if heap is empty.
|
||||
|
||||
Returns:
|
||||
|
@ -398,9 +402,9 @@ class FibonacciHeap:
|
|||
>>> heap.is_empty()
|
||||
False
|
||||
"""
|
||||
return self.min_node is None
|
||||
return self.min_node.key is None
|
||||
|
||||
def merge(self, other_heap):
|
||||
def merge(self, other_heap: FibonacciHeap) -> None:
|
||||
"""Merge another Fibonacci heap into this one.
|
||||
|
||||
This operation combines two Fibonacci heaps by concatenating their
|
||||
|
@ -421,9 +425,9 @@ class FibonacciHeap:
|
|||
>>> heap1.total_nodes
|
||||
2
|
||||
"""
|
||||
if other_heap.min_node is None:
|
||||
if other_heap.min_node.key is None:
|
||||
return
|
||||
if self.min_node is None:
|
||||
if self.min_node.key is None:
|
||||
self.min_node = other_heap.min_node
|
||||
else:
|
||||
self.min_node.right.left = other_heap.min_node.left
|
||||
|
|
Loading…
Reference in New Issue
Block a user