Computer-Science

Priority Queues and Heaps

1. Priority Queues

Priority Queue ADT

Example

Operation Output Priority Queue
insert(5) - {5}
insert(9) - {5, 9}
insert(2) - {2, 5, 9}
insert(7) - {2, 5, 7, 9}
min() [2] {2, 5, 7, 9}
removeMin() - {5, 7, 9}
size() 3 {5, 7, 9}
min() [5] {5, 7, 9}
removeMin() - {7, 9}
removeMin() - {9}
removeMin() - {}
empty() true {}
removeMin() "error" {}

List-based Priority Queue

Implementation with an unsorted list Implementation with a sorted list
Performance Performance
insert takes O(1) time since we can insert the item at the beginning or end of the sequence insert takes O(n) time since we have to find the place where to insert the item
removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key removeMin and min take O(1) time, since the smallest key is at the beginning

스크린샷 2021-12-13 오후 12 16 37

Priority Queue Sort

Algorithm PQ-Sort(L, P)
  Input List L, priority queue P
  Output Sorted list L
  while not L.empty()
    e  L.front()
    L.eraseFront()
    P.insert(e)
  while not P.empty()
    e  P.min()
    P.removeMin()
    L.insertBack(e)

Selection-Sort

Selection-Sort Example

  List L Priority Queue P (unsorted list)
Input: (7, 4, 8, 2, 5, 3, 9) ()
     
Phase 1    
(a) (4, 8, 2, 5, 3, 9) (7)
(b) (8, 2, 5, 3, 9) (7, 4)
..
(g) () (7, 4, 8, 2, 5, 3, 9)
     
Phase 2    
(a) (2) (7, 4, 8, 5, 3, 9)
(b) (2, 3) (7, 4, 8, 5, 9)
(c) (2, 3, 4) (7, 8, 5, 9)
(d) (2, 3, 4, 5) (7, 8, 9)
(e) (2, 3, 4, 5, 7) (8, 9)
(f) (2, 3, 4, 5, 7, 8) (9)
(g) (2, 3, 4, 5, 7, 8, 9) ()
Exercise
Result :
  List L Priority Queue P (unsorted list)
Input: (22, 15, 36, 44, 10, 3, 9) ()
     
Phase 1    
(a) (15, 36, 44, 10, 3, 9) (22)
(b) (36, 44, 10, 3, 9) (22, 15)
..
(g) () (22, 15, 36, 44, 10, 3, 9)
     
Phase 2    
(a) (3) (22, 15, 36, 44, 10, 9)
(b) (3, 9) (22, 15, 36, 44, 10)
(c) (3, 9, 10) (22, 15, 36, 44)
(d) (3, 9, 10, 15) (22, 36, 44)
(e) (3, 9, 10, 15, 22) (36, 44)
(f) (3, 9, 10, 15, 22, 36) (44)
(g) (3, 9, 10, 15, 22, 36, 44) ()

Insertion-sort

Insertion-sort Example

  List L Priority Queue P (sorted list)
Input: (7, 4, 8, 2, 5, 3, 9) ()
     
Phase 1    
(a) (4, 8, 2, 5, 3, 9) (7)
(b) (8, 2, 5, 3, 9) (4, 7)
..
(g) () (2, 3, 4, 5, 7, 8, 9)
     
Phase 2    
(a) (2) (7, 4, 8, 5, 3, 9)
(b) (2, 3) (7, 4, 8, 5, 9)
(c) (2, 3, 4) (7, 8, 5, 9)
(d) (2, 3, 4, 5) (7, 8, 9)
(e) (2, 3, 4, 5, 7) (8, 9)
(f) (2, 3, 4, 5, 7, 8) (9)
(g) (2, 3, 4, 5, 7, 8, 9) ()
Exercise
Result :
  List L Priority Queue P (unsorted list)
Input: (22, 15, 36, 44, 10, 3, 9) ()
     
Phase 1    
(a) (15, 36, 44, 10, 3, 9) (22)
(b) (36, 44, 10, 3, 9) (15, 22)
..
(g) () (3, 9, 10, 15, 22, 36, 44)
     
Phase 2    
(a) (3) (22, 15, 36, 44, 10, 9)
(b) (3, 9) (22, 15, 36, 44, 10)
(c) (3, 9, 10) (22, 15, 36, 44)
(d) (3, 9, 10, 15) (22, 36, 44)
(e) (3, 9, 10, 15, 22) (36, 44)
(f) (3, 9, 10, 15, 22, 36) (44)
(g) (3, 9, 10, 15, 22, 36, 44) ()

2. Heaps

Height of a Heap

스크린샷 2021-12-13 오후 2 12 16

Vector-based Heap Implementation

스크린샷 2021-12-13 오후 2 15 02

3. Priority Queues and Heaps

스크린샷 2021-12-13 오후 2 16 31

1) Insertion into a Heap

스크린샷 2021-12-13 오후 2 17 20

2) Upheap

스크린샷 2021-12-13 오후 2 26 51

스크린샷 2021-12-13 오후 3 32 52 스크린샷 2021-12-13 오후 3 33 17

3) Removal from a Heap

스크린샷 2021-12-13 오후 2 29 50

4) Downheap

스크린샷 2021-12-13 오후 2 32 13

스크린샷 2021-12-13 오후 2 32 45스크린샷 2021-12-13 오후 2 32 55

5) Merging Two Heaps

스크린샷 2021-12-13 오후 3 19 14

Heap-Sort

스크린샷 2021-12-13 오후 2 35 58

Priority Queue Summary

스크린샷 2021-12-13 오후 2 37 16

Bottom-up Heap Construction

스크린샷 2021-12-13 오후 3 41 38

Example

스크린샷 2021-12-13 오후 3 42 20 스크린샷 2021-12-13 오후 3 42 30

Analysis