SlideShare a Scribd company logo
1 of 14
Download to read offline
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
1Heaps
Data Structures & File Management
Heaps
A max-heap is a complete binary tree in which the value in each internal node
is greater than or equal to the values in the children of that node.
A min-heap is defined similarly.
97
79
93
90 81
84
83
5542 2173 83
802173554283837990849397
11109876543210
Mapping the elements
of a heap into an array
is trivial:
if a node is stored at
index k, then its left
child is stored at index
2k+1 and its right
child at index 2k+2
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
2Heaps
Data Structures & File Management
Building a Heap
The fact that a heap is a complete binary tree allows it to be efficiently
represented using a simple array.
Given an array of N values, a heap containing those values can be built, in situ,
by simply “sifting” each internal node down to its proper location:
81
73
74
9079 93
93
73
74
9079 81
93
73
90
7479 42
73
93
90
7479 81
- start with the last
internal node
- swap the current
internal node with
its larger child, if
necessary
- then follow the
swapped node
down
- continue until all
internal nodes are
done
* *
*
*
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
3Heaps
Data Structures & File Management
Heap Class Interface
We will consider a somewhat minimal heap class:
template <class Item> class HeapT {
private:
Item* Hp;
int numVertices;
int Size;
void SiftDown(int toSift); // helper for BuildHeap, etc.
public:
HeapT();
HeapT(Item* List, int numItems); // create from an array
HeapT(const HeapT& Source);
HeapT& operator=(const HeapT& Source);
~HeapT();
bool isEmpty() const;
bool isLeaf(int Vertex) const;
int leftChild(int Vertex) const; // traversal navigation
int rightChild(int Vertex) const;
int Parent(int Vertex) const;
Item RemoveRoot(); // deletes root & re-heaps
void BuildHeap(); // heapifies the array
};
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
4Heaps
Data Structures & File Management
BuildHeap and SiftDown
As described earlier:
template <class Item> void HeapT<Item>::BuildHeap() {
for (int Idx = numVertices/2 - 1; Idx >= 0; Idx--)
SiftDown(Idx);
}
template <class Item> void HeapT<Item>::SiftDown(int toSift) {
while ( !isLeaf(toSift) ) {
int MaxChild = leftChild(toSift);
if ( (MaxChild < numVertices - 1) &&
(Hp[MaxChild] < Hp[MaxChild + 1]) )
MaxChild++;
if (Hp[toSift] >= Hp[MaxChild]) return;
Item tmpItem = Hp[toSift];
Hp[toSift] = Hp[MaxChild];
Hp[MaxChild] = tmpItem;
toSift = MaxChild;
}
}
Determine which child node
is larger
See if node must sift down
If so, swap it with its larger
child (maintaining heap
property),
and continue sifting down…
QTP: Why is Idx
initialized this
way?
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
5Heaps
Data Structures & File Management
Cost of SiftDown
In a complete binary tree of N nodes, the number of levels is at most 1 + log(N).
Since each non-terminating iteration of the loop moves the target value a
distance of 1 level, the loop will perform no more than log(N) iterations.
Thus, the worst case cost of SiftDown() is O(log N).
template <class Item> void HeapT<Item>::SiftDown(int toSift) {
while ( !isLeaf(toSift) ) {
int MaxChild = leftChild(toSift);
if ( (MaxChild < numVertices - 1) &&
(Hp[MaxChild] < Hp[MaxChild + 1]) )
MaxChild++;
if (Hp[toSift] >= Hp[MaxChild]) return;
Item tmpItem = Hp[toSift];
Hp[toSift] = Hp[MaxChild];
Hp[MaxChild] = tmpItem;
toSift = MaxChild;
}
}
In the worst case, we
perform two element
comparisons…
…and one element swap per
iteration
That’s 2log(N) comparisons
and log(N) swaps.
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
6Heaps
Data Structures & File Management
Cost of BuildHeap
Suppose we start with a complete binary tree with N nodes; the number of steps
required for sifting values down will be maximized if the tree is also full, in
which case N = 2d-1 for some integer d = élog Nù. For example:
42
55
83
90 81
21
93
3797 8473 83 95 62 17
level 0: 20 nodes, can sift
down d - 1 levels
level 1: 21 nodes, can sift
down d - 2 levels
level 2: 22 nodes, can sift
down d - 3 levels
We can prove that in general, level k of a full and complete binary tree will
contain 2k nodes, and that those nodes are d – k – 1 levels above the leaves.
Thus…
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
7Heaps
Data Structures & File Management
Cost of BuildHeap
In the worst case, the number of comparisons BuildHeap() will require in
building a heap of N nodes is given by:
Since, at worst, there is one swap for each two comparisons, the maximum
number of swaps is N – élog Nù.
Hence, building a heap of N nodes is O(N) in both comparisons and swaps.
( ) ( )
[ ] é ù[ ]log2122
22212122sComparison
1
0
1
0
1
1
0
NNd
kdkd
d
d
k
d
k
kk
d
k
k
−=−−=
ú
û
ù
ê
ë
é
−−=−−= å åå
−
=
−
=
−
−
=
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
8Heaps
Data Structures & File Management
Deleting the Root of a Heap
We will see that the most common operation on a heap is the deletion of the
root node. The heap property is maintained by sifting down…
template <class Item> Item HeapT<Item>::RemoveRoot() {
if (numVertices == 0) return Item();
Item tmpItem = Hp[0];
Hp[0] = Hp[numVertices - 1];
Hp[numVertices - 1] = tmpItem;
numVertices--;
SiftDown(0);
return tmpItem;
}
Check for empty heap
Swap the root with the last
leaf, and
shrink the heap by one node.
Sift the new root down to
restore the heap property.
QTP: Why is the last leaf chosen as the
replacement for the root?
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
9Heaps
Data Structures & File Management
Example of Root Deletion
Given the initial heap:
In a heap of N nodes, the maximum
distance the root can sift down
would be éééélog Nùùùù - 1.
97
79
93
90 81
84
83
5542 2173 83
83
79
93
90 81
84
83
5542 217393
79
83
90 81
84
83
5542 2173
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
10Heaps
Data Structures & File Management
Heap Sort
A list can be sorted by first building it into a heap, and then iteratively deleting
the root node from the heap until the heap is empty. If the deleted roots are
stored in reverse order in an array they will be sorted in ascending order (if a
max heap is used).
void HeapSort(int* List, int Size) {
HeapT<int> toSort(List, Size);
toSort.BuildHeap();
int Idx = Size - 1;
while ( !toSort.isEmpty() ) {
List[Idx] = toSort.RemoveRoot();
Idx--;
}
}
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
11Heaps
Data Structures & File Management
Cost of Deleting the Roots
Recalling the earlier analysis of building a heap, level k of a full and complete
binary tree will contain 2k nodes, and that those nodes are k levels below the
root level.
So, when the root is deleted the maximum number of levels the swapped node
can sift down is the number of the level from which that node was swapped.
Thus, in the worst case, for deleting all the roots…
( )[ ]
é ù é ù NNNN
dkk d
d
k
k
d
k
k
4log2log2
12242422sComparison 1
1
1
1
1
1
−+=
+−=== −
−
=
−
−
=
åå
As usual, with Heap Sort, this would entail half as many element swaps.
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
12Heaps
Data Structures & File Management
Cost of Heap Sort
Adding in the cost of building the heap from our earlier analysis,
é ù( ) é ù é ù( )
é ù NNN
NNNNNN
2log2
4log2log2log22sComparisonTotal
−=
−++−=
and...
So, in the worst case, Heap Sort is Θ(N log N) in both swaps and comparisons.
é ù NNN −= logSwapsTotal
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
13Heaps
Data Structures & File Management
Priority Queues
A priority queue consists of entries, each of which contains a key field, called
the priority of the entry.
Aside from the usual operations of creation, clearing, tests for full and empty,
and reporting its size, a priority queue has only two operations:
- insertion of a new entry
- removal of the entry having the largest (or smallest) key
Key values need not be unique. If not, then removal may target any entry
holding the largest value.
Computer Science Dept Va Tech July 2000 ©2000 McQuain WD
14Heaps
Data Structures & File Management
Representation
Representation of a priority queue may be achieved using:
- a sorted list, but…
- an unsorted list, but…
- a max-heap
Priority queues may be used to manage prioritized processes in a time-sharing
environment, time-dependent simulations, and even numerical linear algebra.

