1 / 54

Lecture: Priority Queue

Lecture: Priority Queue. Questions. Is array a data structure? What is a data structure? What data structures are implemented by array? Priority queue (max --, min --). No! Why?. It is a standard part of algorithm. Stack, Queue, List, Heap, Max-heap, Min-heap, …. Contents.

banyan
Télécharger la présentation

Lecture: Priority Queue

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. Lecture: Priority Queue

  2. Questions • Is array a data structure? • What is a data structure? • What data structures are implemented by array? • Priority queue (max --, min --). No! Why? It is a standard part of algorithm Stack, Queue, List, Heap, Max-heap, Min-heap, …

  3. Contents • Recall: Heap, a data structure Max-heap (min-heap) (a) Max-Heapify procedure (b) Building a max-heap • Priority Queue • Implementation of Prim Algorithm

  4. Heap

  5. A Data Structure Heap • A heap is an array object that can be viewed as a nearly complete binary tree. 1 6 2 3 5 3 6 5 3 2 4 1 4 5 6 2 4 1 Tied with three procedures for finding Parent, finding left child, and finding Right child. All levels except last level are complete.

  6. Max-Heap (Min-Heap)

  7. Max-Heap

  8. Min-Heap

  9. Max-Heapify • Max-Heapify(A,i) is a subroutine. • Input: When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the max-heap property. • Output:Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-heap by letting A[i] “float down”.

  10. 4 14 7 4 14 7 2 8 1 2 8 1 14 8 7 2 1 4

  11. Running time

  12. Building a Max-Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

  13. 4 1 3 10 9 2 16 8 7 14 Proof.

  14. 4 1 3 10 9 2 16 8 7 7 14

  15. 4 1 3 10 9 2 16 8 7 14

  16. Building a Max-Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

  17. 4 1 3 10 9 2 16 8 7 14

  18. 4 1 3 10 9 2 16 8 7 14

  19. 4 1 3 10 9 16 14 2 8 7

  20. 4 1 3 10 9 14 16 2 8 7

  21. 4 1 10 3 9 14 16 2 8 7

  22. 4 1 10 3 9 14 16 2 8 7

  23. 4 16 10 3 9 14 1 2 8 7

  24. 4 16 10 3 9 14 7 2 8 1

  25. 16 4 10 3 9 14 7 2 8 1

  26. 16 14 10 3 9 4 7 2 8 1

  27. 16 14 10 3 9 7 8 2 4 1

  28. Analysis

  29. 16 14 10 3 9 7 8 2 4 1

  30. Running time

  31. Priority Queue

  32. Priority Queue • A priority queue is a data structure for maintaining a set of elements, each with an associated value, called a key. • A max-priority queue supports the following operations: Maximum(S), Extract-Max(S), Increase-Key(S,x,k), Insert(S,x). • Max-Heap can be used for implementing max-priority queue.

  33. Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7. Build a max-heap 16 14 10 3 9 7 8 2 4 1 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.

  34. 1 14 10 3 9 7 8 2 4 1, 14, 10, 8, 7, 9, 3, 2, 4

  35. 14 1 10 3 9 7 8 2 4 14, 1, 10, 8, 7, 9, 3, 2, 4

  36. 14 8 10 3 9 7 1 2 4 14, 8, 10, 1, 7, 9, 3, 2, 4

  37. 14 8 10 3 9 7 4 2 1 14, 8, 10, 4, 7, 9, 3, 2, 1

  38. 16 14 10 3 9 7 8 2 4 1 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.

  39. 16 14 10 3 9 7 8 2 1 15 16, 14, 10, 8, 7, 9, 3, 2, 15, 1.

  40. 16 14 10 3 9 7 15 2 1 8 16, 14, 10, 15, 7, 9, 3, 2, 8, 1.

  41. 16 14 10 3 9 7 15 2 1 8 16, 14, 10, 15, 7, 9, 3, 2, 8, 1.

  42. 16 15 10 3 9 7 14 2 1 8 16, 15, 10, 14, 7, 9, 3, 2, 8, 1.

  43. 16 14 10 3 9 7 8 2 4 1 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.

  44. 16 14 10 3 9 7 8 2 4 1 -∞ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1, -∞

More Related