Heap Sort-Data Structures and Representation-Lecture Slides, Slides of Data Structures and Algorithms

Prof. Chandrashekar Baag delivered this lecture at Amrita Vishwa Vidyapeetham. This lecture is part of lecture series on Data Structures and Representation course. It includes: Heap, Sort, Queues, Priority, Heaps, Implement, Binary, Tree, Nodes, Parent, Child, Siblings

Typology: Slides

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Heap sort
Algorithm and Data structure
Priority Queues
Priority Queues
In many situations, simple queues are inadequate, since first
in/first out scheduling has to be overruled using some priority
criteria.
E i f P
d t b
E
.g.
i
n
a
sequence
o
f
processes,
process
P
2may
nee
d t
o
b
e
executed before process P1for the proper functioning of a system,
even though P1was put on the queue of waiting processes before
P
2
.
P
2
.
Banks managing customer information
Often will remove or get the information about the customer
with the minimum bank account balance
Shared Printer queue
list of print jobs, must get next job with highest priority, and
higher-priority jobs always print before lower-priority jobs
I it ti lik th difi d
i
I
n
s
it
ua
ti
ons
lik
e
th
ese,
a
mo
difi
e
d
queue
or
pr
or
y
queue,
i
s
needed.
In priority queues, elements are dequeued according to their
priority and their current queue positions
priority and their current queue positions
.
Priority Queues
The problem with a priority queue is in finding an
efficient implementation which allows relatively fast
enqueuing and dequeuing
enqueuing and dequeuing
Since elements may arrive randomly to the queue,
there is no guarantee that the front elements will be
the most likely to be dequeued and that the elements
the most likely to be dequeued and that the elements
put at the end will be the last candidates for
dequeuing.
Th it ti i li t d b id t
Th
e
s
it
ua
ti
on
i
s
comp
li
ca
t
e
d b
ecause
a
w
id
e
spec
t
rum
of possible priority criteria can be used in different
cases.
Heaps as Priority Queues
A heap is an excellent way to implement a
priority queue.
3 j b h b b itt d t i t
3 j
o
b
s
h
ave
b
een
su
b
m
itt
e
d t
o
a
pr
i
n
t
er,
the jobs have sizes 100, 10, 1 page.
Average waiting time for FIFO service,
Average waiting time for FIFO service,
(100+110+111)/3 = 107 time units
Average waiting time for shortest job first
i (1 10 111)/3 36 67 ti it
serv
i
ce,
(1
+
10
+
111)/3
=
36
.
67 ti
me
un
it
s
Need to have a queue which does insert
and deletemin
and deletemin
Priority Queue
Heaps
A particular kind of binary tree, called a
hea
p
,
has the followin
g
p
ro
p
erties.
p
,gpp
1. The value of each node is not less than the
values stored in each of its children
2. The tree is an almost complete binary tree.
These two properties define a
max heap.
These two properties define a
max heap.
If “less” in the first property is replaced
with
greater
; then the definition specifies
with greater ; then the definition specifies
a min heap.
Heaps
Almost complete binary tree
All levels are complete except the lowest one
All levels are complete except the lowest one
(According to book complete and almost
complete Binary tree is same but this is a mistake
in most of the books, so plz concentrate only
Lecture slides.)
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Heap Sort-Data Structures and Representation-Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Heap sort

Algorithm and Data structure

Priority QueuesPriority Queues

† In many situations, simple queues are inadequate, since first in/first out scheduling has to be overruled using some priority criteria. †† EE.g. in a sequence of processes, process P i f P 2 may need to bed t b executed before process P 1 for the proper functioning of a system, even though P 1 was put on the queue of waiting processes before PP 2 2 .. „ Banks managing customer information † Often will remove or get the information about the customer with the minimum bank account balance „ Shared Printer queue † list of print jobs, must get next job with highest priority , and higher-priority jobs always print before lower-priority jobs †† IIn situations like these, a modified queue or it ti lik th difi d priority queue,i it iis needed. † In priority queues, elements are dequeued according to their priority and their current queue positionspriority and their current queue positions.

Priority Queues

† The problem with a priority queue is in finding an efficient implementation which allows relatively fast enqueuing and dequeuingenqueuing and dequeuing † Since elements may arrive randomly to the queue, there is no guarantee that the front elements will be the most likely to be dequeued and that the elementsthe most likely to be dequeued and that the elements put at the end will be the last candidates for dequeuing. †† ThThe situation is complicated because a wide spectrum it ti i li t d b id t of possible priority criteria can be used in different cases.

Heaps as Priority Queues

† A heap is an excellent way to implement a

priority queue.

