9_heaps - Priority
Queues
and
Heaps
 ! 

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Priority
Queues
and
Heaps
 !  A
queue
should
implement
at
least
the
first
 two
of
these
operations:
 !  insert
–
insert
item
at
the
back
of
the
queue
 !  remove
–
remove
an
item
from
the
front

 !  peek
–
return
the
item
at
the
front
of
the
queue
 !  It
is
assumed
that
these
operations
will
be
 without
removing
it
 implemented
efficiently
 !  That
is,
in
constant
time
 John Edgar October 2004 2 !  Either
with
an
array
 4 0 18 25 1 2 2 3 5 4 5 6 7 6 !  Or
with
a
linked
list
 front back 4 7 3 October 2004 John Edgar 3 !  Queues
are
first‐in
first‐out
(FIFO)
 !  A
fancier
type
of
queue,
which
maintains
an
 ordering
is
a
priority
queue
 October 2004 John Edgar 4 September 2004 John Edgar 5 !  Items
in
a
priority
queue
are
given
a
priority
 value
 !  The
highest
priority
item
is
removed
first
 !  
Uses
include
 !  System
requests
 !  Data
structure
to
support
Dijkstra’s
Algorithm
 !  Which
could
be
numerical
or
something
else
 September 2004 John Edgar 6 !  Can
items
be
inserted
and
removed
 efficiently
from
a
priority
queue?
 !  Using
an
array,
or
 !  Using
a
linked
list?
 !  Note
that
items
are
not
removed
based
on
 the
order
in
which
they
are
inserted
 Now we’ll see how we can do these efficiently (using a different data structure) September 2004 John Edgar 7 !  Items
in
a
priority
queue
have
a
priority
 !  Not
necessarily
numerical
 !  Could
be
lowest
first
or
highest
first
 !  The
highest
priority
item
is
removed
first
 !  Priority
queue
operations
 !  Insert
 !  Remove
in
priority
queue
order
 ▪  Both
operations
should
be
performed
in
at
most
O(log
n)
 time
 October 2004 John Edgar 8 September 2004 John Edgar 9 !  Items
have
to
be
removed
in
priority
order
 !  This
can
only
be
done
efficiently
if
the
items
are
ordered
in
 !  One
option
would
be
to
use
a
balanced
binary
 search
tree

 !  Binary
search
trees
are
fully
ordered
and
insertion
and
 some
way
 removal
can
be
implemented
in
O(log
n)
time
 !  There
is
a
much
simpler
binary
tree
solution

 John Edgar ▪  Some
operations
(e.g.
removal)
are
complex
 ▪  Although
operations
are
O(logn)
they
require
quite
a
lot
of
 structural
overhead
 October 2004 10 !  !  A
heap
is
binary
tree
with
two
properties
 Heaps
are
complete
 !  All
levels,
except
the
bottom,
are
completely
filled
in
 !  The
leaves
on
the
bottom
level
are
as
far
to
the
left
as
 !  Heaps
are
partially
ordered
 !  The
value
of
a
node
is
at
least
as
large
as
its
children’s
 possible.
 values,
for
a
max
heap
or
 !  The
value
of
a
node
is
no
greater
than
its
children’s
values,
 for
a
min
heap
 October 2004 John Edgar 11 complete
binary
trees
 incomplete
binary
trees
 October 2004 John Edgar 12 98
 86
 41
 13
 65
 32
 29
 9
 10
 44
 23
 21
 17
 Heaps
are
not
fully
ordered,
an
inorder
traversal
would
result
in:
 9,
13,
10,
86,
44,
65,
23,
98,
21,
32,
17,
41,
29
 October 2004 John Edgar 13 !  !  A
heap
can
implement
a
priority
queue
 The
item
at
the
top
of
the
heap
must
always
be
the
 highest
priority
value
 Implement
priority
queue
operations:
 !  Insertions
–
insert
an
item
into
a
heap

 !  Removal
–
remove
and
return
the
heap’s
root
 !  For
both
operations
preserve
the
heap
property
 !  Because
of
the
partial
ordering
property

 !  October 2004 John Edgar 14 September 2004 John Edgar 15 !  !  Heaps
can
be
implemented
 using
arrays
 There
is
a
natural
method
of
 indexing
tree
nodes
 !  Index
nodes
from
top
to
bottom
 0 
 1
 2 
 and
left
to
right
as
shown
on
the
 right
 !  Because
heaps
are
complete
 binary
trees
there
can
be
no
 gaps
