CBMS-2015-09
题目来源:Question 9 日期:2024-08-01 题目主题:CS-算法-堆和排序
Solution
Question 1: Verify if the array is a heap
To determine if the given array is a heap, we need to check if for every , the condition holds.
- :
- :
- :
- :
- :
- :
- :
Since is not greater than or equal to , the array is not a heap.
Question 2: Subheap Property
Statement: If is a heap, then is also a heap for any .
Proof: Let be a heap, meaning for all . Consider any . Since the heap property holds for all elements in , it will also hold for all elements in because the parent-child relationships in the heap are preserved up to index . Therefore, is also a heap.
Question 3: Fixing the Heap Property after Insertion
Suppose is a heap, but after inserting an element at position , the array is not a heap. The violation of the heap property could only occur if the newly inserted element at position is greater than its parent. To fix this, we perform the heapify-up operation.
Heapify-up Procedure:
- Let .
- While and :
- Swap and .
- Set .
Time Complexity: The worst-case time complexity of this procedure is , as we may need to traverse up the height of the heap, which is logarithmic in terms of the number of elements.
Question 4: Extract-Max and Heapify-down
Proof that is the maximum element in : By the definition of a max-heap, the root node (i.e., ) must be greater than or equal to all its children. By induction, it can be proven that is greater than or equal to all elements in the heap. Hence, is the maximum element.
Heapify-down Procedure: After replacing with , if is not a heap, we need to restore the heap property by performing the heapify-down operation.
- Let .
- While (i.e., while has at least one child):
- Let (left child).
- If and , set (right child is larger).
- If , break the loop.
- Swap and .
- Set .
Time Complexity: The worst-case time complexity of this procedure is , as we may need to traverse down the height of the heap.
Question 5: Build Heap from Unordered Array
To convert an unordered array into a heap, we can use the build-heap algorithm:
Build-Heap Algorithm:
- Start from the last non-leaf node, .
- Perform heapify-down on each node from to 1.
Time Complexity: The build-heap process has a time complexity of , as it performs a linear number of operations over the array.
Question 6: Heapsort Algorithm
Using the above procedures, we can describe the Heapsort algorithm:
- Build-Heap: Convert the array into a max-heap.
- Extract-Max repeatedly: Swap with , reduce the size of the heap by 1, and then perform heapify-down on .
- Repeat the above step until the heap size is reduced to 1.
Time Complexity: The time complexity of Heapsort is because:
- Building the heap takes time.
- Each extraction and heapify-down takes time, and there are such extractions.
知识点
解题技巧和信息
堆问题的核心是维护堆的性质,通过理解堆的定义(最大堆或最小堆)来选择合适的操作(如 heapify-up 或 heapify-down)。在建堆和堆排序时,关注堆的高度和操作的时间复杂度。堆操作通常具有 的时间复杂度,而建堆的复杂度为 。
重点词汇
- heap 堆
- heapify 堆化
- max-heap 最大堆
- build-heap 建堆
- heapify-down 下滤
- heapify-up 上滤
参考资料
- Introduction to Algorithms, 3rd Edition, Chapter 6: Heapsort
- Data Structures and Algorithm Analysis in C, Chapter 7: Heaps