Top Banner
Lecture 12 Priority Queue Slides modified from © 2010 Goodrich, Tamassia
101

Lecture 12 Priority Queue

May 01, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 12 Priority Queue

Lecture 12 Priority Queue

Slides modified from © 2010 Goodrich, Tamassia

Page 2: Lecture 12 Priority Queue

Arrays Linked Lists

Stacks Queues

Trees Algorithm Analysis

OOP

Page 3: Lecture 12 Priority Queue

Scheduler

– ready programs are added to the scheduler

– it decides which program to execute next

Page 4: Lecture 12 Priority Queue

SRTF Scheduling Policy

Schedule

45 62 77 10 25 36 23

Page 5: Lecture 12 Priority Queue

Main Operations

insert()

removeMin()

Page 6: Lecture 12 Priority Queue

Priority Queue ADT •  a collection of

entries •  entry = (key, value) •  main methods – insert(k, v) – removeMin()

•  additional methods – min() – size(), – isEmpty()

Page 7: Lecture 12 Priority Queue

Example

Page 8: Lecture 12 Priority Queue

Keys •  specific attribute of the element

•  many times assigned to the element

by user of application

•  key may change for the same

element, e.g. popularity

Page 9: Lecture 12 Priority Queue

Total Order Relations 1. Comparability property: either x ≤ y or y ≤ x 2. Antisymmetric property: x ≤ y and y ≤ x ⇒ x = y

3. Transitive property: x ≤ y and y ≤ z ⇒ x ≤ z

Page 10: Lecture 12 Priority Queue

• keys with total order – weight • keys not having total order – 2D point

Page 11: Lecture 12 Priority Queue

Comparator ADT •  implements isLess(p,q)

•  can derive other relations from this:

– (p == q)?

•  for STL, in C++ overload “()”

Page 12: Lecture 12 Priority Queue

Comparator Examples class LeftRight public:

bool operator()(Point2D& p, Point2D& q) return p.getX() < q.getX();

;

class BottomTop public:

bool operator()( Point2D& p, Point2D& q) return p.getY() < q.getY();

;

Page 13: Lecture 12 Priority Queue

Sort Sequence L with Priority Queue P while !L.empty ()

e ← L.front(); L.eraseFront() P.insert (e)

while !P.empty() e ← P.removeMin() L.insertBack(e)

Page 14: Lecture 12 Priority Queue

What is the time complexity of this

sorting?

Page 15: Lecture 12 Priority Queue

Implementation with sorted sequence

• insert()? • removeMin()?

1 2 3 4 5

Page 16: Lecture 12 Priority Queue

Insertion-Sort 1.  insert at right place to

keep the list sorted 2.  remove head repeatedly

Page 17: Lecture 12 Priority Queue

Insertion-Sort Example Sequence S Priority queue P

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) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9)

Phase 2

(a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. (g) (2,3,4,5,7,8,9) ()

Page 18: Lecture 12 Priority Queue

Time complexity

Best case?

Worst case?

Page 19: Lecture 12 Priority Queue

Implementation with unsorted sequence

• insert()? • removeMin()?

4 5 2 3 1

Page 20: Lecture 12 Priority Queue

Selection-Sort 1.  insert elements in

unsorted list 2.  remove min repeatedly

Page 21: Lecture 12 Priority Queue

Selection-Sort Example Sequence L Priority Queue P 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) ()

Page 22: Lecture 12 Priority Queue

What is time complexity

of selection sort?

Worst case?

Best case?

Page 23: Lecture 12 Priority Queue

Give two advantages of selection sort over others?

Page 24: Lecture 12 Priority Queue

Summary: either insert is O(n) or

removeMin() is O(n)!

Page 25: Lecture 12 Priority Queue

Can we do better?

Page 26: Lecture 12 Priority Queue

Lets Try BST

1 3

2

4

5 7

6

Page 27: Lecture 12 Priority Queue

Time complexity removeMin()?

insert()?

Page 28: Lecture 12 Priority Queue

Is O(h) good enough?

Page 29: Lecture 12 Priority Queue

Look at this tree…

1

3 4

5

2

7 6

BST? O(h)?

Page 30: Lecture 12 Priority Queue

BST Order Restrictions

1.  left subtree ≤ node 2.  right subtree ≥ node

Page 31: Lecture 12 Priority Queue

A simpler order restriction…

1. parent ≤ child

Page 32: Lecture 12 Priority Queue

Remake this tree with new order restriction…

1

3 4

5

2

7 6

Page 33: Lecture 12 Priority Queue

Heap 1. Order restriction:

K(parent) ≤ K(child) 2.  Structural restriction:

complete binary

Page 34: Lecture 12 Priority Queue

Height of Heap h ≤ log n, so, h is O(log n)

1 2

2h-1

