C++ std::priority_queue just need a partial order. But if its implementation is a binary heap, how does it works?
For example: assume we have a partially ordered set ( {a, b, c, x}, {c < b, b < a, c < a} ), x has nothing to do with a, b, c. Then a max-heap is:
layer 1: x
layer 2: b x
layer 3: x x a c
After a pop operation, in a way commonly seen in text books, i.e. replace the root with c and decrease the size by 1. Then we need to heapify the tree below, at the root:
layer 1: c
layer 2: b x
layer 3: x x a
We will swap c and b as c < b, won’t we? And what? We still don’t have a valid heap since b < a. But b cannot “see” a.
The requirement for
priority_queueis (§23.6.4 of the C++ Standard) that the comparator defines a strict, weak ordering. The latter is defined in §25.4/4 as follows:In other words, the comparator-defined relation does not have to be total, but it must be total with respect to the equivalence classes defined by a hypothetical relation
equiv, which defines all elements as equal that are not less-than or greater-than each other.To put it in even simpler terms, any elements not covered by the comparator relation will be treated as equal.