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

Method top_to_bottom

graphs/minimum_spanning_tree_prims.py:15–39  ·  view source on GitHub ↗
(self, heap, start, size, positions)

Source from the content-addressed store, hash-verified

13 self.node_position[vertex] = pos
14
15 def top_to_bottom(self, heap, start, size, positions):
16 if start > size // 2 - 1:
17 return
18 else:
19 if 2 * start + 2 >= size: # noqa: SIM114
20 smallest_child = 2 * start + 1
21 elif heap[2 * start + 1] < heap[2 * start + 2]:
22 smallest_child = 2 * start + 1
23 else:
24 smallest_child = 2 * start + 2
25 if heap[smallest_child] < heap[start]:
26 temp, temp1 = heap[smallest_child], positions[smallest_child]
27 heap[smallest_child], positions[smallest_child] = (
28 heap[start],
29 positions[start],
30 )
31 heap[start], positions[start] = temp, temp1
32
33 temp = self.get_position(positions[smallest_child])
34 self.set_position(
35 positions[smallest_child], self.get_position(positions[start])
36 )
37 self.set_position(positions[start], temp)
38
39 self.top_to_bottom(heap, smallest_child, size, positions)
40
41 # Update function if value of any node in min-heap decreases
42 def bottom_to_top(self, val, index, heap, position):

Callers 2

heapifyMethod · 0.95
delete_minimumMethod · 0.95

Calls 2

get_positionMethod · 0.95
set_positionMethod · 0.95

Tested by

no test coverage detected