More Related Content

What's hot

Working with the Scalding Type-Safe API
Working with the Scalding Type-Safe API Working with the Scalding Type-Safe API
Working with the Scalding Type-Safe API Criteolabs
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data ManagementAlbert Bifet
 
[241]large scale search with polysemous codes
[241]large scale search with polysemous codes[241]large scale search with polysemous codes
[241]large scale search with polysemous codesNAVER D2
 
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and SparkFOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and SparkRob Emanuele
 
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...Spark Summit
 
Processing Reachability Queries with Realistic Constraints on Massive Network...
Processing Reachability Queries with Realistic Constraints on Massive Network...Processing Reachability Queries with Realistic Constraints on Massive Network...
Processing Reachability Queries with Realistic Constraints on Massive Network...BigMine
 
A Short Course in Data Stream Mining
A Short Course in Data Stream MiningA Short Course in Data Stream Mining
A Short Course in Data Stream MiningAlbert Bifet
 
Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...
Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...
Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...Tomonari Masada
 
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...Altinity Ltd
 
Parquet Vectorization in Hive
Parquet Vectorization in HiveParquet Vectorization in Hive
Parquet Vectorization in HiveSahil Takiar
 
Deep dive into deeplearn.js
Deep dive into deeplearn.jsDeep dive into deeplearn.js
Deep dive into deeplearn.jsKai Sasaki
 
