• No results found

Algorithms and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Algorithms and Data Structures"

Copied!
61
0
0

Loading.... (view fulltext now)

Full text

(1)

Algorithms and Data Structures

9th Lecture: Heap

Yutaka Watanobe, Jie Huang, Yan Pei,

Wenxi Chen, Qiangfu Zhao, Wanming Chu

University of Aizu

(2)

Outline

Heap

Heap Properties

Operations on Heaps

Priority Queues

Heap Sort Algorithm

(3)

Heap (1)

The (binary) heap data structure is an array object that can be

viewed as a nearly complete binary tree.

(4)

Heap (2)

Each node of the tree corresponds to an element of the array

that stores the value in the node.

An array A that represents a heap is an object with two attributes:

A.length, which is the number of elements in the array, and

A.heap size, the number of elements in the heap stored within

array A.

Viewing a heap as a tree, we define the height of a node in a

heap to be the number of edges on the longest simple downward

path from the node to a leaf.

(5)

Heap: An Example

25

12

5

8

7

9

4

31

3

6

1

2

3

4

5

6

7

8

9

10

1

2

3

4

5

6

7

8

9

10

(6)

Operations on Heaps

The root of the tree is A[1], and given the index i of a node, the

indices of its parent parent(i), left child left(i), and right child right(i)

can be computed simply:

parent(i)

return floor(i/2)

left(i)

return 2i

right(i)

return 2i + 1

i/2

i

2i

2i+1

(7)

Heap Properties

There are two kinds of binary heaps:

max-heaps and

min-heaps.

(8)

Max-heap

In a max-heap, the max-heap property is that, for every node i

other than the root, A[i]

≤ A[parent(i)], that is, the value of a

node is at most the value of its parent.

The largest element in a max-heap is stored at the root, and the

subtree rooted at a node contains values no larger than that

contained at the node itself.

25

12

9

8

7

5

4

1

2

3

4

5

6

7

8

9

10

(9)

Min-heap

In a min-heap, the min-heap property is that, for every node i

other than the root, A[parent(i)]

≤ A[i].

The smallest element in a min-heap is at the root.

2

6

5

25

7

9

7

1

2

3

4

5

6

7

8

9

10

(10)

Maintaining the Heap Property

maxHeapify is an important subroutine for manipulating max-heap

maxHeapify(A, i)

l = left(i)

r = right(i)

if l <= A.heap_size and A[l] > A[i]

largest = l

else

largest = i

if r <= A.heap_size and A[r] > A[largest]

largest = r

if largest != i

exchange A[i] and A[largest]

maxHeapify(A, largest)

(11)

MaxHeapify: An Example

3. 2. 1. 5 86 37 12 25 32 11 2 19 1 4 7 86 5 37 12 25 32 11 2 19 1 4 7 86 25 37 12 5 32 11 2 19 1 4 7
(12)

MaxHeapify: Down Heap (1)

1.

5

86

37

12

25

32

11

2

19

1

4

7

(13)

MaxHeapify: Down Heap (2)

2.

86

5

37

12

25

32

11

2

19

1

4

7

(14)

MaxHeapify: Down Heap (3)

3.

86

25

37

12

5

32

11

2

19

1

4

7

(15)

Building a Heap (1)

We use the procedure maxHeapify in a bottom-up manner to

convert an array A[1..n], where n = A.length, into a max-heap.

The elements in the subarray A[floor (n/2) + 1..n] are all leaves

of the tree, and so each is a 1-element heap to begin with.

The procedure buildMaxHeap goes through the remaining nodes

of the tree and runs maxHeapify on each one.

(16)

Building a Heap (2)

buildMaxHeap(A)

A.heap_size = A.length

for i = floor(A.length/2) down to 1

maxHeapify(A, i)

The time required by maxHeapify is O(log n).

We can build a max-heap by the procedure buildMaxHeap in

time O(n).

Perform maxHeapify to n/2 sub-trees with height 1, n/4 sub-trees

with height 2, ..., 1 sub-tree with height log n, respectively.

We obtain n

×

log nk =1 k
(17)

Building a Heap: An Example

4. 2. 1. 4 1 3 2 16 9 10 7 8 14 4 1 3 2 16 9 10 7 8 14 4 1 10 14 16 9 3 7 8 2 4 16 10 14 7 9 3 1 8 2 16 14 10 8 7 9 3 1 4 2 4 1 3 14 16 9 10 7 8 2 5. 3. 6.
(18)

Building a Heap: maxHeapify on a Subtree (1)

1.

4

1

3

2

16

9

10

7

8

14

(19)

Building a Heap: maxHeapify on a Subtree (2)

2.

4

1

3

2

16

9

10

7

8

14

(20)

Building a Heap: maxHeapify on a Subtree (3)

4

1

3

14

16

9

10

7

8

2

3.

(21)

Building a Heap: maxHeapify on a Subtree (4)

4.

4

1

10

14

16

9

3

7

8

2

(22)

Building a Heap: maxHeapify on a Subtree (5)

4

16

10

14

7

9

3

1

8

2

5.

(23)

Building a Heap: maxHeapify on a Subtree (6)

16

14

10

8

7

9

3

1

4

2

6.

(24)

Priority Queues

A priority queue is a data structure for maintaining a set S of

elements, each with an associated value called a key.

