cs171 introduction to computer science ii priority queues
play

CS171 Introduction to Computer Science II Priority Queues and Binary - PowerPoint PPT Presentation

CS171 Introduction to Computer Science II Priority Queues and Binary Heap Priority Queues and Binary Heap Review Binary Search Trees (BST) Balanced search trees Hash tables Priority Queues Need to process/search an item with


  1. CS171 Introduction to Computer Science II Priority Queues and Binary Heap Priority Queues and Binary Heap

  2. Review � Binary Search Trees (BST) � Balanced search trees � Hash tables

  3. Priority Queues � Need to process/search an item with largest (smallest) key, but not necessarily full sorted order � Support two operations � Support two operations � Remove maximum (or minimum) � Insert � Similar to � Stacks (remove newest) � Queues (remove oldest)

  4. Example

  5. Applications � Job scheduling � Keys corresponds to priorities of the tasks � Sorting algorithm � Heapsort � Heapsort � Graph algorithms � Shortest path � Statistics � Maintain largest M values in a sequence

  6. Possible implementations � Sorting N items � Time: NlogN � Space: N � Elementary PQ - Compare each new key � Elementary PQ - Compare each new key against M largest seen so far � Time: NM � Space: M � Using an efficient MaxPQ Implementation

  7. Implementations � Elementary representations � Unordered array (lazy approach) � ordered array (eager approach) � Efficient implementation � Efficient implementation � Binary heap structure � Can we implement priority queue using Binary Search Trees?

  8. Sequence-based Priority Queue � Implementation with an � Implementation with a unsorted list sorted list 4 5 2 3 1 1 2 3 4 5 � Performance: � Performance: � Performance: � Performance: � insert takes � ��� time since � insert takes � � � � time since we can insert the item at we have to find the place the beginning or end of the sequence where to insert the item � removeMin and min take � removeMin and min take � � � � time since we have to � ��� time, since the traverse the entire smallest key is at the sequence to find the beginning smallest key 14

  9. Binary Heap Tree � A heap is a binary tree storing keys at its nodes and satisfying two � properties: � Heap-Order: for every internal node v � � other than the root, ��� � � � ≥ ��� � ������ � � �� ��� � � � ≥ ��� � ������ � � �� � � � Complete Binary Tree: let � be the height of the heap � for � = ������� �� − �� there are � � nodes of depth � � at depth � − � , the internal nodes are last node to the left of the external nodes � The last node of a heap is the rightmost node of depth ��� 17

  10. Height of a Heap � Theorem: A heap storing � keys has height � ����� � � Proof: (we apply the complete binary tree property) � Let � be the height of a heap storing �� keys � Since there are � � keys at depth � = ������� �� − �� and at least one key at depth � , we have � ≥ �� + �� + �� + �� + � � − �� + � � Thus, � ≥ � � , i.e., � ≤ ���� � � Thus, � ≥ � � , i.e., � ≤ ���� � depth keys � � � � � − � � � − � � � 19

  11. Insert/Remove and Maintaining Heap order � When a node’s key is larger than its parent key � Upheap (promote, swim) � When a node’s key becomes smaller than its children’s keys children’s keys � Downheap (demote, sink)

  12. Demo

Recommend


More recommend