priority queue (heap)

43
Priority Queue (Heap) Nattee Niparnan

Upload: kalila

Post on 23-Feb-2016

53 views

Category:

Documents


0 download

DESCRIPTION

Priority Queue (Heap). Nattee Niparnan. Flash Back. Still recall BST? and AVL? What about Hash? What they can do?. The need of another ADT. Queue with priority Hospital Bank Can’t be realized by a normal queue Previous ADTs are not efficient enough. Priority Queue (Heap ). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Priority Queue (Heap)

Priority Queue (Heap)

Nattee Niparnan

Page 2: Priority Queue (Heap)

Flash Back

• Still recall BST? and AVL?• What about Hash?

• What they can do?

Page 3: Priority Queue (Heap)

The need of another ADT

• Queue with priority– Hospital– Bank

• Can’t be realized by a normal queue• Previous ADTs are not efficient enough

Page 4: Priority Queue (Heap)

Priority Queue (Heap)

Insert Find Delete Find Min

Delete Min

List O(1) O(n) O(n) O(n) O(n)BST,AVL O(lg n) O(lg n) O(lg n) O(lg n) O(lg n)

Ordered List

O( n ) O( n ) O( n ) O( 1 ) O( 1 )

Hash O(1) O(1) O(1) - -Heap O(lg n) - - O(1) O(lg n)

Excel in “find min”, “delete min”

Page 5: Priority Queue (Heap)

What is Heap?

• Another kind of trees• Which is

– A binary tree (not BST)– (Almost) Complete

Page 6: Priority Queue (Heap)

Still recall a Binary Tree?

• A Binary Tree is either– An empty node– A node with at most two children– The children are also a Binary Tree

Page 7: Priority Queue (Heap)

Specialized Binary Tree

• BST– Add

• “all value in nodes of left tree are less than the value of the node”

• “all value in nodes of right tree are more than the value of the node”

• AVL– Add

• The same as BST• The height of the left subtree and the right subtree must not

be more than 1

Page 8: Priority Queue (Heap)

Specialized Binary Tree

• Heap– Add

A. “all value in nodes of left tree and right tree are more than the value of the node”

B. Tree is “complete binary tree”

Page 9: Priority Queue (Heap)

What is Complete Binary Tree

• A binary tree which is– Completely filled

• Except the bottom level which must be filled from left to right

Page 10: Priority Queue (Heap)

What is Complete Binary Tree

• A binary tree which is– Completely filled

• Except the bottom level which must be filled from left to right

Page 11: Priority Queue (Heap)

Example of a Binary Heap

26

4031

48 50 85 36

99 51 55 57 88

Page 12: Priority Queue (Heap)

Why we need such properties?

• A. because we need to find minimal– Helps us in the process

• B. Also helps us in the process– But also allow us to store a tree on an array

• Easy to identify children and parent

Page 13: Priority Queue (Heap)

Binary Heap on Array26 40 31 48 50 85 36 107 48 55 57 88

26

4031

48 50 85 36

99 51 55 57 88

Page 14: Priority Queue (Heap)

Identifying child

• Parent at heap[i]– Left child at heap[(i * 2)]– Right child at heap[(i * 2) + 1]

26

40 31

48 50 85 36

99 51 55 57 88

3

7 8

0

1 2

4 5 6

9 10 11

Page 15: Priority Queue (Heap)

Identifying parent

• child at heap[i]– Parent at heap[(i – 1) / 2]

26

40 31

48 50 85 36

99 51 55 57 88

3

7 8

0

1 2

4 5 6

9 10 11

Page 16: Priority Queue (Heap)

Operation on Heap

• FindMin– Simple, it is at the top

• Insert• DeleteMin

Page 17: Priority Queue (Heap)

How to insert?

• Where to put?– Have to maintain the property of the heap– Do we know the exact position?

– Solution• Add to the bottom

– Preserve B. but A. might fails• Fix it!!!

Page 18: Priority Queue (Heap)

Percolate Up (Bubble Up)

• To bring smaller item up• How?

While I am less than my parentswap myself with my parent

Page 19: Priority Queue (Heap)

Adding 30 (Bubble Up)

26

4031

48 50 85 36

99 51 55 57 88 30

Page 20: Priority Queue (Heap)

Adding 30 (Bubble Up)

26

4031

48 50 30 36

99 51 55 57 88 85

SWAP!!

Page 21: Priority Queue (Heap)

Adding 30 (Bubble Up)

26

4030

48 50 31 36

99 51 55 57 88 85

DONE!

SWAP!!

Page 22: Priority Queue (Heap)

What is the Big O of bubble Up?

