1

Is there a good way to delete values from the class queue.PriorityQueue() without ruining the priority queue? I guess theoretically I could make a loop that will get all the values until I reach the one I need, and insert all of the other ones back one without including the deleted node. This seems like overkill though. Is there a better way?

Edit: I'm trying to make a priority queue of nodes with the key being the cost to get to said node. If I find a cheaper way of getting to the node I would like to replace it on the priority queue with the cheaper cost.

7
  • 2
    Maybe, but if you want to peek at other values, then PriorityQueue isn't the data-structure you want. Tell us more about the use-case: do you want to find the top-n, nth-largest or what? In any case sounds like you really need some other data-structure. Commented May 19, 2014 at 4:26
  • This definitely indicates a problem with the choice of data structure or algorithm. Commented May 19, 2014 at 4:27
  • I added some clarification... Commented May 19, 2014 at 4:29
  • Are you writing Djikstra's shortest path algorithm? Commented May 19, 2014 at 4:52
  • No, I'm trying to implement the A* algorithm. Commented May 19, 2014 at 17:26

2 Answers 2

2

The queue module and its classes are tools primarily meant to be used for synchronization (multi-threading/multi-processing), rather than being pure data structure.

Perhaps you'll find any of that answers to this question suits your needs, or just use heapq directly.

Sign up to request clarification or add additional context in comments.

Comments

1

No, there isn't a good way to delete any arbitrary value in priority queue. You can only extract top (min/max) element from priority queue.

A set data structure (balanced binary search tree) would be better, because you can find and delete node in O(log n).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.