priority queues 황승원 fall 2010 cse, postech 2 priority queues two kinds of priority queues: min...

62
Priority Queues 황황황 Fall 2010 CSE, POSTECH

Upload: paul-hart

Post on 21-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

Priority Queues

황승원Fall 2010

CSE, POSTECH

Page 2: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

2

Priority Queues

Two kinds of priority queues: Min priority queue. Max priority queue.

Page 3: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

3

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

Page 4: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

4

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

Page 5: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

5

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

Page 6: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

6

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)

Page 7: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

7

Application I: Sorting

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

elements into the sorted array from right to left.

Page 8: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

8

After Putting Into Max Priority Queue

Sorted Array

68

2

41 Max Priority

Queue

Page 9: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

9

After First Remove Max Operation

Sorted Array

6

2

41

8

Max Priority Queue

Page 10: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

10

After Second Remove Max Operation

Sorted Array

2

41

86

Max Priority Queue

Page 11: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

11

After Third Remove Max Operation

Sorted Array

21

864

Max Priority Queue

Page 12: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

12

After Fourth Remove Max Operation

Sorted Array

1

8642

Max Priority Queue

Page 13: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

13

After Fifth Remove Max Operation

Sorted Array

86421

Max Priority Queue

Page 14: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

14

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 of Chapter 2.

LATER: how do we implement this queue?

Page 15: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

15

Application II: 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

Page 16: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

16

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

Page 17: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

17

Machine Scheduling Example

Finish time = 21

Objective: Find schedules with minimum finish time.

A

B

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

6

2

3

7

13

13

21

Page 18: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

18

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.

Page 19: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

19

LPT Schedule

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

A

B

C

14

10

7 13

15

16

16

Finish time is 16!

Page 20: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

20

LPT Schedule

LPT rule does not guarantee minimum finish time schedules.

Minimum finish time scheduling is NP-hard. (LPT Finish Time)/(Minimum Finish Time) <= 4/3 - 1/(3m) where

m is number of machines. Usually LPT finish time is much closer to

minimum finish time.

Page 21: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

21

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.

Page 22: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

22

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.

Page 23: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

23

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) finis

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

Page 24: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

24

Using A Min Priority Queue

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.

Page 25: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

25

Using A Min Priority Queue

1 remove min and 1 put to schedule each job each put and remove min (machine) operation t

akes 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))

Page 26: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

26

Application III: Huffman Codes

Useful in lossless compression.

May be used in conjunction with LZW method.

See additional slides

Page 27: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

2727

Implementation of Priority Queue

Page 28: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

28

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.

Page 29: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

29

Min Tree Example

2

4 9 3

4 8 7

9 9

Root has minimum element.

Page 30: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

30

Max Tree Example

9

4 9 8

4 2 7

3 1

Root has maximum element.

Page 31: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

31

Min Heap Definition

complete binary tree min tree

Page 32: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

32

Min Heap With 9 Nodes

Complete binary tree with 9 nodes.

Page 33: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

33

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

Page 34: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

34

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

Page 35: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

35

Heap Height

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

Page 36: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

36

A Heap Is Efficiently Represented As An Array

9 8 7 6 7 2 6 5 1

1 2 3 4 5 6 7 8 9 100

9

8

6 7 2 6

5 1

7

Page 37: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

37

Moving Up And Down A Heap

9

8

6 7 2 6

5 1

7

1

2 3

4 5 6 7

8 9

Page 38: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

38

Putting An Element Into A Max Heap

Complete binary tree with 10 nodes.

9

8

6 7 2 6

5 1

7

7

Page 39: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

39

Putting An Element Into A Max Heap

New element is 5.

9

8

6 7 2 6

5 1

7

75

Page 40: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

40

Putting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

7

7

Page 41: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

41

Putting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

77

Page 42: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

42

Putting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

Page 43: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

43

Putting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

20

Page 44: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

44

Putting An Element Into A Max Heap

Complete binary tree with 11 nodes.

9

86

7

2 6

5 1

7

77

20

Page 45: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

45

Putting An Element Into A Max Heap

New element is 15.

9

86

7

2 6

5 1

7

77

20

Page 46: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

46

Putting An Element Into A Max Heap

New element is 15.

9

8

6

7

2 6

5 1

7

77

20

8

Page 47: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

47

Putting An Element Into A Max Heap

New element is 15.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 48: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

48

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

Page 49: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

49

Removing The Max Element

Max element is in the root.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 50: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

50

Removing The Max Element

After max element is removed.

8

6

7

2 6

5 1

7

77 8

9

15

Page 51: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

51

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.

Page 52: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

52

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 53: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

53

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 54: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

54

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

8

Page 55: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

55

Removing The Max Element

Max element is 15.

6

7

2 6

5 1

7

77

9

15

8

Page 56: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

56

Removing The Max Element

After max element is removed.

6

7

2 6

5 1

7

77

9

8

Page 57: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

57

Removing The Max Element

Heap with 9 nodes.

6

7

2 6

5 1

7

77

9

8

Page 58: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

58

Removing The Max Element

Reinsert 7.

6 2 6

5 1

79

8

Page 59: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

59

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

Page 60: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

60

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

7

Page 61: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

61

Complexity Of Remove Max Element

Complexity is O(log n).

6 2 6

5 1

7

9

8

7

Page 62: Priority Queues 황승원 Fall 2010 CSE, POSTECH 2 Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue

6262

Coming Up Next

READING: Ch 13.1~3, 13.6 NEXT: More on Heaps (13.4, 13.5)