in
the
array
 3 
 4 
 5 
 6
 October 2004 John Edgar 16 !  It
will
be
necessary
to
find
the
index
of
the
 parents
of
a
node
 !  The
array
is
indexed
from
0
to
n
–
1
 !  Each
level's
nodes
are
indexed
from:
 ▪  2level
–
1
to
2level+1
–
2
(where
the
root
is
level
0)
 !  Or
the
children
of
a
node
 !  The
children
of
a
node
i,
are
the
array
elements
 indexed
at
2i
+
1
and
2i
+
2
 !  The
parent
of
a
node
i,
is
the
array
element
 indexed
at
(i
–
1)
/
2
 October 2004 John Edgar 17 98
 Heap 13
 86
 0 
 41
 1
 4 
 5 
 2 
 6
 3 
 65
 32
 29
 9
 index value 0 7 
 10
 1 2 8 
 44
 3 13 9
 23
 5 32 10 
 21
 7 9 11
 17
 8 10 12 
 4 65 6 29 9 44 10 23 11 21 12 17 98 86 41 Underlying
Array
 October 2004 John Edgar 18 September 2004 John Edgar 19 !  On
insertion
the
heap
properties
have
to
be
 maintained;
remember
that
 !  A
heap
is
a
complete
binary
tree
and
 !  There
are
two
general
strategies
that
could
be
used
 to
maintain
the
heap
properties
 !  Make
sure
that
the
tree
is
complete
and
then
fix
the
 !  A
partially
ordered
binary
tree
 ordering
or
 !  Make
sure
the
ordering
is
correct
first
 !  Which
is
better?
 October 2004 John Edgar 20 !  The
insertion
algorithm
first
ensures
that
the
tree
is
 complete
 !  Make
the
new
item
the
first
available
(left‐most)
leaf
on
 !  Fix
the
partial
ordering
 the
bottom
level
 !  i.e.
the
first
free
element
in
the
underlying
array
 !  Compare
the
new
value
to
its
parent
 !  Swap
them
if
the
new
value
is
greater
than
the
parent
 !  Repeat
until
this
is
not
the
case
 ▪  Referred
to
as
bubbling
up,
or
trickling
up
 October 2004 John Edgar 21 Insert
81
 86
 98
 41
 13
 65
 32
 29
 9
 10
 44
 23
 21
 17
 index value 0 1 2 3 4 5 6 7 9 8 9 10 11 12 17 13
 98 86 41 13 65 32 29 10 44 23 21 October 2004 John Edgar 22 Insert
81
 86
 98
 41
 13
 65
 32
 29
 81
 9
 10
 44
 23
 21
 17
 29
 81
 (13‐1)/2
=
6
 index value October 2004 0 1 2 3 4 5 6 7 9 8 9 10 11 12 17 13
 29
 81
 23 98 86 41 13 65 32 29 81 John Edgar 10 44 23 21 Insert
81
 86
 98
 81
is
less
than
98
so
finished
 81
 41
 13
 65
 32
 29
 81
 41
 9
 10
 44
 23
 21
 17
 29
 81
 (13‐1)/2
=
6
 index value October 2004 0 1 2 3 4 5 6 7 9 8 9 10 11 12 17 13
 29
 81
 24 98 86 41 13 65 32 29 8 41 8 John Edgar 10 44 23 21 Insert
81
 86
 98
 81
is
less
than
98
so
finished
 81
 41
 13
 65
 32
 29
 41
 81
 9
 10
 44
 23
 21
 (6‐1)/2
=
2
 17
 29
 81
 index value October 2004 0 1 2 3 4 5 6 7 9 8 9 10 11 12 17 13
 29
 81
 25 98 86 41 13 65 32 29 8 41 8 John Edgar 10 44 23 21 September 2004 John Edgar 26 !  !  Make
a
temporary
copy
of
the
root’s
data
 Similarly
to
the
insertion
algorithm,
first
ensure
that
 the
heap
remains
complete
 !  Replace
the
root
node
with
the
right‐most
leaf
 !  i.e.
the
highest
(occupied)
index
in
the
array
 !  Swap
the
new
root
with
its
largest
valued
child
until
 the
partially
ordered
property
holds
 !  i.e.
bubble
down
 !  Return
the
root’s
data
 John Edgar 27 October 2004 Remove
(root)
 86
 98
 41
 13
 65
 32
 29
 9
 index value 0 1 10
 2 44
 3 13 4 65 23
 5 32 6 29 21
 7 9 8 10 17
 9 44 10 23 11 21 12 17 98 86 41 October 2004 John Edgar 28 Remove
