Blasted Priority queues

Status
Not open for further replies.
The best way to search a standard PQ is always going to be O(n). If you back it with something like a red-black tree to maintain order, you can get O(n) search time, but there is a severe penalty: to dequeue from the PQ now takes O(log n) (not O(1)) because you have to patch the red-black tree. So, long story short, you can't have your cake and eat it to. :)

If you really find that you need to occasionally search the thing, use a binary heap for the PQ, then you can scan the array for the heap with little cost. Plus, if you aren't dequeue-ing, just searching this way, your pop operation on the PQ is still O(1).
 
D

Deleted member 5324

Guest
Originally posted by nsample:
If you back it with something like a red-black tree to maintain order, you can get O(n) search time, but there is a severe penalty: _to dequeue from the PQ now takes O(log n) (not O(1)) because you have to patch the red-black tree._

Lookup in a balanced binary tree is O(log n), not O(n). Also, even with the "standard" implementation of a priority queue, a heap, dequeuing is O(log n) because the bottom-most element has to be moved to replace the dequeued element at top and then sifted down to the bottom.

I could be wrong, of course. If there is a way to implement a priority queue that has O(1) insert and O(1) dequeue, I'd really be interested in learning how. To my knowledge, however, there's no really good way to do so.
 
Status
Not open for further replies.