8 Priority Queues
Total Page:16
File Type:pdf, Size:1020Kb
8 Priority Queues A Priority Queue S is a dynamic set data structure that supports the following operations: ñ S.build(x1; : : : ; xn): Creates a data-structure that contains just the elements x1; : : : ; xn. ñ S.insert(x): Adds element x to the data-structure. ñ element S.minimum(): Returns an element x S with 2 minimum key-value key[x]. ñ element S.delete-min(): Deletes the element with minimum key-value from S and returns it. ñ boolean S.is-empty(): Returns true if the data-structure is empty and false otherwise. Sometimes we also have ñ S.merge(S ): S : S S ; S : . 0 = [ 0 0 = EADS © Ernst Mayr, Harald Räcke 301 8 Priority Queues An addressable Priority Queue also supports: ñ handle S.insert(x): Adds element x to the data-structure, and returns a handle to the object for future reference. ñ S.delete(h): Deletes element specified through handle h. ñ S.decrease-key(h; k): Decreases the key of the element specified by handle h to k. Assumes that the key is at least k before the operation. EADS 8 Priority Queues © Ernst Mayr, Harald Räcke 302 Dijkstra’s Shortest Path Algorithm Algorithm 18 Shortest-Path(G (V , E, d), s V) = ∈ 1: Input: weighted graph G (V , E, d); start vertex s; = 2: Output: key-field of every node contains distance from s; 3: S.build(); // build empty priority queue 4: for all v V s do ∈ \{ } 5: v. key ; ← ∞ 6: h S.insert(v); v ← 7: s. key 0; S.insert(s); ← 8: while S.is-empty() false do = 9: v S.delete-min(); ← 10: for all x V s.t. (v, x) E do ∈ ∈ 11: if x. key > v. key d(v, x) then + 12: S.decrease-key(h ,v. key d(v, x)); x + 13: x. key v. key d(v, x); ← + EADS 8 Priority Queues © Ernst Mayr, Harald Räcke 303 Prim’s Minimum Spanning Tree Algorithm Algorithm 19 Prim-MST(G (V , E, d), s V) = ∈ 1: Input: weighted graph G (V , E, d); start vertex s; = 2: Output: pred-fields encode MST; 3: S.build(); // build empty priority queue 4: for all v V s do ∈ \{ } 5: v. key ; ← ∞ 6: h S.insert(v); v ← 7: s. key 0; S.insert(s); ← 8: while S.is-empty() false do = 9: v S.delete-min(); ← 10: for all x V s.t. v, x E do ∈ { } ∈ 11: if x. key > d(v, x) then 12: S.decrease-key(hx,d(v, x)); 13: x. key d(v, x); ← 14: x. pred v; ← EADS 8 Priority Queues © Ernst Mayr, Harald Räcke 304 Analysis of Dijkstra and Prim Both algorithms require: ñ 1 build() operation ñ V insert() operations j j ñ V delete-min() operations j j ñ V is-empty() operations j j ñ E decrease-key() operations j j How good a running time can we obtain? EADS 8 Priority Queues © Ernst Mayr, Harald Räcke 305 8 Priority Queues Binary Binomial Fibonacci Operation BST Heap Heap Heap* build n n log n n log n n minimum 1 log n log n 1 is-empty 1 1 1 1 insert log n log n log n 1 delete log n** log n log n log n delete-min log n log n log n log n decrease-key log n log n log n 1 merge n n log n log n 1 Note that most applications use build() only to create an empty heap which then costs time 1. * Fibonacci heaps only give an ** The standard version of binary heaps is not address- amortized guarantee. able. Hence, it does not support a delete. EADS 8 Priority Queues © Ernst Mayr, Harald Räcke 306 8 Priority Queues Using Binary Heaps, Prim and Dijkstra run in time (( V E ) log V ). O j j + j j j j Using Fibonacci Heaps, Prim and Dijkstra run in time ( V log V E ). O j j j j + j j EADS 8 Priority Queues © Ernst Mayr, Harald Räcke 307 8.1 Binary Heaps ñ Nearly complete binary tree; only the last level is not full, and this one is filled from left to right. ñ Heap property: A node’s key is not larger than the key of one of its children. 7 9 15 17 11 31 19 25 43 13 12 80 EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 308 Binary Heaps Operations: ñ minimum(): return the root-element. Time (1). O ñ is-empty(): check whether root-pointer is null. Time (1). O EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 309 8.1 Binary Heaps Maintain a pointer to the last element x. ñ We can compute the predecessor of x (last element when x is deleted) in time (log n). O go up until the last edge used was a right edge. go left; go right until you reach a leaf if you hit the root on the way up, go to the rightmost element 7 9 15 17 11 31 19 25 43 13 12 80 x EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 310 8.1 Binary Heaps Maintain a pointer to the last element x. ñ We can compute the successor of x (last element when an element is inserted) in time (log n). O go up until the last edge used was a left edge. go right; go left until you reach a null-pointer. if you hit the root on the way up, go to the leftmost element; insert a new element as a left child; 7 9 15 17 11 31 19 25 43 13 12 80 x EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 311 Insert 1. Insert element at successor of x. 2. Exchange with parent until heap property is fulfilled. 7 9 14 17 11 15 19 25 43 13 12 80 311x x Note that an exchange can either be done by moving the data or by changing pointers. The latter method leads to an addressable priority queue. EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 312 Delete 1. Exchange the element to be deleted with the element e pointed to by x. 2. Restore the heap-property for the element e. 7 e 13 9 18 1716 12 19 25 43 27 201 x 13 x At its new position e may either travel up or down in the tree (but not both directions). EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 313 Binary Heaps Operations: ñ minimum(): return the root-element. Time (1). O ñ is-empty(): check whether root-pointer is null. Time (1). O ñ insert(k): insert at x and bubble up. Time (log n). O ñ delete(h): swap with x and bubble up or sift-down. Time (log n). O EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 314 Build Heap We can build a heap in linear time: 31 30 2 13 9 5 3 15 14 11 10 7 6 4 17 16 24 28 23 22 12 27 21 20 26 19 8 25 18 29 35 2` (h `) (2h) (n) · − = O = O levelsX ` EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 315 Binary Heaps Operations: ñ minimum(): Return the root-element. Time (1). O ñ is-empty(): Check whether root-pointer is null. Time (1). O ñ insert(k): Insert at x and bubble up. Time (log n). O ñ delete(h): Swap with x and bubble up or sift-down. Time (log n). O ñ build(x1; : : : ; xn): Insert elements arbitrarily; then do sift-down operations starting with the lowest layer in the tree. Time (n). O EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 316 Binary Heaps The standard implementation of binary heaps is via arrays. Let A[0; : : : ; n 1] be an array − i 1 ñ The parent of i-th element is at position − . b 2 c ñ The left child of i-th element is at position 2i 1. + ñ The right child of i-th element is at position 2i 2. + Finding the successor of x is much easier than in the description on the previous slide. Simply increase or decrease x. The resulting binary heap is not addressable. The elements don’t maintain their positions and therefore there are no stable handles. EADS 8.1 Binary Heaps © Ernst Mayr, Harald Räcke 317 8.2 Binomial Heaps Binary Binomial Fibonacci Operation BST Heap Heap Heap* build n n log n n log n n minimum 1 log n log n 1 is-empty 1 1 1 1 insert log n log n log n 1 delete log n** log n log n log n delete-min log n log n log n log n decrease-key log n log n log n 1 merge n n log n log n 1 EADS 8.2 Binomial Heaps © Ernst Mayr, Harald Räcke 318 Binomial Trees B0 B1 B2 B3 B4 Bt Bt 1 − Bt 1 − EADS 8.2 Binomial Heaps © Ernst Mayr, Harald Räcke 319 Binomial Trees Properties of Binomial Trees k ñ Bk has 2 nodes. ñ Bk has height k. ñ The root of Bk has degree k. ñ has k nodes on level . Bk ` ` ñ Deleting the root of Bk gives trees B0;B1;:::;Bk 1. − EADS 8.2 Binomial Heaps © Ernst Mayr, Harald Räcke 320 Binomial Trees B0 B1 B2 B3 B4 Deleting the root of B5 leaves sub-trees B4, B3, B2, B1, and B0.