Scala meetup - Intro to spark
Scala meetup - Intro to sparkScala meetup - Intro to spark
Scala meetup - Intro to sparkJavier Arrieta
 
Mining Adaptively Frequent Closed Unlabeled Rooted Trees in Data Streams
Mining Adaptively Frequent Closed Unlabeled Rooted Trees in Data StreamsMining Adaptively Frequent Closed Unlabeled Rooted Trees in Data Streams
Mining Adaptively Frequent Closed Unlabeled Rooted Trees in Data StreamsAlbert Bifet
 
Introduction to Hadoop and MapReduce
Introduction to Hadoop and MapReduceIntroduction to Hadoop and MapReduce
Introduction to Hadoop and MapReduceDr Ganesh Iyer
 
GeoSpatially enabling your Spark and Accumulo clusters with LocationTech
GeoSpatially enabling your Spark and Accumulo clusters with LocationTechGeoSpatially enabling your Spark and Accumulo clusters with LocationTech
GeoSpatially enabling your Spark and Accumulo clusters with LocationTechRob Emanuele
 
Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...
Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...
Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...Cloudera, Inc.
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 

What's hot (20)

Working with the Scalding Type-Safe API
Working with the Scalding Type-Safe API Working with the Scalding Type-Safe API
Working with the Scalding Type-Safe API
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
[241]large scale search with polysemous codes
[241]large scale search with polysemous codes[241]large scale search with polysemous codes
[241]large scale search with polysemous codes
 
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and SparkFOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
 
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
A Scalable Hierarchical Clustering Algorithm Using Spark: Spark Summit East t...
 
Processing Reachability Queries with Realistic Constraints on Massive Network...
Processing Reachability Queries with Realistic Constraints on Massive Network...Processing Reachability Queries with Realistic Constraints on Massive Network...
Processing Reachability Queries with Realistic Constraints on Massive Network...
 
A Short Course in Data Stream Mining
A Short Course in Data Stream MiningA Short Course in Data Stream Mining
A Short Course in Data Stream Mining
 
Matlab netcdf guide
Matlab netcdf guideMatlab netcdf guide
Matlab netcdf guide
 
Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...
Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...
Accelerating Collapsed Variational Bayesian Inference for Latent Dirichlet Al...
 
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
A Practical Introduction to Handling Log Data in ClickHouse, by Robert Hodges...
 
Parquet Vectorization in Hive
Parquet Vectorization in HiveParquet Vectorization in Hive
Parquet Vectorization in Hive
 
Deep dive into deeplearn.js
Deep dive into deeplearn.jsDeep dive into deeplearn.js
Deep dive into deeplearn.js
 
