112
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4, 2009 1

Priority Queues

Embed Size (px)

DESCRIPTION

Priority Queues. Two kinds of priority queues: Min priority queue. Max priority queue. Min Priority Queue. Collection of elements. Each element has a priority or key. Supports following operations: isEmpty size add/put an element into the priority queue get element with min priority - PowerPoint PPT Presentation

Citation preview

Page 1: Priority Queues

Priority Queues

Two kinds of priority queues:

• Min priority queue.

• Max priority queue.

Nov 4, 2009 1

Page 2: Priority Queues

Min Priority Queue

• Collection of elements.

• Each element has a priority or key.

• Supports following operations: isEmpty size add/put an element into the priority queue get element with min priority remove element with min priority

Nov 4, 2009 2

Page 3: Priority Queues

Max Priority Queue

• Collection of elements.

• Each element has a priority or key.

• Supports following operations: isEmpty size add/put an element into the priority queue get element with max priority remove element with max priority

Nov 4, 2009 3

Page 4: Priority Queues

Complexity Of Operations

Two good implementations are heaps and leftist trees.

isEmpty, size, and get => O(1) time

put and remove => O(log n) time where n is the size of the priority queue

Nov 4, 2009 4

Page 5: Priority Queues

Applications

Sorting

• use element key as priority

• put elements to be sorted into a priority queue

• extract elements in priority order if a min priority queue is used, elements are

extracted in ascending order of priority (or key) if a max priority queue is used, elements are

extracted in descending order of priority (or key)

Nov 4, 2009 5

Page 6: Priority Queues

Sorting Example

Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. Put the five elements into a max priority queue. Do five remove max operations placing removed

elements into the sorted array from right to left.

Nov 4, 2009 6

Page 7: Priority Queues

After Putting Into Max Priority Queue

Sorted Array

68

2

41 Max Priority

Queue

Nov 4, 2009 7

Page 8: Priority Queues

After First Remove Max Operation

Sorted Array

6

2

41

8

Max Priority Queue

Nov 4, 2009 8

Page 9: Priority Queues

After Second Remove Max Operation

Sorted Array

2

41

86

Max Priority Queue

Nov 4, 2009 9

Page 10: Priority Queues

After Third Remove Max Operation

Sorted Array

21

864

Max Priority Queue

Nov 4, 2009 10

Page 11: Priority Queues

After Fourth Remove Max Operation

Sorted Array

1

8642

Max Priority Queue

Nov 4, 2009 11

Page 12: Priority Queues

After Fifth Remove Max Operation

Sorted Array

86421

Max Priority Queue

Nov 4, 2009 12

Page 13: Priority Queues

Complexity Of Sorting

Sort n elements. n put operations => O(n log n) time. n remove max operations => O(n log n) time. total time is O(n log n). compare with O(n2) for sort methods

Nov 4, 2009 13

Page 14: Priority Queues

Heap Sort

Uses a max priority queue that is implemented as a heap.

Initial put operations are replaced by a heap initialization step that takes O(n) time.

Nov 4, 2009 14

Page 15: Priority Queues

Machine Scheduling m identical machines (drill press, cutter, sander,

etc.) n jobs/tasks to be performed assign jobs to machines so that the time at which

the last job completes is minimum

Nov 4, 2009 15

Page 16: Priority Queues

Machine Scheduling Example

3 machines and 7 jobs

job times are [6, 2, 3, 5, 10, 7, 14]

possible schedule

A

B

C

time ----------->

6

2

3

7

13

13

21

Nov 4, 2009 16

Page 17: Priority Queues

Machine Scheduling Example

Finish time = 21

Objective: Find schedules with minimum finish time.

A

B

Ctime ----------->

6

2

3

7

13

13

21

Nov 4, 2009 17

Page 18: Priority Queues

LPT Schedules

Longest Processing Time first.

Jobs are scheduled in the order14, 10, 7, 6, 5, 3, 2

