I am using heapq in python 3.7
I have two questions about heapq:
I don't know how to keep the heap invariant efficiently if I just want to modify the min element.
And here is my implementation. (It is quite slow)q= [5,8,9,10] heapq.heapify(q) q[0] = 1200 heapq.heapify(q)
what do these two methods _siftdown() and _siftup() use for? And what is the difference between them? How to use these two methods to maintain the heap invariant?
Finally, I implement a code using _siftdown() (But I am Still confused about these two methods and do not ensure that whether my code is correct.)
s = time.time()
q = []
for i in range(0, 10000):
heapq.heappush(q, i)
for i in range(0, 10000):
q[0] = 10000+i
heapq._siftup(q,0)
print(q[0])
e2 =time.time()
print(e2-s)
s = time.time()
q = []
for i in range(0, 10000):
heapq.heappush(q, i)
for i in range(0, 10000):
q[0] = 10000+i
heapq.heapify(q)
print(q[0])
e2 =time.time()
print(e2-s)
The output is:
10000
0.09700560569763184
10000
7.193411350250244