Skip to content

Commit a9cf350

Browse files
committed
✨ Solutions of 6.5
1 parent 2151606 commit a9cf350

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/C6/CLRS 6.4.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ https://github.com/MurphysL/Introduction-to-Algorithms/blob/master/src/C6/code/s
1616
**Answer:**
1717

1818
**初始化:**第一次循环之前,此时为最大堆,子数组为空。
19+
1920
**保持:**每次迭代,最大堆堆顶元素被交换,交换后调用 MAX-HEAPIFY( A, 1)保持最大堆性质。经 i 此迭代后,子数组为 A[i+1..n],为从小到大排序的数组。
21+
2022
**终止:**i = 1 时过程终止。
2123

2224

src/C6/CLRS 6.5.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# CLRS 6.5
2+
3+
## 6.5-3
4+
5+
**Write pseudocode for the procedures HEAP-MINIMUM, HEAP-EXTRACT-MIN, HEAP- DECREASE-KEY, and MIN-HEAP-INSERT that implement a min-priority queue with a min-heap.**
6+
7+
**Answer:**
8+
9+
https://github.com/MurphysL/Introduction-to-Algorithms/blob/master/src/C6/code/s6.5-3.kt
10+
11+
## 6.5-4
12+
13+
**Why do we bother setting the key of the inserted node to -∞ in line 2 of MAX-HEAP- INSERT when the next thing we do is increase its key to the desired value?**
14+
15+
**Answer:**
16+
17+
确保 key 的值小于 A[heap.size]
18+
19+
## 6.5-5
20+
21+
**Argue the correctness of HEAP-INCREASE-KEY using the following loop invariant:**
22+
> At the start of each iteration of the while loop of lines 4-6, the array A[1...heap- size[A]] satisfies the max-heap property, except that there may be one violation: A[i] may be larger than A[PARENT(i)].
23+
24+
**Answer:**
25+
26+
**初始化:**第一次循环迭代开始之前,除 A[heap.size] 外,其他元素均满足最大堆的性质。
27+
**保持:**循环过程中,不断交换 key 与 其父结点的值使其满足最大堆性质。
28+
**终止:**当 i = 1 时,所有元素已按最大堆的性质排好。
29+
30+
## 6.5-6
31+
32+
**Each exchange operation on line 5 of HEAP-INCREASE-KEY typically requires three asignments. Show how to use the idea of the inner loop of INSERTION-SORT to reduce the three assignments down to just one assignment.**
33+
34+
**Answer:**
35+
```
36+
HEAP-INCREASE-KEY(A, I, key):
37+
if key < A[i]
38+
error "new key is smaller than current key"
39+
A[i] = key
40+
while i > 1 and A[PARENT(i)] < key:
41+
A[i] = A[PARENT]
42+
i = PARENT(i)
43+
A[i] = key
44+
```
45+
46+
## 6.5-7
47+
48+
**Show how to implement a first-in, first-out queue with a priority queue. Show how to implement a stack with a priority queue. (Queues and stacks are defined in Section 10.1.)**
49+
50+
**Answer:**
51+
* **队列:**将新插入的值赋予更低的优先级
52+
* **栈:**将新插入的值赋予更高的优先级
53+
54+
## 6.5-8
55+
56+
**The operation HEAP-DELETE(A, i) deletes the item in node i from heap A. Give an implementation of HEAP-DELETE that runs in O(lg n) time for an n-element max-heap.**
57+
58+
**Answer:**
59+
60+
***A[A.heap-size] == A[i]** 时,时间复杂度为 O(1)
61+
***A[A.heap-size] > A[i]** 时,时间复杂度为 O(lgn)
62+
***A[A.heap-size] < A[i]** 时,时间复杂度为 O(lgn),例:
63+
64+
> 10
65+
> / \
66+
> 5 9
67+
> / \ / \
68+
> 2 3 7 8
69+
70+
https://github.com/MurphysL/Introduction-to-Algorithms/blob/master/src/C6/code/s6.5-8.kt
71+

0 commit comments

Comments
 (0)