computer notes - Data Structures - 27

Page 1

Class No.27 Data Structures http://ecomputernotes.com


Priority Queue Using Heap #include “Event.cpp” #include “Heap.cpp” #define PQMAX 30 class PriorityQueue { public: PriorityQueue() { heap = new Heap<Event>( PQMAX ); }; ~PriorityQueue() { delete heap; };

http://ecomputernotes.com


Priority Queue Using Heap Event* remove() { if( !heap->isEmpty() ) { Event* e; heap->deleteMin( e ); return e; } return (Event*)NULL; cout << "remove - queue is empty." << endl; };

http://ecomputernotes.com


Priority Queue Using Heap int insert(Event* e) { if( !heap->isFull() ) { heap->insert( e ); return 1; } cout << "insert queue is full." << endl; return 0; }; int full(void){ return heap->isFull(); }; };

int length() { return heap->getSize(); };

http://ecomputernotes.com


The Selection Problem  Given a list of N elements (numbers, names etc.), which can be totally ordered, and an integer k, find the kth smallest (or largest) element.  One way is to put these N elements in an array an sort it. The kth smallest of these is at the kth position.

http://ecomputernotes.com


The Selection Problem  A faster way is to put the N elements into an array and apply the buildHeap algorithm on this array.  Finally, we perform k deleteMin operations. The last element extracted from the heap is our answer.  The interesting case is k = N/2, since this is known as the median. http://ecomputernotes.com


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

http://ecomputernotes.com


Disjoint Set ADT  Suppose we have a database of people.  We want to figure out who is related to whom.  Initially, we only have a list of people, and information about relations is gained by updates of the form “Haaris is related to Saad”.

http://ecomputernotes.com


Disjoint Set ADT  Key property: If Haaris is related to Saad and Saad is related to Ahmad, then Haaris is related to Ahmad.  Once we have relationships information, we would like to answer queries like “Is Haaris related to Ahmad?”

http://ecomputernotes.com


Disjoint Set ADT Blob Coloring  A well-known low-level computer vision problem for black and white images is the following: Gather together all the picture elements (pixels) that belong to the same "blobs", and give each pixel in each different blob an identical label.

http://ecomputernotes.com


Disjoint Set ADT Blob Coloring  Thus in the following image, there are five blobs.

 We want to partition the pixels into disjoint sets, one set per blob. http://ecomputernotes.com


Disjoint Set ADT The image segmentation problem.

http://ecomputernotes.com


Equivalence Relations  A binary relation  over a set S is called an equivalence relation if it has following properties 1. Reflexivity: for all element xS, x  x 2. Symmetry: for all elements x and y, x  y if and only if y  x 3. Transitivity: for all elements x, y and z, if x  y and y  z then x  z

 The relation “is related to” is an equivalence relation over the set of people

http://ecomputernotes.com


Equivalence Relations  The  relationship is not an equivalence relation.  It is reflexive, since x  x,  and transitive, since x  y and y  z implies x  z,  it is not symmetric since x  y does not imply y  x. http://ecomputernotes.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.