1 / 16

Heaps

Heaps. Section 6.4, Pg. 309 (Section 9.1). T. How many times is this edge visited?. Motivation: Prim’s Algorithm. Starting node. 3 times. Problem : You keep comparing the same edges over and over. Solution : Use priority queues containing the edges. Priority Queues. Typical example:.

eden-best
Download Presentation

Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Heaps Section 6.4, Pg. 309 (Section 9.1)

  2. T How many times is this edge visited? Motivation: Prim’s Algorithm Starting node 3 times Problem: You keep comparing the same edges over and over. Solution: Use priority queues containing the edges

  3. Priority Queues • Typical example: printing in a Unix/Linux environment. Printing jobs have different priorities. These priorities override the FIFO policy of the queues • Operations supported in a priority queue: • Insert a new element • Extract/Delete of the element with the highest priority • The priority is a number

  4. Heap greater Implementing Priority Queues: min-HEAPS We assume that each element has a key, K, and other information, I. K is the priority of I. • A min-Heap is a binary tree T such that: • The priority of the element in any node is less or equal than the priority of the elements in its children nodes • T is complete Note: The algorithms for Heaps are the same as for min-Heaps. Just invert the comparisons.

  5. 10 > 9! Tree is not complete (non) Examples 5 5 5 8 8 8 10 9 9 16 16 10 12 9 56 12 16 10 12

  6. 5 8 9 12 56 16 10 22 44 13 18 20 Insert a New Element Idea: insert the new element as the last leaf and re-adjust the tree Insert 7

  7. 5 8 9 12 56 16 10 22 44 13 18 20 Insert a New Element (II) Step 1: add it as the last leaf 7

  8. Insert a New Element (III) Steps 2, 3 ,… : swap until key is in the right place 5 8 9 12 56 16 7 22 44 13 10 18 20

  9. Insert a New Element (IV) Steps 2, 3 ,… : swap until it finds its place 5 8 7 12 56 16 9 22 44 13 10 18 20 Complexity is O(log2 n)

  10. 5 8 9 12 56 16 10 22 44 13 18 20 Extract/Delete the Element with the Lowest Priority Idea: the root has always the lowest priority. Then delete it and replace it with the last child. Re-adjust the tree. Extract/Step 1: returns 5

  11. Extract/Delete the Element with the Lowest Priority (II) Step 2: Put the last key as the new root. 13 8 9 12 56 16 10 22 44 18 20

  12. Extract/Delete the Element with the Lowest Priority (III) Steps 3, 4, … : Swap until key is in the correct place. Always swap with node that has the lowest priority. 8 13 9 12 56 16 10 22 44 18 20

  13. Extract/Delete the Element with the Lowest Priority (IV) Steps 3, 4, … : Swap until key is in the correct place. Always swap with node that has the lowest priority. 8 12 9 13 56 16 10 22 44 18 20 Complexity is O(log2 n)

  14. 5 8 9 12 56 16 10 22 44 13 18 20 Complete Trees can be Represented in Arrays 0 1 2 … Corresponding array: 5 8 9 16 12 10 56 18 20 22 44 13

  15. Operations in Array Representation of Complete Trees Assume that the first index in the array is 0 and that the number of keys is n root(i): i = 0 LeftChild(i): 2i + 1 rightChild(i): 2i + 2 isLeaf(i): Homework Parent(i): Homework

  16. T The Fringe (neighbors of T) are maintained in a priority list (min-Heap), which ensures O((|E|+|V|)log2|E|) Using Priority Queues in Prim’s Algorithm Maintaining the Fringe is the crucial aspect of Djisktra’s Algorithm for computing shortest path between two points (next class)

More Related