(root)
 86
 97
 18
 replace
root
with
right‐most
leaf
 41
 13
 65
 32
 29
 9
 index index value 0 1 10
 2 44
 3 13 4 65 23
 5 32 6 29 21
 7 9 8 10 17
 9 44 10 23 11 21 12 17 97 86 41 18 October 2004 John Edgar 29 Remove
(root)
 17
 86
 ?
 87
 16
 ?
 41
 swap
with
larger
child
 13
 65
 32
 29
 9
 index value 0 1 10
 2 44
 3 13 4 65 23
 5 32 6 29 21
 7 9 8 10 9 44 10 23 11 21 12 86 86 41 17 17 children
of
root:
2*0+1,
2*0+2
=
1,
2
 October 2004 John Edgar 30 Remove
(root)
 ?
 15
 67
 ?
 86
 41
 swap
with
larger
child
 13
 67
 15
 32
 29
 9
 index value 0 1 10
 2 41 44
 3 13 4 67 15 23
 5 32 6 29 21
 7 9 8 10 9 44 10 23 11 21 12 86 67 15 children
of
1:
2*1+1,
2*1+2
=
3,
4
 October 2004 John Edgar 31 Remove
(root)
 65
 86
 41
 swap
with
larger
child
 13
 ?
 44
 17
 ? 32
 29
 9
 index value 0 1 10
 2 41 44
 17
 3 13 4 44 17 23
 5 32 6 29 21
 7 9 8 10 9 44 17 10 23 11 21 12 86 65 children
of
4:
2*4+1,
2*4+2
=
9,
10
 October 2004 John Edgar 32 September 2004 John Edgar 33 !  Helper
functions
are
usually
written
for
preserving
 the
heap
property
 !  bubbleUp
ensures
that
the
heap
property
is
preserved
from
 !  These
functions
may
be
implemented
recursively
or
 iteratively
 the
start
node
up
to
the
root
 !  bubbleDown
ensures
that
the
heap
property
is
preserved
 from
the
start
node
down
to
the
leaves
 October 2004 John Edgar 34 Go to Eclipse
 October 2004 John Edgar 35 October 2004 John Edgar 36 October 2004 John Edgar 37 October 2004 John Edgar 38 !  For
both
insertion
and
removal
the
heap
performs
 at
most
height
swaps
 !  For
insertion
at
most
height
comparisons
 ▪  To
bubble
up
the
array
 !  For
removal
at
most
height
*
2
comparisons
 ▪  To
bubble
down
the
array
(have
to
compare
two
children)
 !  Height
of
a
complete
binary
tree
is
log2(n)
 !  Both
insertion
and
removal
are
therefore
O(logn)
 October 2004 John Edgar 39 September 2004 John Edgar 40 !  !  !  !  Observation
1:
Removal
of
a
node
from
a
heap
can
 be
performed
in
O(logn)
time
 Observation
2:
Nodes
are
removed
in
order
 Conclusion:
Removing
all
of
the
nodes
one
by
one
 would
result
in
sorted
output
 Analysis:
Removal
of
all
the
nodes
from
a
heap
is
a
 O(n*logn)
operation
 October 2004 John Edgar 41 !  !  A
heap
can
be
used
to
return
sorted
data
 However,
we
can’t
assume
that
the
data
to
be
 sorted
just
happens
to
be
in
a
heap!
 !  Aha!
But
we
can
put
it
in
a
heap.
 !  Inserting
an
item
into
a
heap
is
a
O(logn)
operation
so
 !  In
O(n*logn)
time
 !  But
we
can
do
better
thanjust
repeatedly
calling
the
 insertion
algorithm

 inserting
n
items
is
O(n*logn)

 October 2004 John Edgar 42 !  To
create
a
heap
from
an
unordered
array
 repeatedly
call
bubbleDown
 !  Any
subtree
in
a
heap
is
itself
a
heap
 !  Call
bubbleDown
on
elements
in
the
upper
½
of
the
array

 !  Start
with
index
n/2
and
work
up
to
index
0
 ▪  i.e.
from
the
last
non‐leaf
node
to
the
root
 !  bubbleDown
does
not
need
to
be
called
on
the
lower
 half
of
the
array
(the
leaves)

 !  Since
bubbleDown
restores
the
partial
ordering
from
any
 given
node
down
to
the
leaves
 October 2004 John Edgar 43 Assume
