Why does Dijkstra's Algorithm use a heap (priority queue)?

后端 未结 4 680
余生分开走
余生分开走 2021-02-07 05:33

I have tried using Djikstra\'s Algorithm on a cyclic weighted graph without using a priority queue (heap) and it worked.

Wikipedia states that the original implementatio

4条回答
  •  鱼传尺愫
    2021-02-07 05:51

    A heap is the best choice for this task as it guarantees O(log(n)) for adding edges to our queue and to remove the top element. Any other implementation of priority queue would sacrifice in either adding to our queue or removing from it to gain a performance boost somewhere else. Depending on how sparse the graph, then you might find better performance using a different implementation of priority queue, but generally speaking a min-heap is best since it balances the two.

    Not the greatest source but: http://en.wikipedia.org/wiki/Heap_(data_structure)

提交回复
热议问题