Heaps
HeapsHeaps
Heaps
 
Scala meetup - Intro to spark
Scala meetup - Intro to sparkScala meetup - Intro to spark
Scala meetup - Intro to spark
 
Heaps
HeapsHeaps
Heaps
 
Mining Adaptively Frequent Closed Unlabeled Rooted Trees in Data Streams
Mining Adaptively Frequent Closed Unlabeled Rooted Trees in Data StreamsMining Adaptively Frequent Closed Unlabeled Rooted Trees in Data Streams
Mining Adaptively Frequent Closed Unlabeled Rooted Trees in Data Streams
 
Introduction to Hadoop and MapReduce
Introduction to Hadoop and MapReduceIntroduction to Hadoop and MapReduce
Introduction to Hadoop and MapReduce
 
GeoSpatially enabling your Spark and Accumulo clusters with LocationTech
GeoSpatially enabling your Spark and Accumulo clusters with LocationTechGeoSpatially enabling your Spark and Accumulo clusters with LocationTech
GeoSpatially enabling your Spark and Accumulo clusters with LocationTech
 
Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...
Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...
Hadoop Summit 2012 | Bayesian Counters AKA In Memory Data Mining for Large Da...
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 

Similar to C07.heaps

Parallel Implementation of K Means Clustering on CUDA
Parallel Implementation of K Means Clustering on CUDAParallel Implementation of K Means Clustering on CUDA
Parallel Implementation of K Means Clustering on CUDAprithan
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...David Walker
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdfNash229987
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptMouDhara1
 
Unlocking Your Hadoop Data with Apache Spark and CDH5
Unlocking Your Hadoop Data with Apache Spark and CDH5Unlocking Your Hadoop Data with Apache Spark and CDH5
Unlocking Your Hadoop Data with Apache Spark and CDH5SAP Concur
 
Porting and optimizing UniFrac for GPUs
Porting and optimizing UniFrac for GPUsPorting and optimizing UniFrac for GPUs
Porting and optimizing UniFrac for GPUsIgor Sfiligoi
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationGeoffrey Fox
 
Lecture 25
Lecture 25Lecture 25
Lecture 25Shani729
 
Introduction to Apache Spark
Introduction to Apache SparkIntroduction to Apache Spark
Introduction to Apache SparkDatio Big Data
 
Deep Learning: R with Keras and TensorFlow
Deep Learning: R with Keras and TensorFlowDeep Learning: R with Keras and TensorFlow
Deep Learning: R with Keras and TensorFlowOswald Campesato
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearlESUG
 
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen TatarynovWorkshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen TatarynovFwdays
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded ProgrammingSri Prasanna
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Heaps
HeapsHeaps
HeapsIIUM
 

Similar to C07.heaps (20)

Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Parallel Implementation of K Means Clustering on CUDA
Parallel Implementation of K Means Clustering on CUDAParallel Implementation of K Means Clustering on CUDA
Parallel Implementation of K Means Clustering on CUDA
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
Ns2
Ns2Ns2
Ns2
 
Heap
HeapHeap
Heap
 
Ns network simulator
Ns network simulatorNs network simulator
Ns network simulator
 
Unlocking Your Hadoop Data with Apache Spark and CDH5
Unlocking Your Hadoop Data with Apache Spark and CDH5Unlocking Your Hadoop Data with Apache Spark and CDH5
Unlocking Your Hadoop Data with Apache Spark and CDH5
 
Porting and optimizing UniFrac for GPUs
Porting and optimizing UniFrac for GPUsPorting and optimizing UniFrac for GPUs
Porting and optimizing UniFrac for GPUs
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel application
 
Lecture 25
Lecture 25Lecture 25
Lecture 25
 
Introduction to Apache Spark
Introduction to Apache SparkIntroduction to Apache Spark
Introduction to Apache Spark
 
Deep Learning: R with Keras and TensorFlow
Deep Learning: R with Keras and TensorFlowDeep Learning: R with Keras and TensorFlow
Deep Learning: R with Keras and TensorFlow
 
