I know it is possible to realize decrease-key functionality in O(log n) but I don\'t know how?
The heapq documentation has an entry on exactly how to do this.
However, I have written a heap
package that does exactly this (it is a wrapper around heapq
). So if you have pip
or easy_install
you could do something like
pip install heap
Then in your code write
from heap.heap import heap
h = heap()
h['hello'] = 4 # Insert item with priority 4.
h['hello'] = 2 # Update priority/decrease-key has same syntax as insert.
It is pretty new though, so might be full of bugs.