Each job is scheduled on the machine on which it finishes earliest.

Nov 4, 2009 18

Page 19: Priority Queues

LPT Schedule

[14, 10, 7, 6, 5, 3, 2]

A

B

C

14

10

7 13

15

16

16

Finish time is 16!Nov 4, 2009 19

Page 20: Priority Queues

LPT Schedule

• LPT rule does not guarantee minimum finish time schedules.

• Usually LPT finish time is much closer to minimum finish time.

• Minimum finish time scheduling is NP-hard.

Nov 4, 2009 20

Page 21: Priority Queues

NP-hard Problems

• Infamous class of problems for which no one has developed a polynomial time algorithm.

• That is, no algorithm whose complexity is O(nk) for any constant k is known for any NP-hard problem.

• The class includes thousands of real-world problems.

• Highly unlikely that any NP-hard problem can be solved by a polynomial time algorithm.

Nov 4, 2009 21

Page 22: Priority Queues

NP-hard Problems• Since even polynomial time algorithms with

degree k > 3 (say) are not practical for large n, we must change our expectations of the algorithm that is used.

• Usually develop fast heuristics for NP-hard problems. Algorithm that gives a solution close to best. Runs in acceptable amount of time.

• LPT rule is good heuristic for minimum finish time scheduling.Nov 4, 2009 22

Page 23: Priority Queues

Complexity Of LPT Scheduling

• Sort jobs into decreasing order of task time. O(n log n) time (n is number of jobs)

• Schedule jobs in this order. assign job to machine that becomes available first must find minimum of m (m is number of machines)

finish times takes O(m) time using simple strategy so need O(mn) time to schedule all n jobs.

Nov 4, 2009 23

Page 24: Priority Queues

Using A Min Priority Queue

• Min priority queue has the finish times of the m machines.

• Initial finish times are all 0.

• To schedule a job remove machine with minimum finish time from the priority queue.

• Update the finish time of the selected machine and put the machine back into the priority queue.

Nov 4, 2009 24

Page 25: Priority Queues

Using A Min Priority Queue

• m put operations to initialize priority queue

• 1 remove min and 1 put to schedule each job

• each put and remove min operation takes O(log m) time

• time to schedule is O(n log m)

• overall time is

O(n log n + n log m) = O(n log (mn))Nov 4, 2009 25

Page 26: Priority Queues

Huffman Codes

Useful in lossless compression.

Nov 4, 2009 26

Page 27: Priority Queues

Min Tree Definition

Each tree node has a value.

Value in any node is the minimum value in the subtree for which that node is the root.

Equivalently, no descendent has a smaller value.

Nov 4, 2009 27

Page 28: Priority Queues

Min Tree Example

2

4 9 3

4 8 7

9 9

Root has minimum element.Nov 4, 2009 28

Page 29: Priority Queues

Max Tree Example

9

4 9 8

4 2 7

3 1

Root has maximum element.Nov 4, 2009 29

Page 30: Priority Queues

Min Heap Definition

• complete binary tree

• min tree

Nov 4, 2009 30

Page 31: Priority Queues

Min Heap With 9 Nodes

Complete binary tree with 9 nodes.

Nov 4, 2009 31

Page 32: Priority Queues

Min Heap With 9 Nodes

Complete binary tree with 9 nodes that is also a min tree.

2

4

6 7 9 3

8 6

3

Nov 4, 2009 32

Page 33: Priority Queues

Max Heap With 9 Nodes

Complete binary tree with 9 nodes that is also a max tree.

9

8

6 7 2 6

5 1

7

Nov 4, 2009 33

Page 34: Priority Queues

Heap Height

Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

Nov 4, 2009 34

Page 35: Priority Queues

9 8 7 6 7 2 6 5 1

1 2 3 4 5 6 7 8 9 100

A Heap Is Efficiently Represented As An Array

9

8

6 7 2 6

