How to update element priorities in a heap for Prim's Algorithm?

前端 未结 2 740
野性不改
野性不改 2021-02-05 17:55

I am studying Prim\'s Algorithm. There is a part within the code the next vertex across the cut will be coming to the set of the vertices belonging to the MST. Whi

2条回答
  •  爱一瞬间的悲伤
    2021-02-05 18:32

    It is possible to build priority queues that support an operation called decrease-key, which takes the priority of an existing object in a priority queue and lowers it. Most versions of priority queues that ship with existing libraries don't support this operation, but it's possible to build it in several ways.

    For example, given a binary heap, you can maintain an auxiliary data structure that maps from elements to their positions in the binary heap. You would then update the binary heap implementation so that whenever a swap is performed, this auxiliary data structure is updated. Then, to implement decrease-key, you could access the table, find the position of the node in the binary heap, then continue a bubble-up step.

    Other pointer-based heaps like binomial heaps or Fibonacci heaps explicitly support this operation (the Fibonacci heap was specifically designed for it). You usually have an auxiliary map from objects to the node they occupy in the heap and can then rewire the pointers to move the node around in the heap.

    Hope this helps!

提交回复
热议问题