• O (lg n)

• But… assume that the value to be add more than the median of all value– The “bigger” half must be the leave of the tree– So, we usually bubble up only one time.

Page 23: Priority Queue (Heap)

How to Delete

• We know where to delete– At the root– But…. What happen after that?

• Fix it!!

40 31

48 50 85 36

99 51 55 57 88

Headless tree

Page 24: Priority Queue (Heap)

How to Delete

• Bring someone else as the head– Who?

• The easiest to moved one• The tail

40 31

48 50 85 36

99 51 55 57 88

Headless tree88

Page 25: Priority Queue (Heap)

How to Delete

• Now, we fix B.– But A. fail

• Fix it

40 31

48 50 85 36

99 51 55 57

Headless tree88

Page 26: Priority Queue (Heap)

Percolate Down (sieve down)

• To bring bigger item down• How?

While I am more than any of my childrenswap myself with “smaller” child

Page 27: Priority Queue (Heap)

Percolate Up (Bubble Up)

• To bring smaller item up• How?

While I am less than my parentswap myself with my parent

Page 28: Priority Queue (Heap)

See it in action

88

4031

48 50 85 36

99 51 55 57

Page 29: Priority Queue (Heap)

See it in action

88

4031

48 50 85 36

99 51 55 57

More than?

Which is smaller?

Page 30: Priority Queue (Heap)

See it in action

31

4088

48 50 85 36

99 51 55 57

SWAP!!

Page 31: Priority Queue (Heap)

See it in action

31

4088

48 50 85 36

99 51 55 57

More than?

Which is smaller?

Page 32: Priority Queue (Heap)

See it in action

88

4036

48 50 85 88

99 51 55 57Done!!

SWAP!!

Page 33: Priority Queue (Heap)

What is the Big O of sieve down?

• O (lg n)

• Unlike the bubble up case– The value we put at the top is usually the bigger

one• Hence, most of the time, we will have to actually do the

O(lg n)

Page 34: Priority Queue (Heap)

That’s it

• Now, we look at the code

Page 35: Priority Queue (Heap)

Java Code of Heap

Page 36: Priority Queue (Heap)

public class BinaryHeap<AnyType extends Comparable<? super AnyType>>{ public BinaryHeap( ) public BinaryHeap( int capacity ) public BinaryHeap( AnyType [ ] items )

public void insert( AnyType x )

public AnyType findMin( )

public AnyType deleteMin( )

private static final int DEFAULT_CAPACITY = 10; private int currentSize; private AnyType [ ] array;

private void percolateDown( int hole )}

Page 37: Priority Queue (Heap)

/** * Construct the binary heap. * @param capacity the capacity of the binary heap. */ public BinaryHeap( int capacity ) { currentSize = 0; array = (AnyType[]) new Comparable[ capacity + 1 ]; }

Constructor

Page 38: Priority Queue (Heap)

public AnyType findMin( ){ if( isEmpty( ) ) throw new UnderflowException( ); return array[ 1 ];}

FindMin

Page 39: Priority Queue (Heap)

public void insert( AnyType x ){ if( currentSize == array.length - 1 ) enlargeArray( array.length * 2 + 1 );

// Percolate up int hole = ++currentSize; for( ; hole > 1 && x.compareTo( array[ hole / 2 ] ) < 0; hole /= 2 ) array[ hole ] = array[ hole / 2 ];

array[ hole ] = x;}

Insert & Percolate Up

Loop until root

Father is larger?

Page 40: Priority Queue (Heap)

Delete Min

public AnyType deleteMin( ){ if( isEmpty( ) ) throw new UnderflowException( );

AnyType minItem = findMin( ); array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 );

return minItem;}

Page 41: Priority Queue (Heap)

Percolate Downprivate void percolateDown( int hole ){ int child; AnyType tmp = array[ hole ];

For( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2; if( child != currentSize && array[ child + 1 ].compareTo( array[ child ] ) < 0 ) child++; if( array[ child ].compareTo( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp;}

We have two children?

The right one is smaller?

The smaller one is less than me?

Loop until the bottom

Page 42: Priority Queue (Heap)

Main (testing)

public static void main( String [ ] args ) { int numItems = 10000; BinaryHeap<Integer> h = new BinaryHeap<Integer>( ); int i = 37;

for( i = 37; i != 0; i = ( i + 37 ) % numItems ) h.insert( i ); for( i = 1; i < numItems; i++ ) if( h.deleteMin( ) != i ) System.out.println( "Oops! " + i ); }

Page 43: Priority Queue (Heap)

26

4031

48 50 85 36

99 51 55 57 88