130 likes | 278 Views
CS 330: Algorithms Priority Queues. Gene Itkis. 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
E N D
CS 330: AlgorithmsPriority Queues Gene Itkis
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(PQ1,PQ2) PQ • Returns PQ which contains all the elements of PQ1 and PQ2 • 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 Gene Itkis
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 Gene Itkis
PQ Sort • PQSort(L) • BuildPQ(L) • while not PQ.Empty() do return PQ.RemoveMin() Gene Itkis
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: • O(lg n) - Insert, RemoveMin, Merge • O(n) - BuildHeap Gene Itkis
Heap properties • Heap order: • Parent is always smaller than its child • Therefore, root contains MIN element • 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 Gene Itkis
2-Heap x z y y • (see textbook and web for pseudo-code) • Insert(x) • Heap shape: initial position • Heap order: bubble towards the root • Cost: max 1compare(&swap) / level: O(lg n) • RemoveMin() • Heap shape: where to delete • Heap order: bubble down • Choose smaller of the children(to preserve heap order with the other child) • Cost: max 2compares(&swaps) / level: O(lg n) Gene Itkis
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 Gene Itkis
Leftist heaps • Heap order: • Parent is always smaller than its child • 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 Gene Itkis
Leftist heaps: example 2 a 0: Ranks 1 3 : Dummy nodes b c 2 1 d : right path(always shortest path down to a leaf) 0 0 1 2 f 0 0 g h 1 1 0 0 i 0 0 j 0 0 right path length ≤ lg n Gene Itkis
Leftist heaps: operations z L R • 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 + Gene Itkis
Leftist heaps: Merge 7 5 z y • Heaps ~ • sorted linked lists • (with stuff hanging off the list nodes) • Merge sorted linked lists • Lists are lg n long • So Merge takes O(lg n) steps • 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 Gene Itkis
Summary • Both leftist and 2-heaps • Insert(x), RemoveMin(): O(lg n) worst case • But leftist heaps also support: • Merge: O(lg n) worst case • Your assignment: • 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 Gene Itkis