Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章 (Chapter 58) 伏 者潛藏也 道紀章 (Chapter 14) 道無形象, 視之不可見者曰 夷

2 Fuw-Yi Yang2 Reference: T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, 3rd ed. Chapter Heap

3 Fuw-Yi Yang3 Chapter 6 Heap—Parent, Left, Right Parent(i) 1. Return  i/2  Left(i) 1. Return 2i Right(i) 1. Return 2i + 1

4 Fuw-Yi Yang4 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)

5 Fuw-Yi Yang5 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)

6 Fuw-Yi Yang6 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)

7 Fuw-Yi Yang7 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

8 Fuw-Yi Yang8 Chapter 6 Heap— Complexity of Build-Max-Heap

9 Fuw-Yi Yang9 Chapter 6 Heap— Complexity of Build-Max-Heap Thus, = O(n)

10 Fuw-Yi Yang10 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)

11 Fuw-Yi Yang11 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.

12 Fuw-Yi Yang12 Chapter 6 Heap—Priority Queues Maximum Heap-Maximum (A) 1. Return A[1] Heap-Minimum (A) 1. Return A[1]

13 Fuw-Yi Yang13 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

14 Fuw-Yi Yang14 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

15 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)

16 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)

17 Fuw-Yi Yang17 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)

18 Fuw-Yi Yang18 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)


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

Similar presentations


Ads by Google