Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of...
-
date post
21-Dec-2015 -
Category
Documents
-
view
223 -
download
4
Transcript of Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of...
![Page 1: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/1.jpg)
Heaps & Priority Queues
Nelson Padua-Perez
Bill PughDepartment of Computer Science
University of Maryland, College Park
![Page 2: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/2.jpg)
Overview
Heaps
Priority queues
![Page 3: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/3.jpg)
Heaps
Two key propertiesHeap shape
Value at node
Smaller than or equal to values in subtrees
Example heapX YX Z
Y
X
Z
![Page 4: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/4.jpg)
Heap & Non-heap Examples
Heaps Non-heaps
6
2
22
8 45 25
6
2
22
8 45 25
8
6 45
5
6 22
25
5
5 45
5
![Page 5: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/5.jpg)
Heap Properties
Key operationsinsert ( X )
getSmallest ( )
Key applicationsHeapsort
Priority queue
![Page 6: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/6.jpg)
Heap Operations – Insert( X )
AlgorithmAdd X to end of tree
While (X < parent)
Swap X with parent // X bubbles up tree
Complexity# of swaps proportional to height of tree
O( log(n) )
![Page 7: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/7.jpg)
Heap Insert Example
Insert ( 20 )
10
30 25
37
10
30 25
37 20
10
20 25
37 30
1) Insert to end of tree
2) Compare to parent, swap if parent key larger
3) Insert complete
![Page 8: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/8.jpg)
Heap Insert Example
Insert ( 8 )
10
30 25
37
10
30 25
37 8
10
8 25
37 30
8
10 25
37 30
1) Insert to end of tree
2) Compare to parent, swap if parent key larger
3) Insert complete
![Page 9: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/9.jpg)
Heap Operation – getSmallest()
AlgorithmGet smallest node at root
Replace root with X at end of tree
While ( X > child )
Swap X with smallest child // X drops down tree
Return smallest node
Complexity# swaps proportional to height of tree
O( log(n) )
![Page 10: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/10.jpg)
Heap GetSmallest Example
getSmallest ()
8
10 25
37
30
10 25
37
10
30 25
3730
1) Replace root with end of tree
2) Compare node to children, if larger swap
with smallest child
3) Repeat swap if needed
![Page 11: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/11.jpg)
Heap GetSmallest Example
getSmallest ()
8
10 25
30
37
10 25
30
10
37 25
3037
1) Replace root with end of tree
2) Compare node to children, if larger swap
with smallest child
3) Repeat swap if needed
10
30 25
37
![Page 12: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/12.jpg)
Heap Implementation
Can implement heap as arrayStore nodes in array elements
Assign location (index) for elements using formula
![Page 13: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/13.jpg)
Heap Implementation
ObservationsCompact representation
Edges are implicit (no storage required)
Works well for complete trees (no wasted space)
![Page 14: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/14.jpg)
Heap Implementation
Calculating node locationsArray index i starts at 0
Parent(i) = ( i – 1 ) / 2 LeftChild(i) = 2 i +1
RightChild(i) = 2 i +2
![Page 15: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/15.jpg)
Heap Implementation
ExampleParent(1) = ( 1 – 1 ) / 2 = 0 / 2 = 0
Parent(2) = ( 2 – 1 ) / 2 = 1 / 2 = 0
Parent(3) = ( 3 – 1 ) / 2 = 2 / 2 = 1
Parent(4) = ( 4 – 1 ) / 2 = 3 / 2 = 1
Parent(5) = ( 5 – 1 ) / 2 = 4 / 2 = 2
![Page 16: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/16.jpg)
Heap Implementation
ExampleLeftChild(0) = 2 0 +1 = 1
LeftChild(1) = 2 1 +1 = 3
LeftChild(2) = 2 2 +1 = 5
![Page 17: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/17.jpg)
Heap Implementation
ExampleRightChild(0) = 2 0 +2 = 2
RightChild(1) = 2 1 +2 = 4
![Page 18: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/18.jpg)
Heap Application – Heapsort
Use heaps to sort valuesHeap keeps track of smallest element in heap
AlgorithmCreate heap
Insert values in heap
Remove values from heap (in ascending order)
ComplexityO( nlog(n))
![Page 19: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/19.jpg)
Heapsort Example
Input11, 5, 13, 6, 1
View heap during insert, removalAs tree
As array
![Page 20: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/20.jpg)
Heapsort – Insert Values
![Page 21: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/21.jpg)
Heapsort – Remove Values
![Page 22: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/22.jpg)
Heapsort – Insert in to Array 1
Input11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 11 11
![Page 23: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/23.jpg)
Heapsort – Insert in to Array 2
Input11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 5 11 5
Swap 5 11
![Page 24: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/24.jpg)
Heapsort – Insert in to Array 3
Input11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 13 5 11 13
![Page 25: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/25.jpg)
Heapsort – Insert in to Array 4
Input11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 6 5 11 13 6
Swap 5 6 13 11
…
![Page 26: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/26.jpg)
Heapsort – Remove from Array 1
Input11, 5, 13, 6, 1
Index = 0 1 2 3 4
Remove root 1 5 13 11 6
Replace 6 5 13 11
Swap w/ child 5 6 13 11
![Page 27: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/27.jpg)
Heapsort – Remove from Array 2
Input11, 5, 13, 6, 1
Index = 0 1 2 3 4
Remove root 5 6 13 11
Replace 11 6 13
Swap w/ child 6 11 13
![Page 28: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/28.jpg)
Heap Application – Priority Queue
QueueLinear data structure
First-in First-out (FIFO)
Implement as array / linked list
Priority queueElements are assigned priority value
Higher priority elements are taken out first
Equal priority elements are taken out in arbitrary order
Implement as heap
DequeueEnqueue
![Page 29: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/29.jpg)
Priority Queue
PropertiesLower value = higher priority
Heap keeps highest priority items in front
ComplexityEnqueue (insert) = O( log(n) )
Dequeue (remove) = O( log(n) )
For any heap
![Page 30: Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader031.fdocuments.net/reader031/viewer/2022012916/56649d545503460f94a30724/html5/thumbnails/30.jpg)
Heap vs. Binary Search Tree
Binary search treeKeeps values in sorted order
Find any value
O( log(n) ) for balanced tree
O( n ) for degenerate tree (worst case)
HeapKeeps smaller values in front
Find minimum value
O( log(n) ) for any heap
Can also organize heap to find maximum value
Keep largest value in front