Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001."— Presentation transcript:

1 CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001

2 Binary Heap Priority Q Data Structure 201412911 81067 54 2 Heap-order property –parent’s key is less than children’s keys –result: minimum is always at the top Structure property –complete tree with fringe nodes packed to the left –result: depth is always O(log n); next open location always known

3 DeleteMin 201412911 81067 54 ? 2 201412911 81067 54 2 pqueue.deleteMin()

4 Percolate Down 201412911 81067 54 ? 201412911 81067 5? 4 201412911 810?7 56 4 201420911 810127 56 4

5 Finally… 1420911 810127 56 4

6 DeleteMin Code Object deleteMin() { assert(!isEmpty()); returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size+1]); Heap[newPos] = Heap[size + 1]; return returnVal; } int percolateDown(int hole, Object val) { while (2*hole <= size) { left = 2*hole; right = left + 1; if (right <= size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole; } runtime:

7 Insert 201412911 81067 54 2 201412911 81067 54 2 pqueue.insert(3) ?

8 Percolate Up 201412911 81067 54 2 ?201412911 8?67 54 2 10 201412911 8567 ?4 2 10201412911 8567 34 2 10 3 3 3

9 Insert Code void insert(Object o) { assert(!isFull()); size++; newPos = percolateUp(size,o); Heap[newPos] = o; } int percolateUp(int hole, Object val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole; } runtime:

10 Performance of Binary Heap In practice: binary heaps much simpler to code, lower constant factor overhead Binary heap worst case Binary heap avg case AVL tree worst case AVL tree avg case InsertO(log n)O(1) percolates 1.6 levels O(log n) Delete Min O(log n)

11 Changing Priorities In many applications the priority of an object in a priority queue may change over time –if a job has been sitting in the printer queue for a long time increase its priority –unix “renice” –Alon’s story Must have some (separate) way of find the position in the queue of the object to change (e.g. a hash table)

12 Other Priority Queue Operations decreaseKey –given the position of an object in the queue, reduce its priority value increaseKey –given the position of an an object in the queue, increase its priority value remove –given the position of an object in the queue, remove it buildHeap –given a set of items, build a heap

13 DecreaseKey, IncreaseKey, and Remove void decreaseKey(int pos, int delta){ temp = Heap[pos] - delta; newPos = percolateUp(pos, temp); Heap[newPos] = temp; } void increaseKey(int pos, int delta) { temp = Heap[pos] + delta; newPos = percolateDown(pos, temp); Heap[newPos] = temp; } void remove(int pos) { percolateUp(pos, NEG_INF_VAL); deleteMin(); }

14 BuildHeap Floyd’s Method. Thank you, Floyd. 511310694817212 pretend it’s a heap and fix the heap-order property! 27184 96103 115 12 Easy worst case bound: Easy average case bound:

15 Build(this)Heap 67184 92103 115 12 671084 9213 115 12 1171084 9613 25 12 1171084 9653 21 12

16 Finally… 11710812 9654 23 1 runtime?

17 Complexity of Build Heap Note: size of a perfect binary tree doubles (+1) with each additional layer At most n/4 percolate down 1 level at most n/8 percolate down 2 levels at most n/16 percolate down 3 levels… O(n)

18 Thinking about Heaps Observations –finding a child/parent index is a multiply/divide by two –operations jump widely through the heap –each operation looks at only two new nodes –inserts are at least as common as deleteMins Realities –division and multiplication by powers of two are fast –looking at one new piece of data terrible in a cache line –with huge data sets, disk accesses dominate

19 4 9654 23 1 81012 7 11 Solution: d-Heaps Each node has d children Still representable by array Good choices for d: –optimize performance based on # of inserts/removes –choose a power of two for efficiency –fit one set of children on a memory page/disk block 3728512111069112 What do d-heaps remind us of???

20 New Operation: Merge Given two heaps, merge them into one heap –first attempt: insert each element of the smaller heap into the larger. runtime: –second attempt: concatenate heaps’ arrays and run buildHeap. runtime: How about O(log n) time?

21 Idea: Hang a New Tree 1213106 115 2 + 1014 49 1 = 1213106 49115 12 ? 10 Now, just percolate down! Note: we just gave up the nice structural property on binary heaps!

22 Problem? Need some other kind of balance condition… 2 + 4 1 = 2 ? 12 15 18 4 1 12 15 18 15

23 Leftist Heaps Idea: make it so that all the work you have to do in maintaining a heap is in one small part Leftist heap: –almost all nodes are on the left –all the merging work is on the right

24 the null path length (npl) of a node is the number of nodes between it and a null in the tree Random Definition: Null Path Length npl(null) = -1 npl(leaf) = 0 npl(single-child node) = 0 000 001 11 2 0

25 Leftist Heap Properties Heap-order property –parent’s priority value is  to childrens’ priority values –result: minimum element is at the root Leftist property –null path length of left subtree is  npl of right subtree –result: tree is at least as “heavy” on the left as the right Are leftist trees complete? Balanced?

26 Leftist tree examples NOT leftistleftist 00 001 11 2 0 0 000 11 2 1 000 0 0 0 0 0 1 0 0 every subtree of a leftist tree is leftist, comrade!

27 Right Path in a Leftist Tree is Short If the right path has length at least r, the tree has at least 2 r - 1 nodes Proof by induction Basis: r = 1. Tree has at least one node: 2 1 - 1 = 1 Inductive step: assume true for r’ < r. The right subtree has a right path of at least r - 1 nodes, so it has at least 2 r - 1 - 1 nodes. The left subtree must also have a right path of at least r - 1 (otherwise, there is a null path of r - 3, less than the right subtree). Again, the left has 2 r - 1 - 1 nodes. All told then, there are at least: 2 r - 1 - 1 + 2 r - 1 - 1 + 1 = 2 r - 1 So, a leftist tree with at least n nodes has a right path of at most log n nodes 0 000 11 2 1 00

28 Merging Two Leftist Heaps merge(T 1,T 2 ) returns one leftist heap containing all elements of the two (distinct) leftist heaps T 1 and T 2 a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 recursive merge b L2L2 R2R2 R1R1

29 Merge Continued a L1L1 R’R’ R ’ = Merge(R 1, T 2 ) a R’R’ L1L1 npl(R ’ ) > npl(L 1 ) constant work at each merge; recursively traverse RIGHT right path of each tree; total work = O(log n)

30 Operations on Leftist Heaps merge with two trees of total size n: O(log n) insert with heap size n: O(log n) –pretend node is a size 1 leftist heap –insert by merging original heap with one node heap deleteMin with heap size n: O(log n) –remove and return root –merge left and right subtrees merge

31 Example 1210 5 87 3 14 1 00 1 00 0 merge 7 3 14 ? 0 0 1210 5 8 1 00 0 merge 10 5 ? 0 merge 12 8 0 0 8 0 0

32 Sewing Up the Example 8 12 0 0 10 5 ? 0 7 3 14 ? 0 0 8 12 0 0 10 5 1 0 7 3 14 ? 0 0 8 12 0 0 10 5 1 0 7 3 14 2 0 0 Done?

33 Finally… 8 12 0 0 10 5 1 0 7 3 14 2 0 0 7 3 2 0 0 8 12 0 0 10 5 1 0

34 Skew Heaps Problems with leftist heaps –extra storage for npl –two pass merge (with stack!) –extra complexity/logic to maintain and check npl Solution: skew heaps –blind adjusting version of leftist heaps –amortized time for merge, insert, and deleteMin is O(log n) –worst case time for all three is O(n) –merge always switches children when fixing right path –iterative method has only one pass What do skew heaps remind us of?

35 Merging Two Skew Heaps a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 merge b L2L2 R2R2 R1R1 Notice the old switcheroo!

36 Example 1210 5 87 3 14 merge 7 3 14 1210 5 8 merge 7 3 14 10 5 8 merge 12 7 3 14 10 8 5 12

37 Skew Heap Code void merge(heap1, heap2) { case { heap1 == NULL: return heap2; heap2 == NULL: return heap1; heap1.findMin() < heap2.findMin(): temp = heap1.right; heap1.right = heap1.left; heap1.left = merge(heap2, temp); return heap1; otherwise: return merge(heap2, heap1); }

38 Comparing Heaps Binary Heaps d-Heaps Binomial Queues Leftist Heaps Skew Heaps


Download ppt "CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001."

Similar presentations


Ads by Google