unsorted
input
is
contained
in 
an
array
as
shown
here
(indexed
from 
top
to
bottom
and
left
to
right)
 29
 1
 3 
 4 
 89
 0 23
 5 
 2 
 36
 48
 94
 13
 6
 27
 7 
 70
 8 
 76
 9
 37
 10 
 42
 11
 58
 12 
 October 2004 John Edgar 44 n
=
12,
(n‐1)/2
=
5
 bubbleDown(5)
 29
 1
 3 
 4 
 5 
 89
 0 23
 2 
 36
 48
 94
 13
 6
 27
 7 
 70
 8 
 76
 9
 37
 10 
 42
 11
 58
 12 
 October 2004 John Edgar 45 n
=
12,
(n‐1)/2
=
5
 bubbleDown(5)
 bubbleDown(4)
 29
 1
 3 
 4 
 5 
 89
 0 23
 2 
 36
 48
 76
 94
 13
 6
 27
 7 
 70
 8 
 48
 76
 9
 37
 10 
 42
 11
 58
 12 
 October 2004 John Edgar 46 n
=
12,
(n‐1)/2
=
5
 bubbleDown(5)
 bubbleDown(4)
 bubbleDown(3)
 3 
 29
 1
 4 
 5 
 89
 0 23
 2 
 70
 36
 76
 94
 13
 6
 27
 7 
 36
 70
 8 
 48
 9
 37
 10 
 42
 11
 58
 12 
 October 2004 John Edgar 47 n
=
12,
(n‐1)/2
=
5
 bubbleDown(5)
 bubbleDown(4)
 bubbleDown(3)
 bubbleDown(2)
 70
 3 
 29
 1
 4 
 5 
 89
 0 94
 23
 2 
 76
 94
 58
 23
 13
 6
 27
 7 
 36
 8 
 48
 9
 37
 10 
 42
 11
 58
 23
 12 
 October 2004 John Edgar 48 n
=
12,
(n‐1)/2
=
5
 bubbleDown(5)
 bubbleDown(4)
 bubbleDown(3)
 bubbleDown(2)
 bubbleDown(1)
 70
 3 
 29
 1
 76
 4 
 5 
 89
 0 94
 2 
 76
 29
 48
 58
 13
 6
 27
 7 
 36
 8 
 48
 29
 9
 37
 10 
 42
 11
 23
 12 
 October 2004 John Edgar 49 n
=
12,
(n‐1)/2
=
5
 bubbleDown(5)
 bubbleDown(4)
 bubbleDown(3)
 bubbleDown(2)
 bubbleDown(1)
 bubbleDown(0)
 27
 7 
 36
 8 
 29
 9
 37
 10 
 42
 11
 23
 12 
 70
 3 
 76
 1
 4 
 5 
 94
 89
 0 89
 94
 2 
 48
 58
 13
 6
 October 2004 John Edgar 50 !  bubbleDown
is
called
on
half
the
array
 !  The
cost
for
bubbleDown
is
O(height)
 !  It
would
appear
that
heapify
cost
is
O(n*logn)
 !  !  In
fact
the
cost
is
O(n)
 The
analysis
is
complex
but
 !  bubbleDown
is
only
called
on
n/2
nodes
 !  and
mostly
on
sub‐trees
 October 2004 John Edgar 51 !  !  Heapify
the
array
 Repeatedly
remove
the
root
 !  After
each
removal
swap
the
root
with
the
last
element
in
 !  At
the
end
of
the
sort
the
array
will
be
sorted
in
 reverse
order
 the
tree
 !  The
array
is
divided
into
a
heap
part
and
a
sorted
part
 October 2004 John Edgar 52 !  The
algorithm
runs
in
O(n*logn)
time
 !  Considerably
more
efficient
than
selection
sort
 !  The
sort
can
be
carried
out
in‐place
 !  That
is,
it
does
not
require
that
a
copy
of
the
array
 and
insertion
sort
 ! The
same
(O)
efficiency
as
MergeSort
and
 QuickSort
 to
be
made
 October 2004 John Edgar 53 September 2004 John Edgar 54 !  Define
the
ADT
priority
queue
 !  Define
the
partially
ordered
property
 !  Define
a
heap
 !  Implement
a
heap
using
an
array
 !  Implement
the
heapSort
algorithm
 October 2004 John Edgar 55 !  Java
Ch.
12
 !  C++
Ch.
11
 October 2004 John Edgar 56 ...
View Full Document

Ask a homework question - tutors are online