Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011.

Similar presentations


Presentation on theme: "Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011."— Presentation transcript:

1 Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011

2 Definition ► The Priority Queue Data Structure ► stores elements with assigned priorities ► insertion of an object must include a priority level (key) ► removal of an object is based on the priority levels of the elements in the queue; the object whose level is smallest gets removed first ► Need an Entry interface/class ► Entry encapsulates a key and the object/record stored ► Could be an interface so the actual class containing the key and object implements this interface

3 PriorityQueue Interface public interface PriorityQueue { public int size(); public boolean isEmpty(); public Entry insert( int key, Object value) public Entry min() throws EmptyPriorityQueueException; public Entry removeMin() throws EmptyPriorityQueueException; } Could be an Object instead of int to allow for other key types; e.g. String

4 List-based Implementations ► Using either an array or a linked list, the elements can be stored as a sequence of entries ► The sequence of entries can be stored in the order the elements arrive (unsorted list implementation ) ► Or, sorted by key (sorted list implementation)

5 Unsorted List Implementation ► Insertion is done such that the incoming element is appended to the list ► An O( 1 ) operation ► Removal involves scanning the array or the linked list and determining the element with the minimum-valued key ► If using an array, elements need to be adjusted upon removal ► An O( n ) operation regardless because of the scan

6 Sorted List Implementation ► Insertion is done such that the incoming element is stored or inserted in its proper place (elements are sorted by key) ► An O( n ) operation ► Removal operation: ► If using an array, the sequence is stored in decreasing order, and removal involves returning the last element ► If using a linked list, the sequence is stored in increasing order, and removal involves returning the first element (head of the list) ► An O( 1 ) operation regardless

7 Using a Heap ► A heap is a (complete) binary tree of entries such that for every node except for the root, the node’s key is greater than or equal to its parent’s ► The root of a heap contains the element with the minimum-valued key (highest priority) ► Insertion and removal operations for a heap both run in O( log n ) time

8 Heap Data Structure (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C)

9 Heaps and BTs using Arrays ► Heaps are complete (all levels filled up except perhaps for last level) which makes an array implementation of a binary tree most appropriate ► But… we need to ensure that the completeness of binary tree stays even after insertion or removal of elements

10 Insertion into a Heap ► Add new element at the end of the array ► Compare the key of the newly added element with it’s parent’s key to check if heap property is observed ► If heap property is violated, swap element at last position with element at its parent ► repeat this process for the parent ► stop once heap property is satisfied ► In effect, the new element is “promoted” to its appropriate level

11 Inserting into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C)

12 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C) (2,T)

13 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C) (2,T)

14 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C) (2,T)

15 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(2,T) (5,A)(6,Z) (4,C) (20,B)

16 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(2,T) (5,A)(6,Z) (4,C) (20,B)

17 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(6,Z) (5,A)(2,T) (4,C) (20,B)

18 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(6,Z) (5,A)(2,T) (4,C) (20,B)

19 Insertion into a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(6,Z) (5,A)(4,C) (2,T) (20,B)

20 Removal from a Heap ► Vacate root position of the tree ► Element in the root to be returned by the method ► Get last element in the array, place it in the root position ► Compare this element’s key with the root’s children’s keys, and check if the heap property is observed ► If heap property is violated, swap root element with the child with minimum key value ► repeat process for the child position ► stop when heap property is satisfied

21 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C)

22 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (4,C)

23 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S)(13,W) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z)

24 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (13,W)

25 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (13,W)

26 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(9,F)(7,Q)(20,B) (5,A)(6,Z) (13,W)

27 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(9,F)(7,Q)(20,B) (13,W)(6,Z) (5,A)

28 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(9,F)(7,Q)(20,B) (13,W)(6,Z) (5,A)

29 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(13,W)(7,Q)(20,B) (9,F)(6,Z) (5,A)

30 Removal from a Heap (16,X)(25,J)(14,E)(12,H)(11,S) (15,K)(13,W)(7,Q)(20,B) (9,F)(6,Z) (5,A)

31 Removal from a Heap (16,X)(25,J)(14,E)(13,W)(11,S) (15,K)(12,H)(7,Q)(20,B) (9,F)(6,Z) (5,A)

32 Why O( log n )? ► Worst-case number of swaps is proportional to the height of the tree ► What is the height h of a binary tree with n nodes? ► If binary tree is complete: ► 2 h <= n < 2 h+1 ► log 2 h <= log n < log 2 h+1 ► h <= log n < h+1 ► h is O( log n ) ► Insertion and removal for a heap is O( log n )

33 Time Complexity Summary O( log n ) Heap O( 1 )O( n )Sorted List O( n )O( 1 )Unsorted List RemovalInsertionOperation

34 About Priority Queues ► Choose an implementation that fits the application’s requirements ► Note trade-off between insertion and removal time complexity ► Keys need not be integers ► Need the concept of a comparator (e.g., can’t use ≤ operator for String values ) ► Value of the key parameter in the insert method needs to be validated if the key is of type Object


Download ppt "Priority Queues CS 110: Data Structures and Algorithms First Semester, 2010-2011."

Similar presentations


Ads by Google