Java Priority Queue reordering when editing elements

后端 未结 6 1120
陌清茗
陌清茗 2020-11-29 06:23

I\'m trying to implement Dijkstra\'s algorithm for finding shortest paths using a priority queue. In each step of the algorithm, I remove the vertex with the shortest distan

6条回答
  •  遥遥无期
    2020-11-29 07:08

    I solve this problem by dividing my process into timeSlots ( A time Scheduler will be just fine ) and Extending the native PriorityQueue. So I implement a notify method where the key of this method is the following code:

    // If queue has one or less elements, then it shouldn't need an ordering
    // procedure
    if (size() > 1)
    {
        // holds the current size, as during this process the size will
        // be vary
        int tmpSize = size();
        for (int i = 1; i < tmpSize; i++)
        {
            add(poll());
        }
    }
    

    I hope It helped.

提交回复
热议问题