Class No.27  Data Structures http://ecomputernotes.com
Priority Queue Using Heap <ul><li>#include “Event.cpp” </li></ul><ul><li>#include “Heap.cpp” </li></ul><ul><li>#define PQM...
Priority Queue Using Heap <ul><li>Event* remove() </li></ul><ul><li>{ </li></ul><ul><li>if( !heap->isEmpty() ) { </li></ul...
Priority Queue Using Heap <ul><li>int insert(Event* e)  </li></ul><ul><li>{ </li></ul><ul><li>if( !heap->isFull() ) { </li...
The Selection Problem <ul><li>Given a list of  N  elements (numbers, names etc.), which can be totally ordered, and an int...
The Selection Problem <ul><li>A faster way is to put the  N  elements into an array and apply the  buildHeap  algorithm on...
HeapSort <ul><li>If  k  =  N , and we record the  deleteMin  elements as they come off the heap, we will have essentially ...
Disjoint Set ADT <ul><li>Suppose we have a database of people. </li></ul><ul><li>We want to figure out who is related to w...
Disjoint Set ADT <ul><li>Key property: If Haaris is related to Saad  and Saad is related to Ahmad, then Haaris is related ...
Disjoint Set ADT <ul><li>Blob Coloring </li></ul><ul><li>A well-known low-level computer vision problem for black and whit...
Disjoint Set ADT <ul><li>Blob Coloring </li></ul><ul><li>Thus in the following image, there are five blobs.  </li></ul><ul...
Disjoint Set ADT <ul><li>The image segmentation problem. </li></ul>http://ecomputernotes.com
Equivalence Relations <ul><li>A binary relation    over a set S is called an  equivalence relation  if it has following p...
Equivalence Relations <ul><li>The    relationship is  not  an  equivalence relation.  </li></ul><ul><li>It is reflexive, ...
Upcoming SlideShare
Loading in …5
×

Computer notes - Heap Sort

987 views

Published on

If k = N, and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements. We will refine this idea to obtain a fast sorting algorithm called heapsort

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
987
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
42
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Start of lecture 33
  • buildHeap is O ( N ) operation. Each deleteMin is O (log 2 N ). The total running time is thus O ( N + k log 2 N ). If k is small, or O ( N / log 2 N ), then the time required is O ( N ).
  • End of lecture 33, Start of lecture 34
  • Computer notes - Heap Sort

    1. 1. Class No.27 Data Structures http://ecomputernotes.com
    2. 2. Priority Queue Using Heap <ul><li>#include “Event.cpp” </li></ul><ul><li>#include “Heap.cpp” </li></ul><ul><li>#define PQMAX 30 </li></ul><ul><li>class PriorityQueue </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>PriorityQueue() { </li></ul><ul><li>heap = new Heap<Event>( PQMAX ); </li></ul><ul><li>}; </li></ul><ul><li>~PriorityQueue() { </li></ul><ul><li>delete heap; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    3. 3. Priority Queue Using Heap <ul><li>Event* remove() </li></ul><ul><li>{ </li></ul><ul><li>if( !heap->isEmpty() ) { </li></ul><ul><li>Event* e; </li></ul><ul><li>heap->deleteMin( e ); </li></ul><ul><li>return e; </li></ul><ul><li>} </li></ul><ul><li>return (Event*)NULL; </li></ul><ul><li>cout << &quot;remove - queue is empty.&quot; << endl; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    4. 4. Priority Queue Using Heap <ul><li>int insert(Event* e) </li></ul><ul><li>{ </li></ul><ul><li>if( !heap->isFull() ) { </li></ul><ul><li>heap->insert( e ); </li></ul><ul><li> return 1; </li></ul><ul><li>} </li></ul><ul><li>cout << &quot;insert queue is full.&quot; << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>}; </li></ul><ul><li>int full(void){ </li></ul><ul><li>return heap->isFull(); </li></ul><ul><li>}; </li></ul><ul><li>int length() { return heap->getSize(); }; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    5. 5. The Selection Problem <ul><li>Given a list of N elements (numbers, names etc.), which can be totally ordered, and an integer k , find the k th smallest (or largest) element. </li></ul><ul><li>One way is to put these N elements in an array an sort it. The k th smallest of these is at the k th position. </li></ul>http://ecomputernotes.com
    6. 6. The Selection Problem <ul><li>A faster way is to put the N elements into an array and apply the buildHeap algorithm on this array. </li></ul><ul><li>Finally, we perform k deleteMin operations. The last element extracted from the heap is our answer. </li></ul><ul><li>The interesting case is k =  N / 2  , since this is known as the median . </li></ul>http://ecomputernotes.com
    7. 7. HeapSort <ul><li>If k = N , and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements. </li></ul><ul><li>Later in the course, we will refine this idea to obtain a fast sorting algorithm called heapsort . </li></ul>http://ecomputernotes.com
    8. 8. Disjoint Set ADT <ul><li>Suppose we have a database of people. </li></ul><ul><li>We want to figure out who is related to whom. </li></ul><ul><li>Initially, we only have a list of people, and information about relations is gained by updates of the form “Haaris is related to Saad”. </li></ul>http://ecomputernotes.com
    9. 9. Disjoint Set ADT <ul><li>Key property: If Haaris is related to Saad and Saad is related to Ahmad, then Haaris is related to Ahmad. </li></ul><ul><li>Once we have relationships information, we would like to answer queries like “Is Haaris related to Ahmad?” </li></ul>http://ecomputernotes.com
    10. 10. Disjoint Set ADT <ul><li>Blob Coloring </li></ul><ul><li>A well-known low-level computer vision problem for black and white images is the following: </li></ul><ul><ul><li>Gather together all the picture elements (pixels) that belong to the same &quot;blobs&quot;, and give each pixel in each different blob an identical label. </li></ul></ul>http://ecomputernotes.com
    11. 11. Disjoint Set ADT <ul><li>Blob Coloring </li></ul><ul><li>Thus in the following image, there are five blobs. </li></ul><ul><li>We want to partition the pixels into disjoint sets , one set per blob. </li></ul>http://ecomputernotes.com
    12. 12. Disjoint Set ADT <ul><li>The image segmentation problem. </li></ul>http://ecomputernotes.com
    13. 13. Equivalence Relations <ul><li>A binary relation  over a set S is called an equivalence relation if it has following properties </li></ul><ul><ul><li>Reflexivity: for all element x  S, x  x </li></ul></ul><ul><ul><li>Symmetry: for all elements x and y, x  y if and only if y  x </li></ul></ul><ul><ul><li>Transitivity: for all elements x, y and z, if x  y and y  z then x  z </li></ul></ul><ul><li>The relation “is related to” is an equivalence relation over the set of people </li></ul>http://ecomputernotes.com
    14. 14. Equivalence Relations <ul><li>The  relationship is not an equivalence relation. </li></ul><ul><li>It is reflexive, since x  x, </li></ul><ul><li>and transitive, since x  y and y  z implies x  z, </li></ul><ul><li>it is not symmetric since x  y does not imply y  x. </li></ul>http://ecomputernotes.com

    ×