Python calculates minimum priority queue code sharing
- 2020-04-02 13:17:47
- OfStack
# -*- coding: utf-8 -*-
class Heap(object):
@classmethod
def parent(cls, i):
""" Parent subscript """
return int((i - 1) >> 1);
@classmethod
def left(cls, i):
""" Left son subscript """
return (i << 1) + 1;
@classmethod
def right(cls, i):
""" Right son subscript """
return (i << 1) + 2;
class MinPriorityQueue(list, Heap):
@classmethod
def min_heapify(cls, A, i, heap_size):
""" The minimum heap, A[i] Is the subtree of the root """
l, r = cls.left(i), cls.right(i)
if l < heap_size and A[l] < A[i]:
least = l
else:
least = i
if r < heap_size and A[r] < A[least]:
least = r
if least != i:
A[i], A[least] = A[least], A[i]
cls.min_heapify(A, least, heap_size)
def minimum(self):
""" Returns the smallest element, the pseudo-code is as follows:
HEAP-MINIMUM(A)
1 return A[1]
T(n) = O(1)
"""
return self[0]
def extract_min(self):
""" Remove and return the smallest element. The pseudo-code is as follows:
HEAP-EXTRACT-MIN(A)
1 if heap-size[A] < 1
2 then error "heap underflow"
3 min please A[1]
4 A[1] please A[heap-size[A]] //The tail element comes first
5 heap-size[A] please heap-size[A] - 1 //Reduce heap - size [A]
6 MIN-HEAPIFY(A, 1) //Keep the minimum heap property
7 return min
T(n) = Theta. (lgn)
"""
heap_size = len(self)
assert heap_size > 0, "heap underflow"
val = self[0]
tail = heap_size - 1
self[0] = self[tail]
self.min_heapify(self, 0, tail)
self.pop(tail)
return val
def decrease_key(self, i, key):
""" will i The value at PI goes down to PI key , the pseudo-code is as follows:
HEAP-DECREASE-KEY(A, i, key)
1 if key > A[i]
2 then error "new key is larger than current key"
3 A[i] please key
4 while i > 1 and A[PARENT(i)] > A[i] //Not the root and the parent is larger
5 do exchange A[i] ↔ A[PARENT(i)] //Swap two elements
6 i please PARENT(i) //Point to the parent location
T(n) = Theta. (lgn)
"""
val = self[i]
assert key <= val, "new key is larger than current key"
self[i] = key
parent = self.parent
while i > 0 and self[parent(i)] > self[i]:
self[i], self[parent(i)] = self[parent(i)], self[i]
i = parent(i)
def insert(self, key):
""" will key insert A , the pseudo-code is as follows:
MIN-HEAP-INSERT(A, key)
1 heap-size[A] please heap-size[A] + 1 //Increase the number of elements
2 A[heap-size[A]] please + up //We started off with a plus infinity
3 HEAP-DECREASE-KEY(A, heap-size[A], key) //Reduce the new element to key
T(n) = Theta. (lgn)
"""
self.append(float('inf'))
self.decrease_key(len(self) - 1, key)
if __name__ == '__main__':
import random
keys = range(10)
random.shuffle(keys)
print(keys)
queue = MinPriorityQueue() # The insertion mode builds the minimum heap
for i in keys:
queue.insert(i)
print(queue)
print('*' * 30)
for i in range(len(queue)):
val = i % 3
if val == 0:
val = queue.extract_min() # Remove and return the smallest element
elif val == 1:
val = queue.minimum() # Return the smallest element
else:
val = queue[1] - 10
queue.decrease_key(1, val) # queue[1] To reduce 10
print(queue, val)
print([queue.extract_min() for i in range(len(queue))])