1

keys 0 1 h-1 h

depth

Page 35: Lecture 12 Priority Queue

Heaps and Priority Queues •  Heap can be used to implement a priority

queue •  store a (key, element) item at each internal

node •  keep track of the last node (2, Chomu)

(6, Sohan) (5, Ram)

(9, Shyam) (7, Anna)

Page 36: Lecture 12 Priority Queue

The last node of a heap is the rightmost node of

maximum depth! 2

6 5 7 9

last node

Page 37: Lecture 12 Priority Queue

insert() 1. find insertion node 2. add new element (k)

at this node 3. restore heap order

Page 38: Lecture 12 Priority Queue

Finding the Insertion Node •  go up until node becomes a left child or the root

is reached •  if root is reached, go left until leaf node is reached •  if node becomes left child, go to the right sibling

and go down left until a leaf is reached

Page 39: Lecture 12 Priority Queue

time complexity of finding the insertion

node

Page 40: Lecture 12 Priority Queue

Insertion into a Heap

(14,E)

(5,A) (6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)

(4,C)

(2, T)

Page 41: Lecture 12 Priority Queue

up-heap bubbling

(2,T)

(5,A) (6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E)

(4,C)

Page 42: Lecture 12 Priority Queue

up-heap bubbling

(20,B)

(5,A) (6,Z)

(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E)

(2,T)

(4,C)

Page 43: Lecture 12 Priority Queue

up-heap bubbling

(2,T)

(5,A) (6,Z)