A max-priority queue supports the following operations.

insert(S, x) inserts the element x into the set S.

maximum(S) returns the element of S with the largest key.

extractMax(S) removes and returns the element of S with the

largest key.

increaseKey (S, p, k ) increases the value of element p’s key to the

new value k , which is assumed to be the least as large as p’s

current key value.

(25)

HeapMaximum

The procedure heapMaximum implements the maximum operation in

O(1) time.

heapMaximum(A)

return A[1]

(26)

HeapExtractMax

The procedure heapExtractMax implements the extractMax operation

in O(log n).

heapExtractMax(A)

if A.heap_size < 1

output error "heap underflow"

max = A[1]

A[1] = A[A.heap_size]

A.heap_size--maxHeapify(A, 1)

return max

(27)

HeapExtractMax: An Example

4. 3. 2. 1. 86 37 32 12 14 25 11 2 1 3 7 3 37 32 12 14 25 11 2 1 3 7 3 37 32 12 14 25 11 37 14 32 12 3 25 11 86 maxHeapify heapExtractMax
(28)

HeapExtractMax (1)

1.

86

37

32

12

14

25

11

2

1

3

7

86

heapExtractMax

(29)

HeapExtractMax (2)

2.

3

37

32

12

14

25

11

2

1

3

7

(30)

HeapExtractMax (3)

3.

3

37

32

12

14

25

11

2

1

7

maxHeapify

(31)

HeapExtractMax (4)

4.

37

14

32

12

3

25

11

2

1

7

(32)

MaxHeapInsert

The procedure maxHeapInsert implements the insert operation in

O(log n) time.

maxHeapInsert(A, key)

A.heap_size = A.heap_size + 1

A[A.heap_size] = -INF

(33)

HeapIncreaseKey

The procedure heapIncreaseKey implements the increaseKey

operation in O(log n) time.

heapIncreaseKey(A, i, key)

if key < A[i]

output error "new key is smaller than current key"

A[i] = key

while i > 1 and A[parent(i)] < A[i]

exchange A[i] and A[parent(i)]

i = parent(i)

(34)

HeapIncreaseKey: An Example

4. 3. 2. 1. 86 14 37 12 5 32 11 2 1 25 7 86 14 37 12 5 32 11 2 1 25 7 86 14 37 12 25 32 11 2 1 5 7 86 25 37 12 14 32 11 2 1 5 7
(35)

HeapIncreaseKey (1)

1.

86

14

37

12

5

32

11

2

1

25

7

(36)

HeapIncreaseKey (2)

2.

86

14

37

12

5

32

11

2

1

25

7

(37)

HeapIncreaseKey (3)

3.

86

14

37

12

25

32

11

2

1

5

7

(38)

HeapIncreaseKey (4)

4.

86

25

37

12

14

32

11

2

1

5

7

(39)

Heap Sort (1)

Here is Heap Sort Algorithm working on the input array A[1..n], where

n = A.length.

heapsort(A)

buildMaxHeap(A)

for i = A.length down to 2

exchange A[1] and A[i]

(40)

Heap Sort (1)

1.

2

12

14

25

86

37

32

11

5

1

7

(41)

Heap Sort (2)

2.

2

12

14

25

86

37

32

11

5

1

7

(42)

Heap Sort (3)

3.

2

12

14

25

86

37

32

11

5

1

7

(43)

Heap Sort (4)

4.

2

12

14

25

86

37

32

11

5

1

7

(44)

Heap Sort (5)

5.

2

12

14

25

86

37

32

11

5

1

7

(45)

Heap Sort (6)

6.

32

12

14

25

86

37

11

2

5

1

7

(46)

Heap Sort (7)

7.

32

12

14

25

86

37

11

2

5

1

7

(47)

Heap Sort (8)

8.

32

12

14

25

86

37

11

2

5

1

7

(48)

Heap Sort (9)

9.

32

12

14

25

37

86

11

2

5

1

7

(49)

Heap Sort (10)

10.

32

12

14

25

37

86

11

2

5

1

7

(50)

Heap Sort (11)

11.

32

12

14

25

37

86

11

2

5

1

7

(51)

Heap Sort (12)

12.

32

12

14

25

37

86

11

2

1

5

7

(52)

Heap Sort (13)

13.

32

12

14

25

37

86

11

2

5

1

7

(53)

Heap Sort (14)

14.

32

12

14

25

37

86

11

2

5

1

7

(54)

Heap Sort (15)

15.

32

12

14

25

37

86

11

2

5

1

7

(55)

Heap Sort (16)

16.

32

12

14

25

37

86

11

1

5

7

2

(56)

Heap Sort (17)

17.

32

12

14

25

37

86

11

1

5

7

2

(57)

Heap Sort (18)

18.

32

12

14

25

37

86

11

5

1

7

2

(58)

Heap Sort (19)

19.

32

12

14

25

37

86

11

1

5

7

2

(59)

Heap Sort (20)

20.

32

12

14

25

37

86

11

1

5

7

2

(60)

Complexity of Heap Sort

The heapsort procedure takes time O(n log n), since the call to

buildMaxHeap takes time O(n) and each of the n

− 1 calls to

maxHeapify takes time O(log n).

(61)

Reference

1

Introduction to Algorithms (third edition), Thomas H.Cormen,

Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The

MIT Press, 2012.

References

Related documents