Dancing Links: an educational pearl
Dancing Links: an educational pearlDancing Links: an educational pearl
Dancing Links: an educational pearl
 
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen TatarynovWorkshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
Workshop "Can my .NET application use less CPU / RAM?", Yevhen Tatarynov
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Heaps
HeapsHeaps
Heaps
 
Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2
 

Recently uploaded

Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentBharaniDharan195623
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxachiever3003
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 

Recently uploaded (20)

Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managament
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptx
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 

C07.heaps

  • 1. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 1Heaps Data Structures & File Management Heaps A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. A min-heap is defined similarly. 97 79 93 90 81 84 83 5542 2173 83 802173554283837990849397 11109876543210 Mapping the elements of a heap into an array is trivial: if a node is stored at index k, then its left child is stored at index 2k+1 and its right child at index 2k+2
  • 2. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 2Heaps Data Structures & File Management Building a Heap The fact that a heap is a complete binary tree allows it to be efficiently represented using a simple array. Given an array of N values, a heap containing those values can be built, in situ, by simply “sifting” each internal node down to its proper location: 81 73 74 9079 93 93 73 74 9079 81 93 73 90 7479 42 73 93 90 7479 81 - start with the last internal node - swap the current internal node with its larger child, if necessary - then follow the swapped node down - continue until all internal nodes are done * * * *
  • 3. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 3Heaps Data Structures & File Management Heap Class Interface We will consider a somewhat minimal heap class: template <class Item> class HeapT { private: Item* Hp; int numVertices; int Size; void SiftDown(int toSift); // helper for BuildHeap, etc. public: HeapT(); HeapT(Item* List, int numItems); // create from an array HeapT(const HeapT& Source); HeapT& operator=(const HeapT& Source); ~HeapT(); bool isEmpty() const; bool isLeaf(int Vertex) const; int leftChild(int Vertex) const; // traversal navigation int rightChild(int Vertex) const; int Parent(int Vertex) const; Item RemoveRoot(); // deletes root & re-heaps void BuildHeap(); // heapifies the array };
  • 4. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 4Heaps Data Structures & File Management BuildHeap and SiftDown As described earlier: template <class Item> void HeapT<Item>::BuildHeap() { for (int Idx = numVertices/2 - 1; Idx >= 0; Idx--) SiftDown(Idx); } template <class Item> void HeapT<Item>::SiftDown(int toSift) { while ( !isLeaf(toSift) ) { int MaxChild = leftChild(toSift); if ( (MaxChild < numVertices - 1) && (Hp[MaxChild] < Hp[MaxChild + 1]) ) MaxChild++; if (Hp[toSift] >= Hp[MaxChild]) return; Item tmpItem = Hp[toSift]; Hp[toSift] = Hp[MaxChild]; Hp[MaxChild] = tmpItem; toSift = MaxChild; } } Determine which child node is larger See if node must sift down If so, swap it with its larger child (maintaining heap property), and continue sifting down… QTP: Why is Idx initialized this way?
  • 5. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 5Heaps Data Structures & File Management Cost of SiftDown In a complete binary tree of N nodes, the number of levels is at most 1 + log(N). Since each non-terminating iteration of the loop moves the target value a distance of 1 level, the loop will perform no more than log(N) iterations. Thus, the worst case cost of SiftDown() is O(log N). template <class Item> void HeapT<Item>::SiftDown(int toSift) { while ( !isLeaf(toSift) ) { int MaxChild = leftChild(toSift); if ( (MaxChild < numVertices - 1) && (Hp[MaxChild] < Hp[MaxChild + 1]) ) MaxChild++; if (Hp[toSift] >= Hp[MaxChild]) return; Item tmpItem = Hp[toSift]; Hp[toSift] = Hp[MaxChild]; Hp[MaxChild] = tmpItem; toSift = MaxChild; } } In the worst case, we perform two element comparisons… …and one element swap per iteration That’s 2log(N) comparisons and log(N) swaps.
  • 6. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 6Heaps Data Structures & File Management Cost of BuildHeap Suppose we start with a complete binary tree with N nodes; the number of steps required for sifting values down will be maximized if the tree is also full, in which case N = 2d-1 for some integer d = élog Nù. For example: 42 55 83 90 81 21 93 3797 8473 83 95 62 17 level 0: 20 nodes, can sift down d - 1 levels level 1: 21 nodes, can sift down d - 2 levels level 2: 22 nodes, can sift down d - 3 levels We can prove that in general, level k of a full and complete binary tree will contain 2k nodes, and that those nodes are d – k – 1 levels above the leaves. Thus…
  • 7. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 7Heaps Data Structures & File Management Cost of BuildHeap In the worst case, the number of comparisons BuildHeap() will require in building a heap of N nodes is given by: Since, at worst, there is one swap for each two comparisons, the maximum number of swaps is N – élog Nù. Hence, building a heap of N nodes is O(N) in both comparisons and swaps. ( ) ( ) [ ] é ù[ ]log2122 22212122sComparison 1 0 1 0 1 1 0 NNd kdkd d d k d k kk d k k −=−−= ú û ù ê ë é −−=−−= å åå − = − = − − =
  • 8. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 8Heaps Data Structures & File Management Deleting the Root of a Heap We will see that the most common operation on a heap is the deletion of the root node. The heap property is maintained by sifting down… template <class Item> Item HeapT<Item>::RemoveRoot() { if (numVertices == 0) return Item(); Item tmpItem = Hp[0]; Hp[0] = Hp[numVertices - 1]; Hp[numVertices - 1] = tmpItem; numVertices--; SiftDown(0); return tmpItem; } Check for empty heap Swap the root with the last leaf, and shrink the heap by one node. Sift the new root down to restore the heap property. QTP: Why is the last leaf chosen as the replacement for the root?
  • 9. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 9Heaps Data Structures & File Management Example of Root Deletion Given the initial heap: In a heap of N nodes, the maximum distance the root can sift down would be éééélog Nùùùù - 1. 97 79 93 90 81 84 83 5542 2173 83 83 79 93 90 81 84 83 5542 217393 79 83 90 81 84 83 5542 2173
  • 10. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 10Heaps Data Structures & File Management Heap Sort A list can be sorted by first building it into a heap, and then iteratively deleting the root node from the heap until the heap is empty. If the deleted roots are stored in reverse order in an array they will be sorted in ascending order (if a max heap is used). void HeapSort(int* List, int Size) { HeapT<int> toSort(List, Size); toSort.BuildHeap(); int Idx = Size - 1; while ( !toSort.isEmpty() ) { List[Idx] = toSort.RemoveRoot(); Idx--; } }
  • 11. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 11Heaps Data Structures & File Management Cost of Deleting the Roots Recalling the earlier analysis of building a heap, level k of a full and complete binary tree will contain 2k nodes, and that those nodes are k levels below the root level. So, when the root is deleted the maximum number of levels the swapped node can sift down is the number of the level from which that node was swapped. Thus, in the worst case, for deleting all the roots… ( )[ ] é ù é ù NNNN dkk d d k k d k k 4log2log2 12242422sComparison 1 1 1 1 1 1 −+= +−=== − − = − − = åå As usual, with Heap Sort, this would entail half as many element swaps.
  • 12. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 12Heaps Data Structures & File Management Cost of Heap Sort Adding in the cost of building the heap from our earlier analysis, é ù( ) é ù é ù( ) é ù NNN NNNNNN 2log2 4log2log2log22sComparisonTotal −= −++−= and... So, in the worst case, Heap Sort is Θ(N log N) in both swaps and comparisons. é ù NNN −= logSwapsTotal
  • 13. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 13Heaps Data Structures & File Management Priority Queues A priority queue consists of entries, each of which contains a key field, called the priority of the entry. Aside from the usual operations of creation, clearing, tests for full and empty, and reporting its size, a priority queue has only two operations: - insertion of a new entry - removal of the entry having the largest (or smallest) key Key values need not be unique. If not, then removal may target any entry holding the largest value.
  • 14. Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 14Heaps Data Structures & File Management Representation Representation of a priority queue may be achieved using: - a sorted list, but… - an unsorted list, but… - a max-heap Priority queues may be used to manage prioritized processes in a time-sharing environment, time-dependent simulations, and even numerical linear algebra.