Download presentation
Presentation is loading. Please wait.
Published byAda Chambers Modified over 8 years ago
1
Priority Queue Using Heap #include “Event.cpp” #include “Heap.cpp” #define PQMAX 30 class PriorityQueue { public: PriorityQueue() { heap = new Heap ( PQMAX ); }; ~PriorityQueue() { delete heap; };
2
Lecture No.33 Data Structure Dr. Sohail Aslam
3
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; };
4
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(); }; };
5
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 k th smallest of these is at the k th position.
6
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.
7
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.
8
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”.
9
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?”
10
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.
11
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.
12
Disjoint Set ADT The image segmentation problem.
13
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
14
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.