Is partial-order, in contrast to total-order, enough to build a heap?

后端 未结 1 1984
北恋
北恋 2021-01-25 17:23

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 ordere

相关标签:
1条回答
  • 2021-01-25 17:59

    The requirement for priority_queue is (§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:

    The term strict refers to the requirement of an irreflexive relation (!comp(x, x) for all x), and the term weak to requirements that are not as strong as those for a total ordering, but stronger than those for a partial ordering. If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations:

    — comp(a, b) && comp(b, c) implies comp(a, c)

    — equiv(a, b) && equiv(b, c) implies equiv(a, c) [ Note: Under these conditions, it can be shown that

    i) equiv is an equivalence relation

    ii) comp induces a well-defined relation on the equivalence classes determined by equiv

    iii) The induced relation is a strict total ordering. — end note ]

    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.

    0 讨论(0)
提交回复
热议问题