MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / update_key

Method update_key

graphs/minimum_spanning_tree_prims2.py:120–132  ·  view source on GitHub ↗
(self, elem: T, weight: int)

Source from the content-addressed store, hash-verified

118 return elem
119
120 def update_key(self, elem: T, weight: int) -> None:
121 # Update the weight of the given key
122 position = self.position_map[elem]
123 self.heap[position] = (elem, weight)
124 if position > 0:
125 parent_position = get_parent_position(position)
126 _, parent_weight = self.heap[parent_position]
127 if parent_weight > weight:
128 self._bubble_up(elem)
129 else:
130 self._bubble_down(elem)
131 else:
132 self._bubble_down(elem)
133
134 def _bubble_up(self, elem: T) -> None:
135 # Place a node at the proper position (upward movement) [to be used internally

Callers 1

prims_algoFunction · 0.95

Calls 3

_bubble_upMethod · 0.95
_bubble_downMethod · 0.95
get_parent_positionFunction · 0.85

Tested by

no test coverage detected