chapter 5 priority queues (heaps) §1 adt model objects: a finite ordered list with zero or more...

15
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations: PriorityQueue Initialize( int MaxElements ); void Insert( ElementType X, PriorityQueue H ); ElementType DeleteMin( PriorityQueue H ); ElementType FindMin( PriorityQueue H ); delete the element with the highest \ lowest prior 1/15

Upload: leonard-jenkins

Post on 17-Dec-2015

229 views

Category:

Documents


7 download

TRANSCRIPT

CHAPTER 5

PRIORITY QUEUES (HEAPS)

§1 ADT Model

Objects: A finite ordered list with zero or more elements.

Operations:

PriorityQueue Initialize( int MaxElements );

void Insert( ElementType X, PriorityQueue H );

ElementType DeleteMin( PriorityQueue H );

ElementType FindMin( PriorityQueue H );

—— delete the element with the highest \ lowest priority

1/15

§2 Simple Implementations Array :

Insertion — add one item at the end ~ ( 1 )Deletion — find the largest \ smallest key ~ ( n ) remove the item and shift array ~ O( n )

Linked List :

Insertion — add to the front of the chain ~ ( 1 )Deletion — find the largest \ smallest key ~ ( n ) remove the item ~ ( 1 )

Ordered Array :

Insertion — find the proper position ~ O( n ) shift array and add the item ~ O( n )Deletion — remove the first \ last item ~ ( 1 )

Ordered Linked List :

Insertion — find the proper position ~ O( n ) add the item ~ ( 1 )Deletion — remove the first \ last item ~ ( 1 )

Better since there are never more deletions than insertions

2/15

Binary Search Tree :

§2 Simple Implementations

Ah! That’s a good idea! Both insertion and deletion will take

O(log N) only.

Well, insertions are random, but deletions are NOT. We are supposed to delete

The minimum element only.

Oh, right, then we must always delete from the left subtrees….

But hey, what if we keep a balanced tree?

Hey you are getting smarter! Yes a balanced tree such as AVL tree

is not a bad idea since only a constant factor will be added to

the run time. However…

Oh no… what’s wrong?There are many operations related to AVL tree that we don’t really

need for a priority queue.Besides, pointers are

always dangerous.

I bet you have a better option?Now you begin to know me

3/15

§3 Binary Heap1. Structure Property:

【 Definition】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered from 1 to n in the perfect binary tree of height h.

4

8 9

5

10 11

6

12 13

7

14 15

2 3

1

A complete binary tree of height h has between

and nodes.

2h

2h+1 1 h = log N

Array Representation : BT [ n + 1 ] ( BT [ 0 ] is not used)

D

H I

E

J

F G

B C

ABT 0 1

A2B

3C

4D

5E

6F

7G

8H

9I

10J

11 12 13

4/15

§3 Binary Heap

【 Lemma】 If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1 i n, we have:

ni

niiichildright

ni

niiichildleft

i

iiiparent

12 ifNone

