def _min_heap_insert(A, z): A.heap_size = A.heap_size + 1 i = A.heap_size A[i] = z while i > 1 and A[parent(i)].f > A[i].f: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i)
def _min_heap_insert(A, z): A.heap_size = A.heap_size + 1 i = A.heap_size A[i] = z while i > 1 and A[parent(i)].f > A[i].f: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i)
def _heap_decrease_pair_key(A, i, pair): if pair[0].key > A[i][0].key: raise RuntimeError('new key is larger than current key') A[i] = pair while i > 1 and A[parent(i)][0].key > A[i][0].key: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i)
def _heap_increase_element_key(A, i, x): if x.key < A[i].key: raise RuntimeError('new key is smaller than current key') A[i] = x while i > 1 and A[parent(i)].key < A[i].key: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i)
def heap_decrease_key(A, i, key): if key > A[i]: raise RuntimeError('new key is larger than current key') A[i] = key while i > 1 and A[parent(i)] > A[i]: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i)
def heap_increase_key(A, i, key): if key < A[i]: raise RuntimeError('new key is smaller than current key') A[i] = key while i > 1 and A[parent(i)] < A[i]: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i)
def max_heap_delete(A, i): A[i], A[A.heap_size] = A[A.heap_size], A[i] A.heap_size = A.heap_size - 1 max_heapify(A, i) while i > 1 and A[parent(i)] < A[i]: A[i], A[parent(i)] = A[parent(i)], A[i] i = parent(i) return A[A.heap_size + 1]