fuw-yi yang1 演算法概論 introduction to algorithms department of computer science and...

18
Fuw-Yi Yang 1 演演演演演 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 演演演演演演演演演 Speaker: Fuw-Yi Yang 演演演 演演演演演 , 演演演 演演演 (Chapter 58) 演演演演 演演演 (Chapter 14) 演演演演 , 演演演 演演演演

Upload: godwin-griffin

Post on 17-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 1

演算法概論 Introduction to Algorithms 

Department of Computer Science and Information Engineering, Chaoyang University of Technology

朝陽科技大學資工系

Speaker: Fuw-Yi Yang 楊伏夷

伏夷非征番 , 道德經 察政章 (Chapter 58) 伏者潛藏也 道紀章 (Chapter 14) 道無形象 , 視之不可見者曰夷

Page 2: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 2

Reference: T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, 3rd ed.

Chapter Heap

Page 3: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 3

Chapter 6 Heap—Parent, Left, Right

Parent(i)

1. Return i/2

Left(i)

1. Return 2i

Right(i)

1. Return 2i + 1

Page 4: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 4

Chapter 6 Heap—Max-Heapify

Max-Heapify(A, i)

1. l = Left(i)

2. r = Right(i)

3. if l A.heap-size and A[l] > A[i] then largest = l

4. else largest = i

5. if r A.heap-size and A[r] > A[largest] then largest = r

6. if largest i then Exchange A[i] with A[largest]

7. Max-Heapify (A, largest)

Page 5: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 5

Chapter 6 Heap—Max-Heapify

Min-Heapify(A, i)

1. l = Left(i)

2. r = Right(i)

3. if l A.heap-size and A[l] < A[i] then smallest = l

4. else smallest = i

5. if r A.heap-size and A[r] < A[smallest] then smallest = r

6. if smallest i then Exchange A[i] with A[smallest]

7. Min-Heapify (A, smallest)

Page 6: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 6

Chapter 6 Heap—Build-Max-Heap

Build-Max-Heap(A)

1. A.heap-size = A.length

2. for i = A.length / 2 downto 1

3. Max-Heapify(A, i)

Build-Min-Heap(A)

1. A.heap-size = A.length

2. for i = A.length / 2 downto 1

3. Min-Heapify(A, i)

Page 7: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 7

Chapter 6 Heap — Complexity of Build-Max-Heap

1. Each call to Max-Heapify costs O(log n) time, there are O(n)

such calls. Thus the complexity is O(n log n).

2. Tighter analysis results in the complexity of O(n).

The time required by Max-Heapify when called on a node of height h is O(h), and so we can express the total cost of Build-Max-Heap as being bounded by

Next page

)(lg

0

)(lg

01

)2

()()2

(nfloor

hh

nfloor

hh

hnOhO

nceiling

Page 8: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 8

Chapter 6 Heap— Complexity of Build-Max-Heap

)(lg

0

)(lg

01

)2

()()2

(nfloor

hh

nfloor

hh

hnOhO

nceiling

00

20

0

22

2/1;)1(

,1||;1

1

kk

k

k

k

k

k

k

kkx

xifx

xkx

sidesbothatingdifferentixforx

x

Page 9: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 9

Chapter 6 Heap— Complexity of Build-Max-Heap

Thus,

= O(n)

)(lg

0

)(lg

01

)2

()()2

(nfloor

hh

nfloor

hh

hnOhO

nceiling

Page 10: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 10

Chapter 6 Heap—HeapSort

HeapSort(A) //O(n log n)

1. Build-Max-Heap(A) //O(n)

2. for i = A.length downto 2 //O(n)

3. Exchange A[1] with A[i] //O(1)

4. A.heap-size = A.heap-size – 1 //O(1)

5. Max-Heapify(A, 1) //O(log n)

Page 11: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 11

Chapter 6 Heap—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,

Extract-Max(S) removes and returns the element of S with the

largest key,

Increase-Key(S, x, k) increase the value of element x’s key to the

new value k, which is assumed to be at least as large as x’s

current key value.

Page 12: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 12

Chapter 6 Heap—Priority Queues

Maximum

Heap-Maximum (A)

1. Return A[1]

Heap-Minimum (A)

1. Return A[1]

Page 13: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 13

Chapter 6 Heap—Priority Queues

Extract-Max

Heap-Extract-Max (A)

1. If A.heap-size < 1

2. Error “heap empty”

3. Max = A[1]

4. A[1] = A[A.heap-size]

5. A.heap-size = A.heap-size - 1

6. Max-Heapify(A, 1)

7. Return Max

Page 14: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 14

Chapter 6 Heap—Priority Queues

Extract-Min

Heap-Extract-Min (A)

1. If A.heap-size < 1

2. Error “heap empty”

3. Min = A[1]

4. A[1] = A[A.heap-size]

5. A.heap-size = A.heap-size - 1

6. Min-Heapify(A, 1)

7. Return Max

Page 15: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

15

Chapter 6 Heap—Priority Queues

Increase key

Heap-Increase-Key (A, i, key)

1. If key < A[i]

2. Error “new key is smaller than current key”

3. A[i] = key

4. While i > 1 and A[Parent(i)] < A[i]

5. Exchange A[i] with A[Parent(i)]

6. i = Parent(i)

Page 16: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

16

Chapter 6 Heap—Priority Queues

Increase key

Heap-Decrease-Key (A, i, key)

1. If key > A[i]

2. Error “new key is larger than current key”

3. A[i] = key

4. While i > 1 and A[Parent(i)] > A[i]

5. Exchange A[i] with A[Parent(i)]

6. i = Parent(i)

Page 17: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 17

Chapter 6 Heap—Priority Queues

Insert

Max-Heap-Insert (A, key)

1. A.heap-size = A.heap-size + 1

2. A[A.heap-size] = -∞

4. Heap-Increase-Key(A, A.heap-size, key)

Page 18: Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:

Fuw-Yi Yang 18

Chapter 6 Heap—Priority Queues

Insert

Min-Heap-Insert (A, key)

1. A.heap-size = A.heap-size + 1

2. A[A.heap-size] = ∞

4. Heap-Decrease-Key(A, A.heap-size, key)