Priority Queues (Chapter 7) Priority Queue ADT -Keys, Priorities, and Total order Relations - Sorting with a Priority Queue Priority Queue implementation -Implementation with an unsorted sequence - Implementation with a sorted sequence
The Priority Queue Abstract Data Type
We want a comparison rule that will never contradict itself. This requires that the rule define a total order relation. total order relation: Reflexive property: k k. Antisymmetric property: if k 1 k 2 and k 2 k 1, then k 1 = k 2. Transitive property: if k 1 k 2 and k 2 k 3, then k 1 k 3. Examples: Integers, real numbers, lexicographic order of character sequence.
v1 v2 if x2 - x1 = = x4 - x3 Then we have = Therefore, (1, 4) (4, 7) and (4, 7) (1, 4). But (1,4) (7, 4), namely, the relation does not satisfy the antisymmetric property.
If a comparison rule defines a total order relation, it will never lead to a comparison contradiction. the smallest key: If we have a finite number of elements with a total order relation, then the smallest key, denoted by k min, is well-defined: k min is the key that satisfies k min k for any other key k. Being able to find the smallest key is very important because in many cases, we want to have the element with the smallest key.