Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science CS 330: Algorithms Priority Queues Gene Itkis.

Similar presentations


Presentation on theme: "Computer Science CS 330: Algorithms Priority Queues Gene Itkis."— Presentation transcript:

1 Computer Science CS 330: Algorithms Priority Queues Gene Itkis

2 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis2 Priority Queue (PQ) Definition (access methods)  Insert(x)  Inserts element x into the PQ  RemoveMin()  y  Returns the smallest element y in PQ (from all inserted and not yet removed)  Empty()  Returns TRUE if PQ is empty, FALSE otherwise  Optional  BuildPQ(List_of_Elements)  PQ  Returns PQ containing all elements in List_of_Elements  Merge(PQ 1,PQ 2 )  PQ  Returns PQ which contains all the elements of PQ 1 and PQ 2  MultiMerge(List_of_PQs)  PQ  Takes a list of PQs and merges all of them, i.e. returns PQ containing all the elements in all the PQs in the List_of_PQs

3 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis3 PQ Examples  Sequence for PQ:  Insert(5); Insert(8), Insert(3), Insert(6)  RemoveMin()  3  PQ now contains 5,8,6  Sequence for PQ’:  Insert(9); Insert(3), Insert(4), Insert(1)  Merge(PQ, PQ’)  PQ’’  PQ’’ contains 5,8,6,9,3,4,1

4 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis4 PQ Sort  PQSort(L)  BuildPQ  BuildPQ (L) Empty RemoveMin  while not PQ. Empty () do return PQ. RemoveMin ()

5 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis5 PQ implementations  d-Heaps  “Almost balanced” tree  each node has at most d children  usually, d=2  Costs:  O(lg n) - Insert, RemoveMin  O(n) - BuildHeap  Leftist Heaps [Tarjan]  Not balanced binary trees  Costs: Merge  O(lg n) - Insert, RemoveMin, Merge  O(n) - BuildHeap

6 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis6 Heap properties 1.Heap order:  Parent is always smaller than its child  Therefore, root contains MIN element 2.Heap shape:  2-heaps:  All leaves at “almost the same depth”  All leaves are at depth h or h-1;  Depth h leaves are to the left of the depth h-1 leaves  Leftist heaps:  “The path from any node to the closest descendent leaf is by following the right child”  Rank(v)= distance to the closest descendent leaf  Leftist heap: for any node Rank(left child) ≥ Rank(right child) h=? lg n

7 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis7 2-Heap  (see textbook and web for pseudo-code)  Insert(x) 1.Heap shape: initial position 2.Heap order: bubble towards the root O(lg n)  Cost: max 1compare(&swap) / level: O(lg n)  RemoveMin() 1.Heap shape: where to delete 2.Heap order: bubble down  Choose smaller of the children (to preserve heap order with the other child) O(lg n)  Cost: max 2compares(&swaps) / level: O(lg n) x y z y

8 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis8 2-Heap  BuildHeap  Read in the textbook (ours and/or cs112)!!!  Merge, MultiMerge  Not available  Can implement (e.g. removing elements from one and inserting them to the other) – but not efficiently

9 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis9 Leftist heaps 1.Heap order:  Parent is always smaller than its child 2.Heap shape:  Rank(v)= distance to the closest descendent leaf  Leftist heap: for any node Rank(left child) ≥ Rank(right child)  Technical “trick”:  Make all leaves =dummy  Then every non-leaf has exactly 2 children

10 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis10 Leftist heaps: example a h g b c 0 0 0 0 0 0 0 j : Dummy nodes 0: Ranks 1 1 1 2 3 2 : right path (always shortest path down to a leaf) i 0 1 d 0 2 f 0 1 right path length ≤ lg n

11 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis11 Leftist heaps: operations  Basic operation:  Merge  PQ.Insert(x):  Merge(PQ, x)  PQ.RemoveMin():  min  PQ.root()  PQ  Merge(PQ.Left, PQ.Right)  Return min  Costs:  Same as 1 Merge +O(1) x + z L R +

12 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis12 Leftist heaps: Merge  Heaps ~  sorted linked lists  (with stuff hanging off the list nodes) 1.Merge sorted linked lists  Lists are lg n long  So Merge takes O(lg n) steps 2.Fix ranks  If ranks are in the wrong order  Then swap the subtrees  Only the nodes on the merged path could be affected  So, this too takes O(lg n) steps + x y 5 z 7

13 Computer Science CS-330: Algorithms, Fall 2008Gene Itkis13 Summary  Both leftist and 2-heaps O(lg n) worst case  Insert(x), RemoveMin(): O(lg n) worst case  But leftist heaps also support: O(lg n) worst case  Merge: O(lg n) worst case  Your assignment: O(n)  Figure out O(n) MultiMerge (and BuildHeap) for both types: leftist and 2-heaps!  Use textbook, optional textbook (in library), course web  Heaps + PQ-Sort  Heapsort  One of the best sorting algorithms with O(n lg n) worst case bound  Assuming a good implementation of heaps, and use of BuildHeap


Download ppt "Computer Science CS 330: Algorithms Priority Queues Gene Itkis."

Similar presentations


Ads by Google