There is an algorithm with time complexity similar to what you need, but it works O(log n) only in average time , if that is what you need. In this algorithm, you can use the existing priority queue without the requeue() function.
Assuming you have a connection between the nodes in your graph and the items in the priority queue. Let the priority queue element also store an extra bit called ignore . The algorithm for the modified dequeue is as follows:
- Call
dequeue() - If the
ignore bit in the element is true, return to 1, otherwise return the identifier of the element.
The algorithm for the modified enqueue is as follows:
- Call Queue (item, priority)
- Visit the neighboring nodes of the
v element on the graph one at a time- change the
ignore bit to true since the current related item in the queue matches v - enqueue (v, new_priority (v))
- change the node
v connection to the new selected items. num_ignore ++
- If the ignore element number (
num_ignore ) is greater than the number of the element not ignored, rebuild the priority queuedequeue all elements, save and then enqueue again do not ignore elements
In this algorithm, setting the ignore bit takes a constant time, so you basically delay O(log n) "requeue" until you type O(n) ignore elements. Then clear them all once, which will take O(n log n) . Therefore, on average, each "request" takes O(log n) .
unsym source share