View by Category

Loading...

PPT – Priority Queue Heap PowerPoint presentation | free to download

The Adobe Flash plugin is needed to view this content

About This Presentation

Write a Comment

User Comments (0)

Transcript and Presenter's Notes

Priority Queue (Heap)

- A kind of queue
- Dequeue gets element with the highest priority
- Priority is based on a comparable value (key) of

each object (smaller value higher priority, or

higher value higher priority) - Example Applications
- printer -gt print (dequeue) the shortest document

first - operating system -gt run (dequeue) the shortest

job first - normal queue -gt dequeue the first enqueued

element first

Priority Queue (Heap) Operations

Priority Queue

insert

deleteMin

- insert (enqueue)
- deleteMin (dequeue)
- smaller value higher priority
- find, return, and remove the minimum element

Implementation using Linked List

- Unsorted linked list
- insert takes O(1) time
- deleteMin takes O(N) time
- Sorted linked list
- insert takes O(N) time
- deleteMin takes O(1) time

Implementation using Binary Search Tree

- insert takes O(log N) time
- deleteMin takes O(log N) time
- support other operations that are not required by

priority queue (for example, findMax) - deleteMin make the tree unbalanced

Binary Heap

- binary tree
- completely filled (bottom level is filled from

left to right - size between 2h (bottom level has only one node)

and 2h1-1

Array Implementation of Binary Heap

left child is in position 2i right child is in

position (2i1) parent is in position i/2

Heap Order Property (for Minimum Heap)

- Any node is smaller than (or equal to) all of its

children (any subtree is a heap) - Smallest element is at the root (findMin take

O(1) time)

Insert

- Create a hole in the next available location
- Move the hole up (swap with its parent) until

data can be placed in the hole without violating

the heap order property (called percolate up)

Insert

insert 14

Percolate Up -gt move the place to put 14 up (move

its parent down) until its parent lt 14

Insert

deleteMin

- Create a hole at the root
- Move the hole down (swap with the smaller one of

its children) until the last element of the heap

can be placed in the hole without violating the

heap order property (called percolate down)

deleteMin

Percolate Down -gt move the place to put 31 down

(move its smaller child up) until its children gt

31

deleteMin

deleteMin

Running Time

- insert
- worst case takes O(log N) time, moves an element

from the bottom to the top - on average takes a constant time (2.607

comparisons), moves an element up 1.607 levels - deleteMin
- worst case takes O(log N) time
- on average takes O(log N) time (element that is

placed at the root is large, so it is percolated

almost to the bottom)

public class BinaryHeap private static

final int DEFAULT_CAPACITY 100 private int

currentSize private Comparable array

public BinaryHeap( ) public BinaryHeap(

int capacity ) public void insert( Comparable

x ) throws Overflow public Comparable

findMin( ) public Comparable deleteMin( )

public boolean isEmpty( ) public boolean

isFull( ) public void makeEmpty( )

private void percolateDown( int hole )

private void buildHeap( )

public static void main( String args )

int numItems 10000 BinaryHeap h new

BinaryHeap( numItems ) int i 37

for( i 37 i ! 0 i ( i 37 ) numItems )

h.insert( new MyInteger( i ) )

for( i 1 i lt numItems i ) if(

((MyInteger)( h.deleteMin( ) )).intValue( ) ! i

) System.out.println( "Oops! " i

)

public BinaryHeap( ) this(

DEFAULT_CAPACITY ) public BinaryHeap( int

capacity ) currentSize 0 array

new Comparable capacity 1 public void

makeEmpty( ) currentSize 0

public void insert( Comparable x ) throws

Overflow if( isFull( ) ) throw new

Overflow( ) int hole currentSize

for( hole gt 1 x.compareTo( array hole / 2

) lt 0 hole / 2 ) array hole

array hole / 2 array hole

x public Comparable deleteMin( ) if(

isEmpty( ) ) return null Comparable minItem

findMin( ) array 1 array

currentSize-- percolateDown( 1 )

return minItem

private void percolateDown( int hole ) int

child Comparable tmp array hole

for( hole 2 lt currentSize hole child )

child hole 2 if( child !

currentSize array child 1

.compareTo( array child ) lt 0 )

child if( array child .compareTo(

tmp ) lt 0 ) array hole array

child else break

array hole tmp

public boolean isEmpty( ) return currentSize

0 public boolean isFull( ) return

currentSize array.length - 1 private

void buildHeap( ) for( int i currentSize

/ 2 i gt 0 i-- ) percolateDown( i

) public Comparable findMin( ) if(

isEmpty( ) ) return null return array 1

Other Operations

- buildHeap
- insert N times
- decreaseKey(p, D )
- decrease the value of the key and percolate up
- increaseKey(p, D )
- increase the value of the key and percolate down
- delete
- decreaseKey(p, ? ) and deleteMin( )

buildHeap

- insert N times
- each insert takes O(1) average time, O(log N)

worst-case time - buildHeap takes O(N) average time, O(N log N)

worst-case time

buildHeap another method

- Place N items in to the tree in any order and

percolate down every node from bottom up to the

root - Worst-case time of buildHeap is the sum of

worst-case time to percolate these nodes, or the

sum of heights of these nodes - This method has O(N) worst-case time

Application Selection Problem

- Find the kth smallest element in a list
- Algorithm A
- buildHeap from all elements and deleteMin k times
- running time O(N k log N)
- if k O(N / log N) then running time is O(N)
- if k O(N) then running time is O(N log N)
- if k N and record the values deleted by

deleteMin --gt heap sort

Application Selection Problem

- Algorithm B (find the kth largest element)
- Maintain a set S of k largest elements all the

time - buildHeap from first k elements, the kth largest

element (the smallest in S, called Sk) is on top - If the next element in the input is larger than

Sk , then Sk is removed and the new one is

inserted into S - Running time is O(k) O(N-k) O((N-k)log k)

O(N log k)

Application Event Simulation

- Simulation of a bank with k tellers and C

customers - Each customer causes two events, arrival and

departure. Each event has a timestamp - Arrival events are put into a queue and departure

events are put into a priority queue - Find event that should occur next and process it
- Running time is O(C log(k1))

d-Heaps

- In a d-heap, all nodes have d children
- A binary heap is a 2-heap
- insert takes O(logd N)
- deleteMin takes O(d logd N)

About PowerShow.com

PowerShow.com is a leading presentation/slideshow sharing website. Whether your application is business, how-to, education, medicine, school, church, sales, marketing, online training or just for fun, PowerShow.com is a great resource. And, best of all, most of its cool features are free and easy to use.

You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

Recommended

«

/ »

Page of

«

/ »

Promoted Presentations

Related Presentations

Page of

Page of

CrystalGraphics Sales Tel: (800) 394-0700 x 1 or Send an email

Home About Us Terms and Conditions Privacy Policy Contact Us Send Us Feedback

Copyright 2014 CrystalGraphics, Inc. — All rights Reserved. PowerShow.com is a trademark of CrystalGraphics, Inc.

Copyright 2014 CrystalGraphics, Inc. — All rights Reserved. PowerShow.com is a trademark of CrystalGraphics, Inc.

The PowerPoint PPT presentation: "Priority Queue Heap" is the property of its rightful owner.

Do you have PowerPoint slides to share? If so, share your PPT presentation slides online with PowerShow.com. It's FREE!