priority queues
play

Priority Queues 3rd October 2019 Priority Queues Binary heaps - PowerPoint PPT Presentation

Priority Queues 3rd October 2019 Priority Queues Binary heaps Leftist heaps Binomial heaps Fibonacci heaps Priority queues are important in, among other things, operating systems (process control in multitasking systems),


  1. Priority Queues 3rd October 2019

  2. Priority Queues • Binary heaps • Leftist heaps • Binomial heaps • Fibonacci heaps Priority queues are important in, among other things, operating systems (process control in multitasking systems), search algorithms (A, A*, D*, etc.), and simulation.

  3. Priority Queues Priority queues are data structures that hold elements with some kind of priority ( key ) in a queue-like structure, implementing the following operations: • insert() – Inserting an element into the queue. • deleteMin() – Removing the element with the highest priority. And maybe also: • buildHeap() – Build a queue from a set (>1) of elements. • increaseKey()/DecreaseKey() – Change priority. • delete() – Removing an element from the queue. • merge() – Merge two queues.

  4. Priority Queues An unsorted linked list can be used. insert() inserts an element at the head of the list ( O (1)), and deleteMin() searches the list for the element with the highest priority and removes it ( O ( n )). A sorted list can also be used (reversed running times). – Not very efficient implementations. To make an efficient priority queue, it is enough to keeps the elements “almost sorted”.

  5. Binary heaps A binary heap is organized as a complete binary tree. (All levels are full, except possibly the last.) In a binary heap the element in the root must have a key less than or equal to the key of its children, in addition each sub-tree must be a binary heap. 1 a 2 ë i / 2 û b c 3 d e f g 2 i 2 i+ 1 4 h i j a b c d e f g h i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4

  6. Binary heaps insert(14) 1 13 2 21 16 3 24 31 19 68 65 26 32 14 4 13 21 16 24 31 19 68 65 26 32 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4

  7. Binary heaps insert(14) 1 13 2 14 16 3 23 21 19 68 65 26 32 31 4 13 14 16 24 21 19 68 65 26 32 31 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 ” percolateUp() ”

  8. Binary heaps deleteMin() 1 2 14 16 3 19 21 19 68 65 26 32 31 4 14 16 19 21 19 68 65 26 32 31 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4

  9. Binary heaps deleteMin() 1 31 2 14 16 3 19 21 19 68 65 26 32 4 31 14 16 19 21 19 68 65 26 32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4

  10. Binary heaps deleteMin() 1 14 2 19 16 3 26 21 19 68 65 31 32 4 14 19 16 19 21 26 68 65 31 32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 ” percolateDown() ”

  11. Binary heaps worst case average O (log N ) O (1) insert() O (log N ) O (log N ) deleteMin() O ( N ) buildHeap() (Insert elements into the array unsorted, and run percolateDown() on each root in the resulting heap (the tree), bottom up) (The sum of the heights of a binary tree with N nodes is O( N ).) O ( N ) merge() ( N = number of elements)

  12. Leftist heaps To implement an efficient merge() , we move away from arrays, and implement so-called leftist heaps as pure trees. The idea is to make the heap (the tree) as skewed as possible, and do all the work on a short (right) branch, leaving the long (left) branch untouched. A leftist heap is still a binary tree with the heap structure (key in root is lower than key in children), but with an extra skewness requirement. For all nodes X in our tree, we define the null-path-length ( X ) as the distance from X to a descendant with less than two children ( i.e. 0 or 1). The skewness requirement is that for every node the null path length of its left child be at least as large as the null path length of the right child. For the empty tree we define the null-path-length to be -1, as a special case.

  13. Leftist heaps 1 1 0 0 0 LEFTIST 1 0 1 0 0 1 0 0 NOT LEFTIST

  14. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33

  15. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33

  16. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33 3 6 7 10 8 18 21 14 12 37 17 23 18 24 26 33

  17. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33 Flip L/R if not leftist 3 6 7 10 8 18 21 14 12 37 17 23 18 24 26 33

  18. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33 Flip L/R if not leftist 3 6 7 10 8 18 21 14 12 37 17 23 18 24 26 33

  19. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33 Flip L/R if not leftist 3 6 7 10 37 21 14 12 8 18 23 18 24 17 33 26

  20. Leftist heaps merge() 3 6 10 8 12 7 21 14 17 18 24 37 18 23 26 33 3 6 10 7 12 21 14 37 8 18 24 18 23 17 33 26

  21. Leftist heaps 5 3 insert(3) merge() 7 6 10 1 deleteMin() merge() 5 3 7 6 11 10

  22. Leftist heaps worst case O (log N ) merge() O (log N ) insert() O (log N ) deleteMin() O ( N ) buildHeap() ( N = number of elements) In a leftist heap with N nodes, the right path is at most ë log ( N +1) û long.

  23. Binomial heaps Leftist heaps: merge() , insert() and deleteMin() in O (log N ) time w.c. Binary heaps: insert() in O (1) time on average. Binomial heaps merge() , insert() og deleteMin() in O (log N ) time w.c. insert() O (1) time on average Binomial heaps are collections of trees (sometimes called a forest), each tree a heap.

  24. Binomial heaps Binomial trees B 0

  25. Binomial heaps Binomial trees B 0 B 1

  26. Binomial heaps Binomial trees B 0 B 1 B 2

  27. Binomial heaps Binomial trees B 0 B 1 B 2 B 3

  28. Binomial heaps Binomial trees B 0 B 1 B 2 B 3 B 4

  29. Binomial heaps Binomial trees B 0 B 1 B 2 B 3 B i = 2 x B i-1 , root of one tree connected as a child of the root of the other tree. A tree of height k has: 2 k nodes in total, B 4 æ k ö nodes on level d . ç ÷ d è ø

  30. Binomial heaps Binomial heap 16 12 18 21 24 65 Maximum one tree of each size: X 6 elements: 6 binary = 011 (0+2+4) B 0 B 1 B 2

  31. Binomial heaps Binomial heap 16 12 18 21 24 65 Maximum one tree of each size: X 6 elements: 6 binary = 011 (0+2+4) B 0 B 1 B 2 The length of the root list in a heap of N elements is O (log N ) . (Doubly linked, circular list.)

  32. Binomial heaps 16 12 13 14 23 merge() 18 21 24 26 51 24 65 65

  33. Binomial heaps 16 12 13 14 23 merge() 18 21 24 26 51 24 65 65

  34. Binomial heaps 16 12 13 14 23 merge() 18 21 24 26 51 24 65 65 13

  35. Binomial heaps 16 12 13 14 23 merge() 18 21 24 26 51 24 65 65 13 14 26 16 18

  36. Binomial heaps 16 12 13 14 23 merge() 18 21 24 26 51 24 65 65 13 23 12 51 24 21 24 14 16 65 65 26 18 The trees (the root list) is kept sorted on height.

  37. Binomial heaps deleteMin() 13 23 12 14 51 24 21 24 16 65 65 26 18

  38. Binomial heaps deleteMin() 13 23 12 14 51 24 21 24 16 65 65 26 18 merge() 14 13 23 21 24 16 51 24 65 26 18 65

  39. Binomial heaps worst case average case O (log N ) O (log N ) merge() O (log N ) O (1) insert() O (log N ) O (log N ) deleteMin() O ( N ) O ( N ) buildHeap() (Run N insert() on an initially empty heap.) ( N = number of elements)

  40. Binomial heaps Implementation Doubly linked, circular lists 13 23 12 51 21 24 14 24 65 65 26 16 18

  41. Binomial heaps Implementation Doubly linked, circular lists 13 23 12 51 21 24 14 24 65 65 26 16 18 23 61 27 88

  42. Binomial heaps Implementation Doubly linked, circular lists 13 23 12 51 21 24 14 24 65 65 26 16 18 23 61 27 88

  43. Fibonacci heaps Very elegant, and in theory efficient, way to implement heaps: Most operations have O (1) amortized running time. (Fredman & Tarjan ’87) insert() , decreaseKey() and merge() O (1) amortized time O (log N ) amortized time deleteMin() Combines elements from leftist heaps and binomial heaps. A bit complicated to implement, and certain hidden constants are a bit high. Best suited when there are few deleteMin() compared to the other operations. The data structure was developed for a shortest path algorithm (with many decreaseKey() operations), also used in spanning tree algorithms.

  44. Fibonacci heaps We include a smart decreaseKey() method from leftist heaps. 2 4 11 9 5 12 17 18 10 8 6 18 31 15 11 21

  45. Fibonacci heaps We include a smart decreaseKey() method from leftist heaps. 2 4 11 0 5 12 17 18 10 8 6 18 31 15 11 21

  46. Fibonacci heaps We include a smart decreaseKey() method from leftist heaps. 0 2 4 11 18 10 5 12 17 31 Leftist 8 6 18 15 11 21 Ikke leftist

  47. Fibonacci heaps We include a smart decreaseKey() method from leftist heaps. 0 2 4 11 18 10 5 12 17 31 Leftist 8 6 18 15 11 21 Ikke leftist

  48. Fibonacci heaps We include a smart decreaseKey() method from leftist heaps. 0 2 11 4 18 10 12 17 5 31 18 Leftist 8 6 15 11 21 Leftist

  49. Fibonacci heaps We include a smart decreaseKey() method from leftist heaps. 0 2 18 11 4 31 12 17 5 10 18 8 6 15 11 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend