Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The reason of using `std::greater` for creating min heap via `priority_queue`

Tags:

I am wondering why for creating a min heap using the priority_queue, the std::greater should be used?

std::priority_queue<T, std::vector<T>, std::greater<T> > min_heap; 

To me, since the smallest value is always located at the top of the heap, the employed class should be std::less

Update: On the other hand, since the default behavior of priority_queue (max heap) is to hold the greatest value at the top, it looks to me that the std::greater should be used for the max heap creation and not for min heap creation

like image 876
Vahid Noormofidi Avatar asked Sep 23 '15 19:09

Vahid Noormofidi


People also ask

Is C++ priority queue max or min?

The default priority queue in C++ is a max priority queue.

How can you implement min heap using priority queue in C++?

priority_queue supports a constructor that requires two extra arguments to make it min-heap. priority_queue <Type, vector<Type>, ComparisonType > min_heap; `The third parameter, 'Comparison Type' can either be a function or factor (aka function object) that must have bool as return-type and must have 2 arguments.

How does priority queue work C++?

A priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e. the first element will be the maximum of all elements in the queue, and elements are in decreasing order.


2 Answers

The logical argument is as follows

  1. std::priority_queue is a container adaptor; basic memory considerations make the back the preferred place for modifications (with pop_back() and push_back()) for sequence containers such as std::vector.
  2. the priority_queue primitives are based on std::make_heap (constructor), std::pop_heap + container::pop_back (priority_queue::pop) and on container::push_back + std::push_heap (priority_queue::push)
  3. pop_heap will take the front of the underlying storage, and put it at the back, restoring the heap invariant afterwards. The reverse goes for push_heap.
  4. doing sort_heap on a max_heap (with the max at the front initially) will repeatedly pop the front to the back and sort the range according to less (which is the default comparison operator)
  5. hence, the preferred implementation of a max_heap is to have the max element w.r.t. less at the front, accessed through priority_queue::top (underlying container::front).
  6. one can still debate whether it is intuitive that priority_queue with a std::less comparator is representing a max_heap. It could have been defined as a min_heap by reversing the comparator's arguments (but see the comment by @T.C. that with C++98 binders this is rather verbose) everywhere in the calls to the various heap functions. The one (for me) counter-intuitive result would have been that top() would then not have given the element with top priority
like image 68
TemplateRex Avatar answered Sep 29 '22 14:09

TemplateRex


The C++ heap functions make_heap, push_heap, and pop_heap operate on a max heap, meaning the top element is the maximum when using the default comparator. So, to create a min-heap, you need to use greater<T> instead of less<T>.

I suspect that a max heap is used instead of a min heap is that it is easier to implement with the less operation. In C++, less has the special privilege of being the sort of "default" comparator for all STL algorithms; if you only are going to implement one comparison operation (other than ==), it should be <. This leads to the unfortunate quirk that priority_queue<T, C<T>, less<T>> means a max-queue and priority_queue<T, C<T>, greater<T>> means a min-queue.

Also, certain algorithms like nth_element need a max-heap.

like image 34
rlbond Avatar answered Sep 29 '22 12:09

rlbond