슬라이드 1 - Go into The Algorithm

Download Report

Transcript 슬라이드 1 - Go into The Algorithm

6. Heapsort
Heejin Park
College of Information and Communications
Hanyang University
1
Contents
Heaps
Building a heap
The heapsort algorithm
Priority queues
2
Heapsort
Like merge sort

Running time is O(nlgn)
Like insertion sort

Heapsort sorts in place.
3
Heaps
The shape of a (binary) heap

A nearly complete binary tree.

Complete binary tree is in which all leaves have the same depth
and all internal nodes have degree 2.
1
16
2
3
14
10
4
5
6
7
8
7
9
3
8
9
10
2
4
1
4
Heaps
Heap property

2 kinds of binary heaps



max-heaps and min-heaps
In the both kinds, the values of the nodes satisfy a heap property.
max-heap property

A[PARENT(i)] ≥ A[i]
 The parent is bigger than its child.
 The root node has the largest element.
 The root of any subtree a max-heap has the largest element
among the subtree.
5
Heaps

min-heap property

A[PARENT(i)] ≤ A[i]
 Organized in the opposite way.
 A child is bigger than its parent.
 The root node has the smallest element.
6
Heaps
A heap can be stored in an array.
The root is stored in A[1].
All elements are stored in level order.


1
16
2
3
14
10
4
5
6
7
8
7
9
3
8
9
10
2
4
1
1
2
3
4
5
6
7
8
9 10
16 14 10 8 7 9 3 2 4 1
7
Heaps

Given the index i of a node



PARENT(i)
i 
return  
2
LEFT(i)
return 2i
RIGHT(i)
return 2i + 1
1
16
2
3
14
10
4
5
6
7
8
7
9
3
8
9
10
2
4
1
1
2
3
4
5
6
7
8
9 10
16 14 10 8 7 9 3 2 4 1
8
Heaps
The height of a node in a heap

The number of edges on the longest simple downward path
from the node to a leaf.
The height of a heap


The height of the root.
Θ(lgn)

Since a heap of n elements is based on a complete binary tree.
9
Maintaining the heap property
Max-Heapify


Input : A node whose left and right subtrees are max-heaps, but
the value at the node may be smaller than those of its children,
thus violating the max-heap property.
Let the value at the node “float down” in the max-heap so that the
subtree rooted at the node becomes a max-heap.
1
16
2
3
4
10
4
5
6
7
14
7
9
3
8
9
10
2
8
1
10
Maintaining the heap property
1
1
16
16
2
3
2
3
4
10
14
10
4
5
6
7
4
5
6
7
14
7
9
3
4
7
9
3
8
9
10
8
9
10
2
8
1
2
8
1
1
16
2
3
14
10
4
5
6
7
8
7
9
3
8
9
10
2
4
1
11
Maintaining the heap property
The running time of MAX-HEAPIFY

T(n) where n is the number of nodes in the subtree.


Θ(1) time to exchange values
O(h) = O(lg n) time in total
12
Building a heap
BUILD-MAX-HEAP

The input array with 10 elements and its binary tree
representation.
1
4
4
1
3
2
16
9
10 14
8
7
2
3
1
3
4
5
6
7
2
16
9
10
8
9
10
14
8
7
13
Building a heap

Call MAX-HEAPIFY(A, i) at the rightmost node that has
the child from the bottom. i  Size(A) / 2
1
1
4
4
2
3
2
3
1
3
1
3
4
5
6
7
4
5
6
7
2
16
9
10
2
16
9
10
8
9
10
8
9
10
14
8
7
14
8
7
14
Building a heap
1
1
4
4
2
3
2
3
1
3
1
10
4
5
6
7
4
5
6
7
14
16
9
10
14
16
9
3
8
9
10
8
9
10
2
8
7
2
8
7
15
Building a heap
1
1
4
16
2
3
2
3
16
10
14
10
4
5
6
7
4
5
6
7
14
7
9
3
8
7
9
3
8
9
10
8
9
10
2
8
1
2
4
1
16
Building a heap
Running time

Upper bound


Each call to MAX-HEAPIFY costs O(lgn) time, and there are O(n)
such calls, Thus, the running time is O(nlgn).
Tighter bound


The time for MAX-HEAPIFY to run at a node varies with the height
of the node in the tree, and the heights of most nodes are small.
Our tighter analysis relies on the properties that an n-element heap
has height ⌊lgn⌋ and at most ⌈n/2h+1⌉ nodes of any height h.
17
Building a heap
Tighter bound

The running time of MAX-HEAPIFY on a node of height h
is O(h), so the total cost of BUILD-MAX-HEAP is
lg n 
lg n  h
 n 

 2h1 (h)  (n  2h )
h 0
h 0

The last summation can be evaluated as follows.

h
1/ 2

2

h
2
(1  1 / 2)
h 0 2
18
Building a heap

Thus, the running time of BUILD-MAX-HEAP can be
bounded as
lg n 

h
h
(n  h )  (n h )  (n)
h 0 2
h 0 2

Hence, we can build a max-heap in linear time.
19
Heapsort
The Heapsort algorithm

BUILD-MAX-HEAP on A[1..n]


O(n) time.
Extract Max for n times

O( nlgn) time.
20
Heapsort
14
16
14
8
2
7
4
8
10
1
9
10
4
3
2
7
1
9
3
16
21
Heapsort
9
10
8
4
2
7
14
8
9
16
1
3
4
3
10
7
14
1
2
16
22
Heapsort
1
2
1
4
10
7
14
2
3
8
4
9
10
16
1
2
3
4
3
7
8
9
7
14
10
8
9
16
14
16
23
Heapsort
HEAPSORT(A)
1. BUILD-MAX-HEAP(A)
2. for i ← length[A] downto 2
3.
do exchange A[1] ↔ A[i]
4.
heap-size[A] ← heap-size[A] - 1
5.
MAX-HEAPIFY(A, 1)
24
Heapsort
The running time of Heapsort

O(nlgn)


BUILD-MAX-HEAP: O(n)
MAX-HEAPFY: O(lgn)
25
Priority queues
Priority Queue

A data structure for maintaining a set S of elements, each
with an associated value called a key.

A max-priority queue operations.




INSERT(S, x) inserts the element x into the set S.
MAXIMUM(S) returns the element of S with the largest key.
EXTRACT-MAX(S) removes and returns the element of S with the
largest key.
INCREASE-KEY(S, x, k) increases the value of element x's key to
the new value k,
26
Priority queues
MAXIMUM


Read the max value
O(1) time
EXTRACT-MAX


Remove the max value + MAX-HEAPIFY
O(lg n)
27
Priority queues
INCREASE-KEY
16
16
14
8
2
7
4
14
10
1
9
10
8
3
2
7
15
9
3
1
28
Priority queues
16
16
14
15
2
7
8
15
10
1
9
10
14
3
2
7
8
9
3
1
29
Priority queues
HEAP-INCREASE-KEY

O(lg n) time.
30
Priority queues
INSERT

O(lg n) time.
MAX-HEAP-INSERT(A, key)
1. heap-size[A] ← heap-size[A] + 1
2. A[heap-size[A]] ← −∞
3. HEAP-INCREASE-KEY(A, heap-size[A], key)
31