5 1

7

Nov 4, 2009 35

Page 36: Priority Queues

Moving Up And Down A Heap9

8

6 7 2 6

5 1

7

1

2 3

4 5 6 7

8 9

Nov 4, 2009 36

Page 37: Priority Queues

Putting An Element Into A Max Heap

Complete binary tree with 10 nodes.

9

8

6 7 2 6

5 1

7

7

Nov 4, 2009 37

Page 38: Priority Queues

Putting An Element Into A Max Heap

New element is 5.

9

8

6 7 2 6

5 1

7

75

Nov 4, 2009 38

Page 39: Priority Queues

Putting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

7

7

Nov 4, 2009 39

Page 40: Priority Queues

Putting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

77

Nov 4, 2009 40

Page 41: Priority Queues

Putting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

Nov 4, 2009 41

Page 42: Priority Queues

Putting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

20

Nov 4, 2009 42

Page 43: Priority Queues

Putting An Element Into A Max Heap

Complete binary tree with 11 nodes.

9

86

7

2 6

5 1

7

77

20

Nov 4, 2009 43

Page 44: Priority Queues

Putting An Element Into A Max Heap

New element is 15.

9

86

7

2 6

5 1

7

77

20

Nov 4, 2009 44

Page 45: Priority Queues

Putting An Element Into A Max Heap

New element is 15.

9

8

6

7

2 6

5 1

7

77

20

8

Nov 4, 2009 45

Page 46: Priority Queues

Putting An Element Into A Max Heap

New element is 15.

8

6

7

2 6

5 1

7

77

20

8

9

15

Nov 4, 2009 46

Page 47: Priority Queues

Complexity Of Put

Complexity is O(log n), where n is heap size.

8

6

7

2 6

5 1

7

77

20

8

9

15

Nov 4, 2009 47

Page 48: Priority Queues

Removing The Max Element

Max element is in the root.

8

6

7

2 6

5 1

7

77

20

8

9

15

Nov 4, 2009 48

Page 49: Priority Queues

Removing The Max Element

After max element is removed.

8

6

7

2 6

5 1

7

77 8

9

15

Nov 4, 2009 49

Page 50: Priority Queues

Removing The Max Element

Heap with 10 nodes.

8

6

7

2 6

5 1

7

77 8

9

15

Reinsert 8 into the heap.Nov 4, 2009 50

Page 51: Priority Queues

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Nov 4, 2009 51

Page 52: Priority Queues

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Nov 4, 2009 52

Page 53: Priority Queues

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

8

Nov 4, 2009 53

Page 54: Priority Queues

Removing The Max Element

Max element is 15.

6

7

2 6

5 1

7

77

9

15

8

Nov 4, 2009 54

Page 55: Priority Queues

Removing The Max Element

After max element is removed.

6

7

2 6

5 1

7

77

9

8

Nov 4, 2009 55

Page 56: Priority Queues

Removing The Max Element

Heap with 9 nodes.

6

7

2 6

5 1

7

77

9

8

Nov 4, 2009 56

Page 57: Priority Queues

Removing The Max Element

Reinsert 7.

6 2 6

5 1

79

8

Nov 4, 2009 57

Page 58: Priority Queues

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

Nov 4, 2009 58

Page 59: Priority Queues

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

7

Nov 4, 2009 59

Page 60: Priority Queues

Complexity Of Remove Max Element

Complexity is O(log n).

6 2 6

5 1

7

9

8

7

Nov 4, 2009 60

Page 61: Priority Queues

Initializing A Max Heap

input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

8

4

7

6 7

8 9

3

710

1

11

5

2

Nov 4, 2009 61

Page 62: Priority Queues

Initializing A Max Heap

Start at rightmost array position that has a child.

8

4

7

6 7

8 9

3

710

1

11

5

2

Index is n/2.Nov 4, 2009 62

Page 63: Priority Queues

