COMP 103 Lecture 1

Download Report

Transcript COMP 103 Lecture 1

COMP 103
(these notes same as “lecture 30”...)
Heaps, and HeapSort
 Lindsay Groves, Marcus Frean , Peter Andreae, and Thomas Kuehne, VUW
2014-T2 Lecture 31
Marcus Frean
School of Engineering and Computer Science, Victoria University of Wellington
RECAP-TODAY
2
RECAP
 Priority
Queue
TODAY
 Heap
 Array-based
Heap
 HeapSort


TreeSort is a fast InsertionSort
HeapSort is a fast SelectionSort
Heap
3

A complete, partially ordered, binary tree
complete = every level full, except bottom, where nodes are to the left

Implemented in an array using breadth-first order
Bee
35
Kea
19
Eel
26
Dog
14
Jay
2
Fox
7
Gnu
13
Hen
23
Ant
9
Cat
4
Bee
35
Kea
19
Eel
26
Dog
14
Fox
7
Hen
23
Ant
9
Jay
2
Gnu
13
Cat
4
0
1
2
3
4
5
6
7
8
9
Heap
4

Bottom right node is last element used

We can compute the index of parent and children of a node:


the children of node i are at (2i+1) and (2i+2)
the parent of nodei is at (i-1)/2
Bee
35
Kea
19
Eel
26
Dog
14
Fox
7
Hen
23
Ant
9
Jay
2
Gnu
13
Cat
4
0
1
2
3
4
5
6
7
8
9
Bee
35
Kea
19
Dog
14

Gasp! There are no gaps!
Jay
2
Eel
26
Fox
7
Gnu
13
Cat
4
Hen
23
Ant
9
Heap: add
5
10
11
Bee
35
Kea
19
Eel
26
Dog
14
Fox
7
Hen
23
Ant
9
Jay
2
Gnu
13
Cat
4
Pig
21
0
1
2
3
4
5
6
7
8
9
10
11
Insert at bottom of tree and bubble up:

Put new item at end:

Compare with parent:
 If


p = (10-1)/2 = 4 ⇒ Fox/7
larger than parent  swap
Compare with parent:
 If
10
p = (4-1)/2 = 1
⇒ Kea/19
larger than parent  swap
Compare with parent:
p = (1-1)/2 = 0
⇒ Bee/35
12
Heap: remove
6
10
11
Bee
35
Pig
21
Eel
26
Dog
14
Kea
19
Hen
23
Ant
9
Jay
2
Gnu
13
Cat
4
Fox
7
0
1
2
3
4
5
6
7
8
9
10

Remove item at 0:

Move last item to 0

Find largest child



c1 = 20+1 = 1, c2 = 20+2 = 2
if largest child is larger  swap
Find largest child

11
c1 = 22+1 = 5, c2 = 22+2 = 6
if largest child is larger  swap
Find largest child
c1 = 25+1 = 11 : No such child
12
HeapQueue: Analysis
7

Cost of offer:
= cost of “bubble up”
= O(log(n))
log(n) comparisons, 2 log(n) assignments

Cost of poll:
= cost of “sink down”
= O(log(n))
2 log(n) comparisons, 2 log(n) assignments

Conclusion: HeapQueue is always fast!
Remember: Tree Sort
8

To sort a list:




TreeSort is like
InsertionSort with a
fast structure to
insert into
Insert each item into a BST
Traverse the BST, reading out elements one by one
Note: not an "in-place" sort
Cost is O(n log n),
if the tree is kept balanced, otherwise O(n2) worst-case
Priority Queue Based Sort
9


If we can use a binary search tree for sorting,
can we use a priority queue for sorting?
Yes  High-level idea:
 enqueue
all items into a priority queue
 dequeue
items from priority queue one by one
Priority Queue Sort
is like SelectionSort
with a fast structure
to select items from
Priority Queue Based Sort: Complexity
10

Add an element to a priority queue:

Remove an element from a priority queue: O(log(n))

Do it n times

No O(n2) worst-case scenario!

O(log(n))
O(n log n)
Heapsort: In-Place Sorting
11

Use an array-based Heap
 in-place
sorting algorithm!
2.
turn the array into a heap
“remove” top element, and
restore heap property again
3.
repeat step 2. n-1 times
1.
in-place
dequeueing
Sorted →
35
19
26
13
14
23
4
9
7
1
0
1
2
3
4
5
6
7
8
9
and so on!
Heapsort: In-Place Sorting
12
How to turn the array into a heap?
(n-2)/2
Heapify
for i = lastParent down to 0
sinkdown(i)
heap property
installed
13
9
23
7
14
26
4
19
35
1
0
1
2
3
4
5
6
7
8
9
HeapSort: Algorithm
13
(a) Turn data into a heap
(b) Repeatedly swap root with last item and push down
public void heapSort(E[] data, int size, Comparator<E> comp) {
for (int i = (size-2)/2; i >= 0; i--)
"heapify"
sinkDown(i, data, size, comp);
while (size > 0) {
size--;
swap(data, size, 0);
sinkDown(0, data, size, comp);
}
}
in-place
dequeueing
Cost of “heapify”
14
log (n+1)-1)  (log(n+1)-1)
(n/2
swaps =
⋮
⋮
swaps = n/82
swaps = n/41
swaps = n/20
Cost = n [1/4 + 2/8 + 3/16 + 4/32 + ⋯ (log(n+1)-1)/n]
Cost of “heapify”
15
Cost = n  [ 1/4 + 2/8 + 3/16 + 4/32 + ⋯]
= n  [ (1/4 + 1/8 + 1/16 + 1/32 + ⋯ ) +
(
1/8 + 1/16 + 1/32 + ⋯ ) +
(
1/16 + 1/32 + ⋯ ) +
⋮
⋮
1st
row
2nd
row
1st
row
2nd
row
3rd
row
3rd
row
≈ n  [ 1/2 + 1/4 + 1/8 + ⋯]
≈ n  [1]

=
n swaps
We can turn an unordered list into a heap in linear time!!!
HeapSort: Summary
16

Cost of heapify
= O(n)
n  Cost of remove = O(n log n)
 Total cost
= O(n log n)


True for worst-case and average case!
 unlike

Can be done in-place
 Unlike

QuickSort and TreeSort
MergeSort, doesn’t need extra memory to be fast
Not stable 