How can I implement decrease-key functionality in Python's heapq?

前端 未结 4 954
故里飘歌
故里飘歌 2020-12-08 06:37

I know it is possible to realize decrease-key functionality in O(log n) but I don\'t know how?

4条回答
  •  春和景丽
    2020-12-08 07:21

    Decrease-key is a must-have operation for many algorithms (Dijkstra's Algorithm, A*, OPTICS), i wonder why Python's built-in priority queue does not support it.

    Unfortunately, i wasn't able to download math4tots's package.

    But, i was able to find this implementation by Daniel Stutzbach. Worked perfectly for me with Python 3.5.

    hd = heapdict()
    hd[obj1] = priority
    hd[obj1] = lower_priority
    # ...
    obj = hd.pop()
    

提交回复
热议问题