Initializing A Max Heap

Move to next lower array position.

8

4

7

6 7

8 9

3

710

1

5

11

2

Nov 4, 2009 63

Page 64: Priority Queues

Initializing A Max Heap

8

4

7

6 7

8 9

3

710

1

5

11

2

Nov 4, 2009 64

Page 65: Priority Queues

Initializing A Max Heap

8

9

7

6 7

8 4

3

710

1

5

11

2

Nov 4, 2009 65

Page 66: Priority Queues

Initializing A Max Heap

8

9

7

6 7

8 4

3

710

1

5

11

2

Nov 4, 2009 66

Page 67: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

2

Nov 4, 2009 67

Page 68: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

2

Nov 4, 2009 68

Page 69: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

Find a home for 2.Nov 4, 2009 69

Page 70: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

75

1

11

Find a home for 2.

10

Nov 4, 2009 70

Page 71: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

1

11

Done, move to next lower array position.

10

5

Nov 4, 2009 71

Page 72: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

1

11

10

5

Find home for 1.Nov 4, 2009 72

Page 73: Priority Queues

11

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

10

5

Find home for 1.Nov 4, 2009 73

Page 74: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Find home for 1.Nov 4, 2009 74

Page 75: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Find home for 1.Nov 4, 2009 75

Page 76: Priority Queues

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Done.

1

Nov 4, 2009 76

Page 77: Priority Queues

Time Complexity

87

6 3

4

7

710

11

5

2

9

8

1

Height of heap = h.

Number of subtrees with root at level j is <= 2 j-1.

Time for each subtree is O(h-j+1).Nov 4, 2009 77

Page 78: Priority Queues

Complexity

Time for level j subtrees is <= 2j-1(h-j+1) = t(j).

Total time is t(1) + t(2) + … + t(h-1) = O(n).

Nov 4, 2009 78

Page 79: Priority Queues

Leftist Trees

Linked binary tree.

Can do everything a heap can do and in the same asymptotic complexity.

Can meld two leftist tree priority queues in O(log n) time.

Nov 4, 2009 79

Page 80: Priority Queues

Extended Binary Trees

Start with any binary tree and add an external node wherever there is an empty subtree.

Result is an extended binary tree.

Nov 4, 2009 80

Page 81: Priority Queues

A Binary Tree

Nov 4, 2009 81

Page 82: Priority Queues

An Extended Binary Tree

number of external nodes is n+1Nov 4, 2009 82

Page 83: Priority Queues

The Function s()

For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

Nov 4, 2009 83

Page 84: Priority Queues

s() Values Example

Nov 4, 2009 84

Page 85: Priority Queues

s() Values Example

0 0 0 0

0 0

0 0

0

0

1 1 1

2 1 1

2 1

2

Nov 4, 2009 85

Page 86: Priority Queues

Properties Of s()

If x is an external node, then s(x) = 0.

Otherwise,s(x) = min {s(leftChild(x)),

s(rightChild(x))} + 1

Nov 4, 2009 86

Page 87: Priority Queues

Height Biased Leftist Trees

A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))

Nov 4, 2009 87

Page 88: Priority Queues

A Leftist Tree

0 0 0 0

0 0

0 0

0

0

1 1 1

2 1 1

2 1

2

Nov 4, 2009 88

Page 89: Priority Queues

Leftist Trees--Property 1

In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).

Nov 4, 2009 89

Page 90: Priority Queues

A Leftist Tree

0 0 0 0

0 0

0 0

0

0

1 1 1

2 1 1

2 1

2

Length of rightmost path is 2.Nov 4, 2009 90

Page 91: Priority Queues

Leftist Trees—Property 2

The number of internal nodes is at least2s(root) - 1

Because levels 1 through s(root) have no external nodes.

So, s(root) <= log(n+1)

Nov 4, 2009 91

Page 92: Priority Queues

A Leftist Tree

0 0 0 0