(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(4,C)

Page 44: Lecture 12 Priority Queue

up-heap bubbling

(2,T)(5,A)

(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(6,Z)

(4,C)

Page 45: Lecture 12 Priority Queue

up-heap bubbling

(6,Z)

(5,A)

(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(2,T)

(4,C)

Page 46: Lecture 12 Priority Queue

up-heap bubbling

(4,C)

(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(6,Z)

(2,T)

(5,A)

Page 47: Lecture 12 Priority Queue

up-heap bubbling

(6,Z)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(2,T)

(4,C)(5,A)

Page 48: Lecture 12 Priority Queue

Another View of Insertion

(6,Z)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(2,T)

(4,C)(5,A)

(2,T)

(5,A) (6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E)

(4,C)

Page 49: Lecture 12 Priority Queue

Correctness of Upheap

(2,T)

(5,A) (6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E)

(4,C)

Page 50: Lecture 12 Priority Queue

Always replaced with smaller key!

(6,Z)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E) (20,B)

(2,T)

(4,C)(5,A)

(2,T)

(5,A) (6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)(14,E)

(4,C)

Page 51: Lecture 12 Priority Queue

Since a heap has height O(log n), up-heap runs in

O(log n) time

Page 52: Lecture 12 Priority Queue

removeMin()

Page 53: Lecture 12 Priority Queue

Removal from a Heap

(14,E)

(5,A) (6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (13,W)(12,H)

(4,C)

(4, C)

Page 54: Lecture 12 Priority Queue

Removal from a Heap

(13,W)

(6,Z)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (12,H)(14,E)

(4,C)

(5,A)

Page 55: Lecture 12 Priority Queue

down-heap bubbling (13,W)

(14,E) (12,H)(25,J)(16,X) (11,S)

(15,K) (9,F) (7,Q) (20,B)

(6,Z)(5,A)

Page 56: Lecture 12 Priority Queue

down-heap bubbling (13,W)

(20,B)(7,Q)(9,F)(15,K)

(11,S)(16,X) (25,J) (12,H)(14,E)

(5,A) (6,Z)

Page 57: Lecture 12 Priority Queue

down-heap bubbling

(13,W)

(14,E) (12,H)(25,J)(16,X) (11,S)

(15,K) (9,F) (7,Q) (20,B)

(6,Z)

(5,A)

Page 58: Lecture 12 Priority Queue

down-heap bubbling

(9,F)

(20,B)(7,Q)(15,K)

(11,S)(16,X) (25,J) (12,H)(14,E)

(5,A)

(13,W)

(6,Z)

Page 59: Lecture 12 Priority Queue

down-heap bubbling

(13,W)

(14,E) (12,H)(25,J)(16,X) (11,S)

(15,K) (7,Q) (20,B)

(6,Z)

(5,A)

(9,F)

Page 60: Lecture 12 Priority Queue

down-heap bubbling

(13,W)

(20,B)(7,Q)(15,K)

(5,A)

(9,F)

(11,S)(14,E)(25,J)(16,X)

(12,H)

(6,Z)

Page 61: Lecture 12 Priority Queue

down-heap bubbling

(13,W)

(20,B)(7,Q)(15,K)

(5,A)

(9,F)

(12,H)

(11,S)(14,E)(25,J)(16,X)

(6,Z)

Page 62: Lecture 12 Priority Queue

Correctness of Downheap

(13,W)

(14,E) (12,H)(25,J)(16,X) (11,S)

(15,K) (9,F) (7,Q) (20,B)

(6,Z)(5,A)

Page 63: Lecture 12 Priority Queue

Since a heap has height O(log n), down-heap runs

in O(log n) time

Page 64: Lecture 12 Priority Queue

Keeping track of last node after

removeMin()

Page 65: Lecture 12 Priority Queue

Similar to finding node for insertion

X

Page 66: Lecture 12 Priority Queue

Vector Implementation of Complete Binary

Tree

Page 67: Lecture 12 Priority Queue

25 12 11 8 23 20

17715

6

16

5

14

4

9

Page 68: Lecture 12 Priority Queue

•  start at rank 1 •  for the node at rank i – the left child is at rank 2i – the right child is at rank 2i + 1

2

6 5

7 9

2 5 6 9 7

1 2 3 4 5 0

Page 69: Lecture 12 Priority Queue

insert() and removeMin() on

vector heap!

Page 70: Lecture 12 Priority Queue

How to build a heap?

Page 71: Lecture 12 Priority Queue

1. insert repeatedly O(n log n)

Page 72: Lecture 12 Priority Queue

(16, 15, 4 12, 6, 7, 23, 20, 25, 9, 11, 17, 5, 8, 14)

Build a heap with the below keys!

Page 73: Lecture 12 Priority Queue

2. Bottom-up Heap Construction

2i -1 2i -1

2i+1-1

Page 74: Lecture 12 Priority Queue

Building a Heap – Bottomup

415 12 6 7 23 2016

Page 75: Lecture 12 Priority Queue

Building a Heap – Bottomup

416 15

9

12 6 7

11

23

17

20

25

Page 76: Lecture 12 Priority Queue

Building a Heap – Bottomup

2016 25 9

4

12 11 7

6

23

1715

Page 77: Lecture 12 Priority Queue

Building a Heap – Bottomup

25 12 11 23 20

1715

16

8

4

9

5

6

7

Page 78: Lecture 12 Priority Queue

Building a Heap – Bottomup

25 12 11 23 20

1715

16 8

5

9

4 6

7

Page 79: Lecture 12 Priority Queue

Building a Heap – Bottomup

25 12 11 8 23 20

17715

6

16

5

14

4

9

Page 80: Lecture 12 Priority Queue

Building a Heap – Bottomup

25 12 11 8 23 20

17715

6

16 14

4

5

9

Page 81: Lecture 12 Priority Queue

in phase i, pairs of heaps with 2i -1 keys are merged into heaps with 2i+1-1 keys

Page 82: Lecture 12 Priority Queue

Analysis 1 -first right, then left until leaf

Page 83: Lecture 12 Priority Queue

Analysis 2 -sum of heights of all nodes

Lecture Notes CMSC 251

0 0 0 0 0 0

2*2

0 0*8

1*4

3*1

Total work for BuildHeap

2

0

1 1 1 1

2

3

Figure 13: Analysis of BuildHeap.

from the bottom there are 2h−j nodes, and each might sift down j levels. So, if we count from bottomto top, level-by-level, we see that the total time is proportional to

T (n) =h!

j=0

j2h−j =h!

j=0

j2h

2j.

If we factor out the 2h term, we have

T (n) = 2hh!

j=0

j

2j.

This is a sum that we have never seen before. We could try to approximate it by an integral, whichwould involve integration by parts, but it turns out that there is a very cute solution to this particularsum. We’ll digress for a moment to work it out. First, write down the infinite general geometric series,for any constant x < 1.

∞!

j=0

xj =1

1− x.

Then take the derivative of both sides with respect to x, and multiply by x giving:∞!

j=0

jxj−1 =1

(1− x)2

∞!

j=0

jxj =x

(1− x)2,

and if we plug x = 1/2, then voila! we have the desired formula:∞!

j=0

j

2j=

1/2(1− (1/2))2

=1/21/4

= 2.

In our case we have a bounded sum, but since the infinite series is bounded, we can use it instead as aneasy approximation.Using this we have

T (n) = 2hh!

j=0

j

2j≤ 2h

∞!

j=0

j

2j≤ 2h · 2 = 2h+1.

Now recall that n = 2h+1 − 1, so we have T (n) ≤ n + 1 ∈ O(n). Clearly the algorithm takes at leastΩ(n) time (since it must access every element of the array at least once) so the total running time forBuildHeap is Θ(n).

45

Page 84: Lecture 12 Priority Queue

j2h− jj=0

h

∑Lecture Notes CMSC 251

0 0 0 0 0 0

2*2

0 0*8

1*4

3*1

Total work for BuildHeap

2

0

1 1 1 1

2

3

Figure 13: Analysis of BuildHeap.

from the bottom there are 2h−j nodes, and each might sift down j levels. So, if we count from bottomto top, level-by-level, we see that the total time is proportional to

T (n) =h!

j=0

j2h−j =h!

j=0

j2h

2j.

If we factor out the 2h term, we have

T (n) = 2hh!

j=0

j

2j.

This is a sum that we have never seen before. We could try to approximate it by an integral, whichwould involve integration by parts, but it turns out that there is a very cute solution to this particularsum. We’ll digress for a moment to work it out. First, write down the infinite general geometric series,for any constant x < 1.

∞!

j=0

xj =1

1− x.

Then take the derivative of both sides with respect to x, and multiply by x giving:∞!

j=0

jxj−1 =1

(1− x)2

∞!

j=0

jxj =x

(1− x)2,

and if we plug x = 1/2, then voila! we have the desired formula:∞!

j=0

j

2j=

1/2(1− (1/2))2

=1/21/4

= 2.

In our case we have a bounded sum, but since the infinite series is bounded, we can use it instead as aneasy approximation.Using this we have

T (n) = 2hh!

j=0

j

2j≤ 2h

∞!

j=0

j

2j≤ 2h · 2 = 2h+1.

Now recall that n = 2h+1 − 1, so we have T (n) ≤ n + 1 ∈ O(n). Clearly the algorithm takes at leastΩ(n) time (since it must access every element of the array at least once) so the total running time forBuildHeap is Θ(n).

45

Page 85: Lecture 12 Priority Queue

sum of heights

O(n)

Page 86: Lecture 12 Priority Queue

How to merge two heaps?

Page 87: Lecture 12 Priority Queue

h1 h2

Page 88: Lecture 12 Priority Queue

h1

h2

Time Complexity??

Page 89: Lecture 12 Priority Queue

Merge two heaps in O(n)

Page 90: Lecture 12 Priority Queue

Min Heap

4 5

2

1

6 7

3

Page 91: Lecture 12 Priority Queue

Max Heap

4 3

6

7

2 1

5

Page 92: Lecture 12 Priority Queue

Recall Priority Queue ADT •  a collection of

entries •  entry = (key, value) •  main methods – insert(k, v) – removeMin()

•  additional methods – min() – size(), – isEmpty()

Page 93: Lecture 12 Priority Queue

Heap-Sort while !L.empty ()

e ← L.front(); L.eraseFront() P.insert (e)

while !P.empty() e ← P.removeMin() L.insertBack(e)

Page 94: Lecture 12 Priority Queue

Check if a given Binary Tree is Heap

Page 95: Lecture 12 Priority Queue

Converting min-heap into max-heap

Page 96: Lecture 12 Priority Queue

Implementing Heap-Sort In-Place

•  Use left side array for heap and right side array for list

•  move from left to right, and then right to left

Page 97: Lecture 12 Priority Queue

| 4 7 2 1 3 6 5 4 | 7 2 1 3 6 5 4 7 | 2 1 3 6 5 7 4 | 2 1 3 6 5

7 4 6 1 3 2 5|

What should be the next step?

Page 98: Lecture 12 Priority Queue

What is the time complexity?

n log n + n log n

Can we make it faster?

n + n log n

Page 99: Lecture 12 Priority Queue

Bottom-up Heap Construction for a Linked List implementation

-  BottomUpHeap(L): -  if L.empty() then return an empty heap -  e ← L.front() -  L.pop front() -  Split L into two lists, L1 and L2, each of size (n − 1)/2 -  T1 ← BottomUpHeap(L1) -  T2 ← BottomUpHeap(L2) -  Create binary tree T with root r storing e, left subtree T1, and

right subtree T2 -  Perform a down-heap bubbling from the root r of T , if

necessary -  return T

Page 100: Lecture 12 Priority Queue

How to find top k students who will be allowed to go for 6 month internship?

E.g. A = [8.1, 7.2, 7.5, 9, 9.8, 10, 5.4], k = 3

Output = [10, 9.8, 9]

•  Solution 1: Sort the numbers => O(n log n) •  Solution 2: Select min k times => O(nk) •  Solution 3: Build min-heap=> O(n+k log n) •  Solution 4: Build min-heap of first k elements, for

rest: –  if < root then ignore else replace root with the number

and heapify –  O(k + (n-k) log k)

•  More?

Page 101: Lecture 12 Priority Queue

A company has n items (w1..wn) and wants to make packages of minimum

weight W. Give a fast algorithms to do this in minimum number of steps.

•  Sort, add first two, sort again, stop when first element in greater than W –  O(xnlog n)

•  Create min heap, compare W with the root, if smaller, remove two elements, add them, and insert into the heap, repeat the procedure –  O(n+x log n)