Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority Queue with a find function - Fastest Implementation

I am looking at implementing a priority queue with an added requirement, a find/search function which will tell whether an item is anywhere within the queue. So the functions will be: insert, del-min and find.

I am unsure whether I should use a Heap or a Self-balancing binary search tree. It appears PQs are usually implemented with a Heap, but I am wondering if there is any advantage in using a binary search tree since I also need that find function.

Furthermore, on average I'll be doing more inserts than deletes. I am also considering a d-ary heap. Basically, every second counts.

Thanks!

like image 554
Harry Avatar asked Oct 20 '10 02:10

Harry


People also ask

How can I make my priority queue faster?

Method 1: The simplest approach is to traverse the given array and push each element one by one in the priority queue. In this method, the push method in the priority queue takes O(log N) time.

Which type of implementation for a priority queue has the best time complexity?

Binary Heap This is the most efficient implementation of a Priority Queue. The top priority element is present at the root node of the heap and hence the peek operation has a time complexity of O(1).

What is the most common implementation of a priority queue?

Priority queues are often implemented using binary heaps.

Which is faster priority queue or set?

The priority queue only offers access to the largest element, while the set gives you a complete ordering of all elements. This weaker interface means that implementations may be more efficient (e.g. you can store the actual queue data in a vector , which may have better performance on account of its memory locality).


2 Answers

Why can't you just use a Priority Queue and a Set? When you enqueue something, you add it to the set. When you dequeue it, you remove it from the set. That way the set will tell you if something is in the queue.

like image 74
dan-gph Avatar answered Oct 16 '22 10:10

dan-gph


If your find operation is relatively infrequent (and your heap fairly small), I'd just do a linear search. If it is relatively frequent, or the heap is enormous, consider tracking heap membership (to do your 'find' test) with a separate data structure or an object flag. The joy of external indexing is being able to put your object in as many containers as you like.

If by 'find' you really mean 'find and modify' (I find I often need to delete things from priority queues independently of the typical insert/del-min), here are three approaches I've used:

Given a high rate of insert/del-min (100k/s continuous) and a low rate of find-delete (say 1/s) over a fairly small working set (500-1000) I did a linear search for the element and then deleted it from the tree in the standard way.

Given a high rate of insert/del-min plus fairly frequent find-deletes I simply marked the deleted objects as "uninteresting" after finding them indirectly. The actual free was deferred until the object was dequeued as normal.

Given a small std::priority_queue (which has no access methods outside of insert/del-min) of only a few elements and fairly infrequent deletions, I just copied the entire queue to a temporary std::vector and copied the modified/desired part back into the queue. Then I cried myself to sleep.

like image 39
Ben Jackson Avatar answered Oct 16 '22 10:10

Ben Jackson