0 0

0 0

0

0

1 1 1

2 1 1

2 1

2

Levels 1 and 2 have no external nodes.Nov 4, 2009 92

Page 93: Priority Queues

Leftist Trees—Property 3

Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree.

Follows from Properties 1 and 2.

Nov 4, 2009 93

Page 94: Priority Queues

Leftist Trees As Priority Queues

Min leftist tree … leftist tree that is a min tree.

Used as a min priority queue.

Max leftist tree … leftist tree that is a max tree.

Used as a max priority queue.

Nov 4, 2009 94

Page 95: Priority Queues

A Min Leftist Tree

8 6 9

6 8 5

4 3

2

Nov 4, 2009 95

Page 96: Priority Queues

Some Min Leftist Tree Operations

put()

remove()

meld()

initialize()

put() and remove() use meld().

Nov 4, 2009 96

Page 97: Priority Queues

Put Operation

put(7)

8 6 9

6 8 5

4 3

2

Nov 4, 2009 97

Page 98: Priority Queues

Put Operation

put(7)

8 6 9

6 8 5

4 3

2

Create a single node min leftist tree. 7

Nov 4, 2009 98

Page 99: Priority Queues

Put Operation

put(7)

8 6 9

6 8 5

4 3

2

Create a single node min leftist tree.

Meld the two min leftist trees.

7

Nov 4, 2009 99

Page 100: Priority Queues

Remove Min

8 6 9

6 8 5

4 3

2

Nov 4, 2009 100

Page 101: Priority Queues

Remove Min

8 6 9

6 8 5

4 3

2

Remove the root.

Nov 4, 2009 101

Page 102: Priority Queues

Remove Min

8 6 9

6 8 5

4 3

2

Remove the root.

Meld the two subtrees.Nov 4, 2009 102

Page 103: Priority Queues

Meld Two Min Leftist Trees

8 6 9

6 8 5

4 3

6

Traverse only the rightmost paths so as to get logarithmic performance.

Nov 4, 2009 103

Page 104: Priority Queues

Meld Two Min Leftist Trees

8 6 9

6 8 5

4 3

6

Meld right subtree of tree with smaller root and all of other tree.

Nov 4, 2009 104

Page 105: Priority Queues

Meld Two Min Leftist Trees

8 6 9

6 8 5

4 3

6

Meld right subtree of tree with smaller root and all of other tree.

Nov 4, 2009 105

Page 106: Priority Queues

Meld Two Min Leftist Trees

8 6

6 8

4 6

Meld right subtree of tree with smaller root and all of other tree.

Nov 4, 2009 106

Page 107: Priority Queues

Meld Two Min Leftist Trees

8 6

Meld right subtree of tree with smaller root and all of other tree.

Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree.Nov 4, 2009 107

Page 108: Priority Queues

Meld Two Min Leftist Trees

Swap left and right subtree if s(left) < s(right).

Make melded subtree right subtree of smaller root.

8 6

6

8

6

8Nov 4, 2009 108

Page 109: Priority Queues

Meld Two Min Leftist Trees

8 6

6 6

4

88 6

6

46

8

Make melded subtree right subtree of smaller root.

Swap left and right subtree if s(left) < s(right).Nov 4, 2009 109

Page 110: Priority Queues

Meld Two Min Leftist Trees

9

5

3

Swap left and right subtree if s(left) < s(right).

Make melded subtree right subtree of smaller root.

8 6

6 6

4

8

Nov 4, 2009 110

Page 111: Priority Queues

Meld Two Min Leftist Trees

9

5

3

8 6

6 6

4

8

Nov 4, 2009 111

Page 112: Priority Queues

Initializing In O(n) Time• create n single node min leftist trees

and place them in a FIFO queue

• repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue

• the process terminates when only 1 min leftist tree remains in the FIFO queue

• analysis is the same as for heap initialization

Nov 4, 2009 112