MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / topToBottom

Function topToBottom

Graphs/minimum_spanning_tree_prims.py:13–33  ·  view source on GitHub ↗
(heap, start, size, positions)

Source from the content-addressed store, hash-verified

11 nodePosition[vertex] = pos
12
13 def topToBottom(heap, start, size, positions):
14 if start > size // 2 - 1:
15 return
16 else:
17 if 2 * start + 2 >= size:
18 m = 2 * start + 1
19 else:
20 if heap[2 * start + 1] < heap[2 * start + 2]:
21 m = 2 * start + 1
22 else:
23 m = 2 * start + 2
24 if heap[m] < heap[start]:
25 temp, temp1 = heap[m], positions[m]
26 heap[m], positions[m] = heap[start], positions[start]
27 heap[start], positions[start] = temp, temp1
28
29 temp = getPosition(positions[m])
30 setPosition(positions[m], getPosition(positions[start]))
31 setPosition(positions[start], temp)
32
33 topToBottom(heap, m, size, positions)
34
35 # Update function if value of any node in min-heap decreases
36 def bottomToTop(val, index, heap, position):

Callers 2

heapifyFunction · 0.85
deleteMinimumFunction · 0.85

Calls 2

getPositionFunction · 0.85
setPositionFunction · 0.85

Tested by

no test coverage detected