Priority Queues Review the abstract data type Priority

Priority Queues • Review the abstract data type Priority Queues • Review different implementation options f 00 1

Abstract Data Type: Priority Queue • A priority queue is a collection of zero or more items, – associated with each item is a priority • A priority queue has at least three operations – insert(item i) (enqueue) a new item – delete() (dequeue) the member with the highest priority – find() the item with the highest priority – decrease. Priority(item i, p) decrease the priority of ith item to p • Note that in a priority queue "first in first out" does not apply in general. f 00 2

Priority Queues: Assumptions • The highest priority can be either the minimum value of all the items, or the maximum. – We will assume the highest priority is the minimum. – Call the delete operation delete. Min(). – Call the find operation find. Min(). • Assume the priority queue has n members f 00 3

Implementations • Heap. – In the worst case insert() is (lg n) and – delete. Min() is (lg n) – find. Min() is (1) – decrease. Key(i, p) is (lg n) 1, x 2, k 3, e 8, d f 00 7, i 9, z 4

Unsorted list: Array 1. Using an array arr. – insert() adds the new item into next empty position in arr, in (1). – find. Min() is (n) in the worst case – delete. Min() is (n) in the worst case • (n) to find the minimum item • and (1) to move the last item to the position of the deleted element. – Decrease. Priority(i, p) – decrease priority of ith item stored at arr[i] in (1) 4, x 8, a 1, b 0 f 00 1 2 9, c 7, y 3 4 5 5

Unsorted list: Linked List 2. Using a linked list. – insert() in (1) with appropriate pointers. – find. Min() is (n) since we may need to search the whole list. – delete. Min() is (n) • In the worst case we may need to search the whole list, (n) • Delete item, (1) f 00 6

Sorted list: Circular Array 9, x 0, b 4, c 1. A circular array A. 0 1 2 3 – insert() must maintain a sorted list. first • (n) in the worst case • For example: The new item needs to be inserted after the item with the highest priority. So n-1 items have to be moved to make room. – find. Min() is (1) – delete. Min() is (1) because the minimum item is the first one in the queue, and only the pointer to the first item needs to be changed. – Decrease. Priority(i, p) – decrease priority of ith item, and reinsert (n) f 00 7, y 4 7

Sorted list: Linked List 2. A linked list. – insert() is (n) • since in the worst case the whole list must be searched sequentially to find the location for insertion. – find. Min() is (1) – delete. Min is (1) • since with appropriate pointers the first element of a linked list can be deleted in (1). f 00 8

Priority Queue Implementations Data Structure Heap Unsorted insert Delete. Min worst case ( lg n) (1) (n) (1) (array or linked list) Sorted (array or linked list) f 00 9

Amortized costs f 00 10

Heaps f 00 Procedure Binary heap (worst-case) Binomial heap (worst-case) Insert (lg n) O(lg n) find. Min (1) O(lg n) delete. Min (lg n) O(lg n) merge (n) O(lg n) decrease. Key (lg n) O(lg n) 11
- Slides: 11