1 / 11

Priority Queues

Priority Queues. Review the abstract data type Priority Queues Review different implementation options. 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

Download Presentation

Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Priority Queues Review the abstract data type Priority Queues Review different implementation options

  2. 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 • decreasePriority(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.

  3. 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 deleteMin(). • Call the find operation findMin(). • Assume the priority queue has n members

  4. 1,x 2,k 3,e 8,d 9,z 7,i Implementations • Heap. • In the worst case insert() is (lg n) and • deleteMin() is (lg n) • findMin() is (1) • decreaseKey(i, p) is (lg n)

  5. 4,x 8,a 7,y 9,c 1,b 0 4 1 2 3 5 Unsorted list: Array 1. Using an array arr. • insert() adds the new item into next empty position in arr, in (1). • findMin() is (n) in the worst case • deleteMin() 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. • DecreasePriority(i, p) – decrease priority of ith item stored at arr[i] in (1)

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

  7. 9,x 7,y 4,c 0,b 0 4 1 2 3 first Sorted list: Circular Array 1. A circular array A. • insert() must maintain a sorted list. • (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. • findMin() is (1) • deleteMin() 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. • DecreasePriority(i, p) – decrease priority of ith item, and reinsert (n)

  8. 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. • findMin() is (1) • deleteMin is (1) • since with appropriate pointers the first element of a linked list can be deleted in (1).

  9. Priority Queue Implementations Data insert DeleteMin Structure worst case worst case Heap ( lg n) ( lg n) Unsorted (array or linked list) (1) (n) Sorted (array or linked list) (n) (1)

  10. Amortized costs

  11. Heaps

More Related