†† 3 j3 jobs have been submitted to a printer, b h b b itt d t i t

the jobs have sizes 100, 10, 1 page.

†† Average waiting time for FIFO service,Average waiting time for FIFO service,

(100+110+111)/3 = 107 time units

† Average waiting time for shortest job first

service, (1+10+111)/3 = 36.67 time unitsi (1 10 111)/3 36 67 ti it

† Need to have a queue which does insert

and deleteminand deletemin

† Priority Queue

Heaps

† A particular kind of binary tree, called a

heapp ,, has the following properties.g p p

1. The value of each node is not less than the

values stored in each of its children

2. The tree is an almost complete binary tree.

†† These two properties define aThese two properties define a max heap.max heap.

† If “less” in the first property is replaced

with “greater”; then the definition specifieswith greater ; then the definition specifies

a min heap.

Heaps

† Almost complete binary tree

„„ All levels are complete except the lowest oneAll levels are complete except the lowest one † (According to book complete and almost complete Binary tree is same but this is a mistake in most of the books, so plz concentrate only Lecture slides.)

Almost Complete Binary Trees

† If the height of the tree is d, then all levels

except possibly level d are completely full.p p y p y

The bottom level has all nodes to the left

side.

Almost Complete Binary TreeAlmost Complete Binary Tree

0 A

k

B C

2k 2k 1 G

F G

E

D 2k 2k+ 7 8 9 10 11 H I J K L

N b i f t ’ d f t i A B C D E F G H I J K L

Numbering of a tree’s nodes for storage in an array

Finding parent/child or siblings

Numbering of a tree’s nodes for storage in an array

A B C D E F G H I J K L

For k th^ element:

RRoot is always at 0 index. t i l t 0 i d

Parent is at [(k-1)/2], using integer divide.

Left child is at 2k+1 andLeft child is at 2k+1 and

Right child is at 2k+2.

MinMin -HeapHeap

Insert a new element

Inserting a new element

† Insert the new element into the next

available slot in the array (preservingy (p g

the structure)

†† To preserve the order, check againstTo preserve the order, check against

its parent. If larger than parent, fine.

If smaller than parent, swap it withs a a pa , s ap

the parent (percolating up).

†† Keep checking until the the newKeep checking until the the new

element is in correct order.

Heap after DeleteMinHeap after DeleteMin

Are these Heaps?

What about these?

Which are min-heaps?

Which are max-heaps?

(^304880)

wrong!

21 10 25 30 14

(^10 ) (^1424)

50 30 (^10 1710 )

33 30 35

50

7 3

(^10 1710 ) 28 18 9 11

22

wrong!g 11

Enqueuing an element to a heap

† To enqueue an element, the element is added at the end of the heap as the last leaf. †† Restoring the heap property in the case of enqueuingRestoring the heap property in the case of enqueuing is achievied by moving from the last leaf toward the root. †† ThThe algorithm for enqueuing is as follows l ith f i i f ll heapEnqueue(el) put el at the end of heap;put el at the end of heap; while el is not in the root and el > parent(el) swap el with its parent

Enqueuing an element to a heap

Enqueuing an element to a heap

20

Dequeuing an element from heap

† Dequeuing an element from the heap

consists of removing the root element from

ththe heap, since h i

† By the heap property it is the element with

the greatest prioritythe greatest priority.

† Then the last leaf is put in its place, and the

heap property almost certainly has to beheap property almost certainly has to be

restored,

†† This time by moving down the root.This time by moving down the root.

Dequeuing an element from heap

† The algorithm for dequeuing is as follows:

heapDequeue()

extract the element from the root;

put the element from the last leaf in its

place;place;

remove the last leaf;

//both subtrees of the root are heaps;//both subtrees of the root are heaps;

p = root;

while p is not a leaf and p < any of itse p s o a ea a d p a y o s

children

swap p with the larger child;

Dequeuing an element from heap d 20

dequeue

Dequeuing an element from heap

Reference

† Mohammad Ikramulhaq „ FAST †† Data stData structures and algorithm in C++ , 2nd Edition, ct es and algo ithm in C++ 2nd Edition By Adam Drozdek (second edition), Brooks / Cole, Thomson Learning Inc. † Introduction to Data Structures and Algorithms with C++ By Glenn W. Rowe, Prentice-Hall, India †† Data structures and algorithm analysis in C++, ByData structures and algorithm analysis in C , By Mark Allen Weiss, Benjamin/Cummings Publishing Company, Inc. †† Data structures and Program Design in CData structures and Program Design in C, By RobertBy Robert L. Kruse ….. † \juweb\jinnah\Dr.A Qadir\CS2114 Summer