12 if12)(_ ofindex (3)

2 ifNone

2 if2)(_ ofindex (2)

1 ifNone

1 if2)( ofindex (1)

5/15

§3 Binary Heap

PriorityQueue Initialize( int MaxElements ) { PriorityQueue H; if ( MaxElements < MinPQSize )

return Error( "Priority queue size is too small" ); H = malloc( sizeof ( struct HeapStruct ) ); if ( H ==NULL )

return FatalError( "Out of space!!!" ); /* Allocate the array plus one extra for sentinel */ H->Elements = malloc(( MaxElements + 1 ) * sizeof( ElementType )); if ( H->Elements == NULL )

return FatalError( "Out of space!!!" ); H->Capacity = MaxElements; H->Size = 0; H->Elements[ 0 ] = MinData; /* set the sentinel */ return H; }

6/15

§3 Binary Heap2. Heap Order Property:

【 Definition】 A min tree is a tree in which the key value in each node is no larger than the key values in its children (if any). A min heap is a complete binary tree that is also a min tree.

Note: Analogously, we can declare a max heap by changing the heap order property.

Note: Analogously, we can declare a max heap by changing the heap order property.

9

6

5

3

[1]

[2] [3]

[4]

A max heap

10

20

50

83

[1]

[2] [3]

[4]

A min heap

The largest key The smallest key

7/15

§3 Binary Heap3. Basic Heap Operations:

insertion

10

12

15

20

[1]

[2] [3]

[4]

18[5] [6]

Sketch of the idea:

The only possible position for a new node

since a heap must be a complete binary tree.

Case 1 : new_item = 21

21

20 21<

Case 2 : new_item = 17

17

20 17>

17

20

10 17<

Case 3 : new_item = 9 20 9>

9

10 9>

9

10

8/15

§3 Binary Heap

/* H->Element[ 0 ] is a sentinel */ void Insert( ElementType X, PriorityQueue H ) { int i;

if ( IsFull( H ) ) { Error( "Priority queue is full" ); return;

}

for ( i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2 ) H->Elements[ i ] = H->Elements[ i / 2 ];

H->Elements[ i ] = X; }

Percolate up

Faster than swap

H->Element[ 0 ] is a sentinel that is no larger

than the minimum element in the heap.

T (N) = O ( log N )

9/15

§3 Binary Heap

DeleteMin

Sketch of the idea:

10

12

15

20

[1]

[2] [3]

[4]

18[5]

The node which must beremoved to keep a

complete binary tree.

move 18 up to the root18

find the smaller child of 18 12 18<18

12

15 18<18

15

Ah! That’s simple --we only have to delete

the root node ...And re-arrange

the rest of the tree so that it’s still a min heap.

T (N) = O ( log N )

10/15

§3 Binary Heap

ElementType DeleteMin( PriorityQueue H ) { int i, Child; ElementType MinElement, LastElement; if ( IsEmpty( H ) ) { Error( "Priority queue is empty" ); return H->Elements[ 0 ]; } MinElement = H->Elements[ 1 ]; /* save the min element */ LastElement = H->Elements[ H->Size-- ]; /* take last and reset size */ for ( i = 1; i * 2 <= H->Size; i = Child ) { /* Find smaller child */ Child = i * 2; if (Child != H->Size && H->Elements[Child+1] < H->Elements[Child])

Child++; if ( LastElement > H->Elements[ Child ] ) /* Percolate one level */

H->Elements[ i ] = H->Elements[ Child ]; else break; /* find the proper position */ } H->Elements[ i ] = LastElement; return MinElement; }

Percolate down

What if this condition is

omitted?

Can we remove it by adding another

sentinel?

11/15

§3 Binary Heap4. Other Heap Operations:

Note: Finding any key except the minimum one will have to take a linear scan through the entire heap.

Note: Finding any key except the minimum one will have to take a linear scan through the entire heap.

DecreaseKey ( P, , H )

Lower the value of the key in the heap H at position P by a positive amount of ……so my programs can run with highest priority .

sys. admin.

IncreaseKey ( P, , H )

Percolate upPercolate up

sys. admin.

Increases the value of the key in the heap H at position P by a positive amount of ……drop the priority of a process that is consuming excessive CPU time.

Percolate downPercolate down

12/15

§3 Binary Heap

Delete ( P, H )

Remove the node at position P from the heap H …… delete the process that is terminated (abnormally) by a user.

sys. admin.

DecreaseKey(P, , H); DeleteMin(H)DecreaseKey(P, , H); DeleteMin(H)

BuildHeap ( H )

Place N input keys into an empty heap H.

sys. admin.

N successive Insertions ? N successive Insertions ? Nehhhhh that would be

toooo slow !

30

100 20

10

90 60

70

50 120

110

140 130

80 40

150

150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130

PercolateDown (7)PercolateDown (6)

50

70

PercolateDown (5)PercolateDown (4)

20

30

PercolateDown (3)PercolateDown (2)

80

10

80

60

PercolateDown (1)

150

10

150

20

150

30

T (N) = ?

13/15

§3 Binary Heap

【 Theorem】 For the perfect binary tree of height h containing 2h+1 1 nodes, the sum of the heights of the nodes is 2h+1 1 (h + 1).

T ( N ) = O ( N )

§4 Applications of Priority Queues

〖 Example〗 Given a list of N elements and an integer k. Find the kth largest element.

How many methods can you think of to solve this problem? What are

their complexities?

Home work:p.180 5.2, 5.3, 5.4

A test case for operationson a heap

14/15

§5 d-Heaps ---- All nodes have d children

3

1513 6

5

178 9

2

74 10

911

1

3-heap

Question: Shall we make d as large as possible?

Note: DeleteMin will take d 1 comparisons to find the smallest child. Hence the total time complexity would be O(d logd N).

*2 or /2 is merely a bit shift, but *d or /d is not. When the priority queue is too large to fit entirely in main

memory, a d-heap will become interesting.

Note: DeleteMin will take d 1 comparisons to find the smallest child. Hence the total time complexity would be O(d logd N).

*2 or /2 is merely a bit shift, but *d or /d is not. When the priority queue is too large to fit entirely in main

memory, a d-heap will become interesting.

Home work:p.181 5.13

Index computations for parents and children

in a d-heap

15/15