Chapter 6: Priority Queues (Heaps)

12
Chapter 6: Priority Queues (Hea •Priority Queue ADT •Heap Implementation CS 340 Page 1 •Heap Applications •Leftist Heaps

description

Chapter 6: Priority Queues (Heaps). Priority Queue ADT. Heap Implementation. Heap Applications. Leftist Heaps. CS 340. Page 100. Priority Queues. - PowerPoint PPT Presentation

Transcript of Chapter 6: Priority Queues (Heaps)

Page 1: Chapter 6: Priority Queues (Heaps)

Chapter 6: Priority Queues (Heaps)• Priority Queue ADT• Heap Implementation

CS 340 Page 1

• Heap Applications• Leftist Heaps

Page 2: Chapter 6: Priority Queues (Heaps)

CS 340 Page 2

Often, a FIFO queue structure has a need for a prioritization capability, so elements with greater priority are removed before lower-priority elements that were actually inserted first.Examples:• Short print jobs may be prioritized

over longer print jobs on a printer queue.

• Real-time network applications (e.g., video, audio) may be prioritized over e-mail and simple file transfers on a network switch’s forwarding queue.

• System maintenance tasks (e.g., memory defragmentation, mouse interrupts) may be prioritized over application software tasks on an operating system’s job queue.

Priority Queues

Page 3: Chapter 6: Priority Queues (Heaps)

CS 340 Page 3

A heap is a complete binary tree in which every node’s value is less than or equal to all of its offsprings’ values.

One Priority Queue Implementation: The Heap

10

17

25 31

45 61 58 47

13

15 30

23

Note:One convenient aspect of the heap is that it can be stored in an array.

10

17

13

25

31

15

30

45

61

58

47

23

Offspring of node i: nodes 2i+1 and 2i+2

Parent of node i: node (i - 1) / 2

Page 4: Chapter 6: Priority Queues (Heaps)

CS 340 Page 4

When inserting a new element into a heap, start at the new leaf node that would maintain the binary tree’s completeness, and “percolate” the new element up to its appropriate position to maintain the heap order property.

Inserting Into A Heap

10

17

25

31

45

61

58

47

13

15

30

23

10

17

25

31

45

61

58

47

13

15

30

23

12

Insert 12

10

17

25

31

45

61

58

47

13

12

30

23

15

Percolate Up10

17

25

31

45

61

58

47

12

13

30

23

15

Percolate Up

Page 5: Chapter 6: Priority Queues (Heaps)

CS 340 Page 5

When deleting the minimum element from a heap, create a “hole” node at the root (where the minimum element was), and slide the smaller of the hole’s offspring up until an appropriate slot is found for the last element.

Deleting From A Heap

29

43

65

58

75

87

80

91

51

63

77

73

Delete Min

Percolate Down

43

65

58

75

87

80

91

51

63

77

73

43

65

58

75

87

80

91

51

63

77

73

43

65

73

75

87

80

91

51

63

77

58

Percolate Down

Page 6: Chapter 6: Priority Queues (Heaps)

CS 340 Page 6

Rather than going to the expense of implementing a complicated system, sometimes it is possible to simulate the system using a statistical model, and to work out the obvious bugs prior to actual implementation.A heap makes a convenient structure in such simulations, where the heap nodes represent the discrete “events” of the system, ordered according to the time at which they “occur”.

Example Application: Discrete Event Simulation

TOKENRING#1

TOKENRING#2

ATMSWITCH

#1

ATMSWITCH

#2

FIBERBACKBONE

PC1A PC1B

PC1C

PC1D

PC1E

PC1F

PC1G

PC2A PC2B

PC2C

PC2D

PC2EPC2F

PC2G

Network Simulation Example

Page 7: Chapter 6: Priority Queues (Heaps)

CS 340 Page 7

Network Simulation Example (Continued)

045: PC1B xmits on TR1

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

049: ATMS2 recvs on TR2

080: PC1D xmits on TR1

059: PC2B recvs on TR2

049: ATMS2 recvs on TR2

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

059: PC2B recvs on TR2

080: PC1D xmits on TR1

Delete Minimum

Process Event

047: ATMS1 recvs on TR1

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

049: ATMS2 recvs on TR2

080: PC1D xmits on TR1

059: PC2B recvs on TR2

Delete Minimum

049: ATMS2 recvs on TR2

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

059: PC2B recvs on TR2

080: PC1D xmits on TR1

Process Event

049: ATMS2 recvs on TR2

053: ATMS1 xmits on FB

072: PC1B xmits on TR1

068: PC2F xmits on TR2

050: ATMS1 xmits on FB

080: PC1D xmits on TR1

059: PC2B recvs on TR2

Delete Minimum...

Page 8: Chapter 6: Priority Queues (Heaps)

CS 340 Page 8

One weakness of the heap structure is the difficulty with which two heaps are merged into one.

Merging Heaps

10

17

25

31

45

61

58

47

12

13

30

23

15

37

39

63

41

71

90

54

46

42

55

48

64

75

49

52

87

78

93How would you merge the two heaps above into

a single heap, maintaining the completeness of the binary tree and the heap order property?When might it be necessary to merge two heaps?• One printer goes down, and its print jobs are

redirected to a second printer with its own priority queue.

• One network route becomes too congested so a switch must merge the forwarding queues for two of its outgoing lines.

• The operating system for a multiprocessor system decides to devote one processor to a certain task, merging its job queue with that of another processor.

Page 9: Chapter 6: Priority Queues (Heaps)

CS 340 Page 9

One solution to the priority queue merging problem is the leftist heap.For any node X in a binary tree, define nullPathLength(X) to be the length of the shortest path from X to a descendant node without two children.A leftist heap is a binary tree with the heap order property (i.e., every node’s value is less than or equal to its offsprings’ values), as well as the leftist heap property: the null path length of each node’s left child is greater than or equal to the null path length of its right child.Like the heap, the leftist heap performs insertions and removals in O(logn) time, but the leftist heap also performs merges in O(logn) time, a big improvement over the heap’s O(n) merge time.Examples:

Leftist Heaps

10

17

25

31

45

61

58

12

13

77

23

7

11

24

46

37

63

50

59

71

84

95

Page 10: Chapter 6: Priority Queues (Heaps)

CS 340 Page 10

template <class Etype> leftNode<Etype> *leftistHeap<Etype>:: Merge(leftNode<Etype> *h1, leftNode<Etype> *h2){ if (h1 == NULL) return h2; if (h2 == NULL) return h1; if (h2->element > h1->element) return Merge1(h1, h2); return Merge1(h2, h1);}

template <class Etype> leftNode<Etype> *leftistHeap<Etype>:: Merge1(leftNode<Etype> *h1, leftNode<Etype> *h2){ if (h1->left == NULL) h1->left = h2; else { h1->right = Merge(h1->right, h2); if (h1->left->nullPathLength < h1->right->nullPathLength) Swap(h1->left, h1->right); h1->nullPathLength = h1->right->nullPathLength + 1; } return h1;}

Merging Leftist Heaps

Page 11: Chapter 6: Priority Queues (Heaps)

CS 340 Page 11

Leftist Heap Merging Example

77

5923

13

12

59

77

10

45

25

61 58

31

17

23

13

12

59

77

37

24

63 50

46

11

8471

95

7

77

10

45

25

61 58

31

17

23

13

12

59

37

24

63 50

46

11

8471

95

77

10

45

25

61 58

31

17

23

13

12

59

710

45

25

61

58

31

17

23

13

12

77

7

37

24

63

50

46

11

84

71

59

95

Original Merge Call

10

45

25

61 58

31

17

23

13

12

77

59

1st Recursive Call

23

13

12

77

2nd Recursive Call

59

77 593rd Recursive

Call

Final Swap

Page 12: Chapter 6: Priority Queues (Heaps)

CS 340 Page 12

STL Priority QueuesThe Standard Template Library includes a template class for priority queues, priority_queue<Object>, implemented as a maximum heap.

#include <queue>#include <vector>#include <iostream>using namespace std;void main( ){ // The first priority_queue uses the // default vector base container priority_queue <int> q1; q1.push( 87 ); q1.push( 65 ); q1.push( 43 ); q1.push( 21 ); cout << "q1 = ( "; while ( !q1.empty( ) ) { cout << q1.top( ) << " "; q1.pop( ); } cout << ")" << endl;

// The second priority_queue uses the vector // base container, but specifies that the comparison // function greater be used for ordering elements, // i.e., that the priority queue be a min-heap. priority_queue <int, vector<int>, greater<int>> q2; q2.push( 87 ); q2.push( 65 ); q2.push( 43 ); q2.push( 21 ); cout << "q2 = ( "; while ( !q2.empty( ) ) { cout << q2.top( ) << " "; q2.pop( ); } cout << ")" << endl;}