Академический Документы
Профессиональный Документы
Культура Документы
Lecture No. 33
___________________________________________________________________
Data Structures
Lecture No. 33
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 6, 8
6.3, 8.1
Summary
{
heap = new Heap <Event> ( PQMAX );
};
~PriorityQueue()
{
delete heap;
};
Event * remove()
{
if( !heap->isEmpty() )
{
Event * e;
heap->deleteMin( e );
return e;
}
cout << "remove - queue is empty." << endl;
return (Event *) NULL;
};
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();
};
};
The first line has a file Event.cpp that contains all the events used for simulation. We are
Page 2 of 9
Page 4 of 9
at each insertion in the array. This new implementation is more efficient as the heap can
readjust itself in log2N times. Gain in performance is the major benefit of implementing
PriorityQueue with heap as compared to implementation with array.
There are other significant benefits of the heap that will be covered in this course time to
time. At the moment, we will have some common example usages of heap to make you
clear about other uses of heap. The heap data structure will be covered in the Algorithms
course also.
Heap Sort
To take the 100th minimum element out from the min-heap, we will call deleteMin to take
out 1st element, 2nd element, 3rd element. Eventually we will call deleteMin 100th time to
take out our required 100th minimum element. Suppose, if the size of the heap is 100
elements, we have taken out all the elements out from the heap. Interestingly the elements
are sorted in ascending order. If somehow, we can store these numbers, lets say in an
array, all the elements sorted (in ascending order in this min-heap case) can be had.
Hence,
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 fine-tune this idea to obtain a fast sorting algorithm called
heapsort.
We conclude our discussion on the heap here. However, it will be discussed in the
forthcoming courses. At the moment, let see another Abstract Data Type.
Page 6 of 9
Page 7 of 9
``
Fig 33.1
This image is black and white, consisting of five non-overlapping black colored regions
of different shapes, called blobs. These blobs are two elliptics- n and u shaped (two
blobs) and one arc at the bottom. We can see these five blobs. How can robot identify
them? So the problem is:
We want to partition the pixels into disjoint sets, one set per blob.
If we make one set per blob, there will be five sets for the above image. To understand
the concept of disjoint sets, we can take an analogy with two sets- A and B (as in
Mathematics) where none of the elements inside set A is present in set B. The sets A and
B are called disjoint sets.
Another problem related to the Computer Vision is the image segmentation problem. See
the image below on the left of an old ship in gray scales.
Fig 33.2
We want to find regions of different colors in this picture e.g. all regions in the picture of
color black and all regions in the image of color gray. The image on the right represents
the resultant image.
Different scanning processes carried out in hospitals through MRI (Magnetic Resonance
Imaging), CAT Scan or CT Scan views the inner parts of the whole body of human
Page 8 of 9
Equivalence Relations
Lets discuss some Mathematics about sets. You might have realized that Mathematics is
handy whenever we perform analysis of some data structure.
A binary relation R over a set S is called an equivalence relation if it has following
properties
1.
Reflexivity: for all element x S, x R x
2.
Symmetry: for all elements x and y, x R y if and only if y R x
3.
Transitivity: for all elements x, y and z, if x R y and y R z then x R z
The relation is related to is an equivalence relation over the set of people.
You are advised to read about equivalence relations yourself from your text books or
from the internet.
Page 9 of 9