diff --git a/C06-Heapsort/6.1.md b/C06-Heapsort/6.1.md new file mode 100644 index 00000000..9fde754e --- /dev/null +++ b/C06-Heapsort/6.1.md @@ -0,0 +1,56 @@ +### Exercises 6.1-1 +*** +What are the minimum and maximum numbers of elements in a heap of height h? + +### `Answer` +最多就是一颗很完美的二叉树,是2\*\*(h+1) − 1 ; 最少的话最后一层只有一个,是2\*\*h + + +### Exercises 6.1-2 +*** +Show that an n-element heap has height ⌞lg n⌟ + +### `Answer` +![](http://latex.codecogs.com/gif.latex? 2^{h+1}-1\\geq x \\geq 2^{h} \\rightrightarrows \\lg{x} \\geq h \\geq \\lg\(x+1\)-1 ) + +所以h = ⌞lg n⌟ + +### Exercises 6.1-3 +*** +Show that in any subtree of a max-heap, the root of the subtree contains the largest value occurring anywhere in that subtree. + +### `Answer` +这就是最大堆的性质. + +### Exercises 6.1-4 +*** +Where in a max-heap might the smallest element reside, assuming that all elements are distinct? + +### `Answer` +肯定是在叶子节点. + +### Exercises 6.1-5 +*** +Is an array that is in sorted order a min-heap? + +### `Answer` +没有说明是递增数组还是递减数组,所以不一定. + +### Exercises 6.1-6 +*** +Is the sequence [23, 17, 14, 6, 13, 10, 1, 5, 7, 12] a max-heap? + +### `Answer` +不是,7 > 6 + +### Exercises 6.1-7 +*** +Show that, with the array representation for storing an n-element heap, the leaves are the nodes indexed by ⌞n/2⌟ + 1, ⌞n/2⌟ + 2, ... , n. + +### `Answer` +挺容易推的,因为每增加两个节点,树的叶子节点就会往后推移一位.再注意一下取整的位置就行. + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C06-Heapsort/6.2.md b/C06-Heapsort/6.2.md new file mode 100644 index 00000000..f46f3ede --- /dev/null +++ b/C06-Heapsort/6.2.md @@ -0,0 +1,70 @@ +### Exercises 6.2-1 +*** +Using Figure 6.2 as a model, illustrate the operation of MAX-HEAPIFY(A, 3) on the array A = 27,17, 3, 16, 13, 10, 1, 5, 7, 12, 4, 8, 9, 0. + +### `Answer` +![](./repo/s2/1.png) + + +### Exercises 6.2-2 +*** +Starting with the procedure MAX-HEAPIFY, write pseudocode for the procedure MIN- HEAPIFY(A, i), which performs the corresponding manipulation on a min-heap. How does the running time of MIN-HEAPIFY compare to that of MAX-HEAPIFY? + +### `Answer` + + MIN-HEAPIFY(A, i): + l <- LEFT(i) + r <- RIGHT(i) + if l ≤ heap-size[A] and A[l] < A[i]: + then smallest <- l + else smallest <- i + if r ≤ heap-size[A] and A[r] < A[smallest]: + then smallest <- r + if smallest ≠ i: + then swap(A[i], A[smallest]) + MIN-HEAPIFY(A, smallest) + +### Exercises 6.2-3 +*** +What is the effect of calling MAX-HEAPIFY(A, i) when the element A[i] is larger than its children? + +### `Answer` +函数直接返回. + +### Exercises 6.2-4 +*** +What is the effect of calling MAX-HEAPIFY(A, i) for i > heap-size[A]/2? + +### `Answer` +这种情况下,这个节点是叶子节点. + +### Exercises 6.2-5 +*** +The code for MAX-HEAPIFY is quite efficient in terms of constant factors, except possibly for the recursive call in line 10, which might cause some compilers to produce inefficient code. Write an efficient MAX-HEAPIFY that uses an iterative control construct (a loop) instead of recursion. + +### `Answer` + + MIN-HEAPIFY(A, i): + while i ≤ heap-size[A]: + l <- LEFT(i) + r <- RIGHT(i) + if l ≤ heap-size[A] and A[l] > A[i]: + then largest <- l + else largest <- i + if r ≤ heap-size[A] and A[r] > A[largest]: + then largest <- r + if largest ≠ i: + then swap(A[i], A[largest]) + i = largest + +### Exercises 6.2-6 +*** +Show that the worst-case running time of MAX-HEAPIFY on a heap of size n is Ω(lg n). (Hint: For a heap with n nodes, give node values that cause MAX-HEAPIFY to be called recursively at every node on a path from the root down to a leaf.) + +### `Answer` +最坏情况是从root一直递归到leaf,因为heap的高度为⌞lg n⌟,所以最坏运行时间是Ω(lgn). + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C06-Heapsort/6.3.md b/C06-Heapsort/6.3.md new file mode 100644 index 00000000..98f9d40d --- /dev/null +++ b/C06-Heapsort/6.3.md @@ -0,0 +1,34 @@ +### Exercises 6.3-1 +*** +Using Figure 6.3 as a model, illustrate the operation of BUILD-MAX-HEAP on the array A = [5, 3, 17, 10, 84, 19, 6, 22, 9]. + +### `Answer` +![](./repo/s3/1.png) + + +### Exercises 6.3-2 +*** +Why do we want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from ⌞length[A]/2⌟ to 1 rather than increase from 1 to ⌞length[A]/2⌟? + +### `Answer` +如果先从1开始,它的子树并不是最大堆,肯定不能这样迭代. + +### Exercises 6.3-3 +*** +Show that there are at most ![](http://latex.codecogs.com/gif.latex? \\lceil n/\(2^{h+1}\) \\rceil) nodes of height h in any n-element heap. + +### `Answer` +根据[6.1.1](./6.1.md)我们有 + +![](http://latex.codecogs.com/gif.latex? 2^{h_0} \\le n \\le 2^{h_0+1}-1 ) + +令k为层数,h0为树的高度,root结点k = 0. + +![](http://latex.codecogs.com/gif.latex? \\lceil n/\(2^{h+1}\) \\rceil \\le \\lceil \(2^{h_0+1}-1\)/\(2^{h+1}\) \\rceil = 2^{h_0-h}) + +每层都满,等式成立;否则,叶子层是小于. + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C06-Heapsort/6.4.md b/C06-Heapsort/6.4.md new file mode 100644 index 00000000..769082d3 --- /dev/null +++ b/C06-Heapsort/6.4.md @@ -0,0 +1,45 @@ +### Exercises 6.4-1 +*** +Using Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A = [5, 13, 2, 25, 7, 17, 20, 8, 4]. + +### `Answer` +![](./repo/s4/1.png) + + +### Exercises 6.4-2 +*** +Argue the correctness of HEAPSORT using the following loop invariant: + • At the start of each iteration of the for loop of lines 2-5, the subarray A[1...i] is a max-heap containing the i smallest elements of A[1...n], and the subarray A[i + 1...n] contains the n - i largest elements of A[1...n], sorted. + +### `Answer` +这个循环不变式比较明显,就不详细写啦 + +### Exercises 6.4-3 +*** +What is the running time of heapsort on an array A of length n that is already sorted in increasing order? What about decreasing order? + +### `Answer` +如果是递减序列,是最差的情况.需要 + +![](http://latex.codecogs.com/gif.latex? \\sum_{i = 1}^{n}\\lg{i} = \\lg{n!} = \\Theta\(n\\lg{n}\) ) + +如果是按递增序列,也需要![](http://latex.codecogs.com/gif.latex?\\Theta\(n\\lg{n}\) ),因为Max_heapify的cost并没变. + + +### Exercises 6.4-4 +*** +Show that the worst-case running time of heapsort is Ω(n lg n). + +### `Answer` +跟6.3.3一样 + +### Exercises 6.4-5 +*** +Show that when all elements are distinct, the best-case running time of heapsort is Ω(n lg n). + +### `Answer` +It is actually a hard problem, see [solution](http://stackoverflow.com/questions/4589988/lower-bound-on-heapsort) + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C06-Heapsort/6.5.md b/C06-Heapsort/6.5.md new file mode 100644 index 00000000..72aee030 --- /dev/null +++ b/C06-Heapsort/6.5.md @@ -0,0 +1,76 @@ +### Exercises 6.5-1 +*** +Illustrate the operation of HEAP-EXTRACT-MAX on the heap A = [15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1]. + +### `Answer` +![](./repo/s5/1.png) + + +### Exercises 6.5-2 +*** +Illustrate the operation of MAX-HEAP-INSERT(A, 10) on the heap A = [15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1]. Use the heap of Figure 6.5 as a model for the HEAP-INCREASE-KEY call. + +### `Answer` +![](./repo/s5/2.png) + +### Exercises 6.5-3 +*** +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. + +### `Answer` +这是最大优先级队列的实现,最小也类似. + +[p_queue.h](./p_queue.h) + +[p_queue.cpp](./p_queue.cpp) + + +### Exercises 6.5-4 +*** +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? + +### `Answer` +为了使HEAP-INCREASE-KEY的条件成立 + +### Exercises 6.5-5 +*** +Argue the correctness of HEAP-INCREASE-KEY using the following loop invariant: + • 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)]. + +### `Answer` +依然是很明显的循环不变式. + +### Exercises 6.5-6 +*** +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.) + +### `Answer` + +* 先进先出队列: 每次都给新插入的元素赋予更低的优先级即可. +* 栈:每次都给新插入的元素赋予更高的优先级. + + +### Exercises 6.5-7 +*** +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. + +### `Answer` + + HEAP-DELETE(A, i): + A[i] = A[A.heap-size] + A.heap-size -= 1 + MAX-HEAPIFY(A, i) + +### Exercises 6.5-8 +*** +Give an O(n lg k)-time algorithm to merge k sorted lists into one sorted list, where n is the total number of elements in all the input lists. (Hint: Use a min-heap for k-way merging.) + +### `Answer` +The problem occurs in [leetcode](https://leetcode.com/problems/merge-k-sorted-lists/) + +This is my [solution](https://github.com/gzc/leetcode/blob/master/cpp/021-030/Merge%20k%20Sorted%20Lists%20.cpp) + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C06-Heapsort/d-ary-heaps.cpp b/C06-Heapsort/d-ary-heaps.cpp new file mode 100644 index 00000000..54745346 --- /dev/null +++ b/C06-Heapsort/d-ary-heaps.cpp @@ -0,0 +1,64 @@ +/************************************************************************* + > File Name: d-ary-heaps.cpp + > Author: Louis1992 + > Mail: zhenchaogan@gmail.com + > Blog: http://gzc.github.io + > Created Time: Tue Jun 23 19:22:21 2015 + ************************************************************************/ +#include +#include +#include +using namespace std; + +#define PARENT(i,d) ((i - 1) / d) +#define CHILD(i,c,d) (3 * i + c + 1) + +typedef struct { + int *elements; + int d; + int heap_size; +} heap_t; + +void max_heapify(heap_t *heap, int i) { + int largest = i; + int basechild = CHILD(i, 0, heap->d); + + for (int k = 0; k < heap->d; k++) { + int child = basechild+k; + if (child < heap->heap_size && heap->elements[child] > heap->elements[largest]) + largest = child; + } + + if (largest != i) { + swap(heap->elements[i],heap->elements[largest]); + max_heapify(heap, largest); + } +} + +int extract_max(heap_t *heap) { + int max = heap->elements[0]; + heap->elements[0] = heap->elements[heap->heap_size - 1]; + heap->heap_size--; + max_heapify(heap, 0); + return max; +}; + +void increase_key(heap_t *heap, int i, int key) { + if (key < heap->elements[i]) { + cerr << "new key is smaller than current key" << endl; + exit(-1); + } + + while (i > 0 && heap->elements[PARENT(i,heap->d)] < key) { + heap->elements[i] = heap->elements[PARENT(i,heap->d)]; + i = PARENT(i,heap->d); + } + + heap->elements[i] = key; +} + +void insert(heap_t *heap, int key) { + heap->heap_size++; + heap->elements[heap->heap_size - 1] = INT_MIN; + increase_key(heap, heap->heap_size - 1, key); +} \ No newline at end of file diff --git a/Chapter6:Heapsort/heap.cpp b/C06-Heapsort/heap.cpp similarity index 100% rename from Chapter6:Heapsort/heap.cpp rename to C06-Heapsort/heap.cpp diff --git a/Chapter6:Heapsort/main.cpp b/C06-Heapsort/main.cpp similarity index 100% rename from Chapter6:Heapsort/main.cpp rename to C06-Heapsort/main.cpp diff --git a/Chapter6:Heapsort/makefile b/C06-Heapsort/makefile similarity index 100% rename from Chapter6:Heapsort/makefile rename to C06-Heapsort/makefile diff --git a/Chapter6:Heapsort/p_queue.cpp b/C06-Heapsort/p_queue.cpp similarity index 100% rename from Chapter6:Heapsort/p_queue.cpp rename to C06-Heapsort/p_queue.cpp diff --git a/Chapter6:Heapsort/p_queue.h b/C06-Heapsort/p_queue.h similarity index 100% rename from Chapter6:Heapsort/p_queue.h rename to C06-Heapsort/p_queue.h diff --git a/C06-Heapsort/problem.md b/C06-Heapsort/problem.md new file mode 100644 index 00000000..eae8471c --- /dev/null +++ b/C06-Heapsort/problem.md @@ -0,0 +1,66 @@ +### Problems 1 : Building a heap using insertion +*** +The procedure BUILD-MAX-HEAP in Section 6.3 can be implemented by repeatedly using MAX-HEAP-INSERT to insert the elements into the heap. Consider the following implementation: + + BUILD-MAX-HEAP'(A) heap-size[A] ← 1 for i←2 to length[A] do MAX-HEAP-INSERT(A, A[i]) + +a. Do the procedures BUILD-MAX-HEAP and BUILD-MAX-HEAP' always create the same heap when run on the same input array? Prove that they do, or provide a counterexample.  +b. Show that in the worst case, BUILD-MAX-HEAP' requires Θ(n lg n) time to build an n-element heap. + +### `Answer` +**a.** +不一定.对于数组[1,2,3,4,5,6].有 + +![](./repo/p/1.png) + + +**b.** +当原数组是递增序列时,需要的时间 + +![](http://latex.codecogs.com/gif.latex? T = \\sum_{i = 2}^{n}\\lg{i} = \\lg{n!} = \\Theta\(n\\lg{n}\) ) + + + +### Problems 2 : Analysis of d-ary heaps +*** +A d-ary heap is like a binary heap, but (with one possible exception) non-leaf nodes have d children instead of 2 children. + a. How would you represent a d-ary heap in an array? b. What is the height of a d-ary heap of n elements in terms of n and d? c. Give an efficient implementation of EXTRACT-MAX in a d-ary max-heap. Analyze its running time in terms of d and n. d. Give an efficient implementation of INSERT in a d-ary max-heap. Analyze its running time in terms of d and n. e. Give an efficient implementation of INCREASE-KEY(A, i, k), which first sets A[i] ← max(A[i], k) and then updates the d-ary max-heap structure appropriately. Analyze its running time in terms of d and n. +### `Answer` +[implementation](./d-ary-heaps.cpp) + +![](http://latex.codecogs.com/gif.latex? \\quad\\text{Height :} \\log_{d}{n} ) + +![](http://latex.codecogs.com/gif.latex? \\quad\\text{\\textbf{Complexity}} \\\\ +\\quad\\text{ EXTRACT-MAX : } d\\log_{d}{n} \\\\ +\\quad\\text{ INSERT : } \\log_{d}{n} \\\\ +\\quad\\text{ INCREASE-KEY : } \\log_{d}{n} ) + + +### Problems 3 : Young tableaus +*** +An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom. Some of the entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus, a Young tableau can be used to hold r ≤ mn finite numbers. + a. Draw a 4×4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}. b. Arguethatanm×nYoungtableauYisemptyifY[1,1]=∞.ArguethatYisfull (contains mn elements) if Y[m, n] < ∞. c. Give an algorithm to implement EXTRACT-MIN on a nonempty m × n Young tableau that runs in O(m + n) time. Your algorithm should use a recursive subroutine that solves an m × n problem by recursively solving either an (m - 1) × n or an m × (n - 1) subproblem. (Hint: Think about MAX-HEAPIFY.) Define T(p), where p = m + n, to be the maximum running time of EXTRACT-MIN on any m × n Young tableau. Give and solve a recurrence for T(p) that yields the O(m + n) time bound. d. Show how to insert a new element into a nonfull m × n Young tableau in O(m + n) time. e. Using no other sorting method as a subroutine, show how to use an n × n Young tableau to sort n^2 numbers in O(n^3) time. f. Give an O(m+n)-time algorithm to determine whether a given number is stored in a given m × n Young tableau. + ### `Answer` + [implementation](./young.cpp) + **a.** + +![](http://latex.codecogs.com/gif.latex? \\begin{matrix} 2 & 3 & 12 & \\infty \\\\ 4 & 8 & 16 & \\infty \\\\ 5 & 9 & \\infty & \\infty \\\\ 14 & \\infty & \\infty & \\infty \\\\ \\end{matrix} ) + **b.** + 这是显然的~ + **c.** +T(p) = T(p-1) + O(1) = O(p) + +**d.** +跟c是一个相反的操作,具体见代码. + +**e.** +![](http://latex.codecogs.com/gif.latex? +T = n^2O\(p\) = O\(n^3\)) + +**f.** +实现见代码. + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C06-Heapsort/repo/p/1.png b/C06-Heapsort/repo/p/1.png new file mode 100644 index 00000000..63b8d040 Binary files /dev/null and b/C06-Heapsort/repo/p/1.png differ diff --git a/C06-Heapsort/repo/s2/1.png b/C06-Heapsort/repo/s2/1.png new file mode 100644 index 00000000..1fccfde5 Binary files /dev/null and b/C06-Heapsort/repo/s2/1.png differ diff --git a/C06-Heapsort/repo/s3/1.png b/C06-Heapsort/repo/s3/1.png new file mode 100644 index 00000000..a390dbcf Binary files /dev/null and b/C06-Heapsort/repo/s3/1.png differ diff --git a/C06-Heapsort/repo/s4/1.png b/C06-Heapsort/repo/s4/1.png new file mode 100644 index 00000000..44e1edb0 Binary files /dev/null and b/C06-Heapsort/repo/s4/1.png differ diff --git a/C06-Heapsort/repo/s5/1.png b/C06-Heapsort/repo/s5/1.png new file mode 100644 index 00000000..5fe94445 Binary files /dev/null and b/C06-Heapsort/repo/s5/1.png differ diff --git a/C06-Heapsort/repo/s5/2.png b/C06-Heapsort/repo/s5/2.png new file mode 100644 index 00000000..bde69315 Binary files /dev/null and b/C06-Heapsort/repo/s5/2.png differ diff --git a/C06-Heapsort/young.cpp b/C06-Heapsort/young.cpp new file mode 100644 index 00000000..a2d5f1ec --- /dev/null +++ b/C06-Heapsort/young.cpp @@ -0,0 +1,129 @@ +/************************************************************************* + > File Name: young.cpp + > Author: Louis1992 + > Mail: zhenchaogan@gmail.com + > Blog: http://gzc.github.io + > Created Time: Tue Jun 23 20:05:44 2015 + ************************************************************************/ +#include +#include +#include +using namespace std; + +int EXTRACT_MIN(vector > &young) +{ + int minimum = young[0][0]; + int i(0),j(0); + + while(i < young.size() && j < young[0].size()) + { + int cur = young[i][j]; + int ori(i),orj(j); + int right(INT_MAX),down(INT_MAX); + + if(i < young.size()-1) down = young[i+1][j]; + if(j < young[0].size()-1) right = young[i][j+1]; + + if(right == INT_MAX && down == INT_MAX) + { + young[i][j] = INT_MAX; + break; + } + else if(down <= right) + i++; + else + j++; + + swap(young[i][j], young[ori][orj]); + } + + return minimum; +} + +void INSERT(vector > &young, int v) +{ + int i = young.size()-1; + int j = young[0].size()-1; + young[i][j] = v; + + while(i >= 0 && j >= 0) + { + int left(INT_MIN),up(INT_MIN); + + if(i > 0) up = young[i-1][j]; + if(j > 0) left = young[i][j-1]; + + if(v >= up && v >= left) + break; + else if(v < up) + { + swap(young[i][j], young[i-1][j]); + i--; + } else { + swap(young[i][j], young[i][j-1]); + j--; + } + } +} + +bool EXIST(vector > &young, int v) +{ + int i(young.size()-1),j(0); + + while(i >= 0 && j < young[0].size()) + { + cout << i << " " << j << endl; + if(young[i][j] == v) + return true; + + if(young[i][j] < v) + j++; + else + i--; + } + + return false; +} + +void print_young(vector > &young) +{ + for(int i = 0;i < young.size();i++) + { + for(int j = 0;j < young[0].size();j++) + { + cout << young[i][j] << " "; + } + cout << endl; + } +} + + +int main() { + + int arr1[4] = {2,3,12,INT_MAX}; + int arr2[4] = {4,8,16,INT_MAX}; + int arr3[4] = {5,9,INT_MAX,INT_MAX}; + int arr4[4] = {14,INT_MAX,INT_MAX,INT_MAX}; + + vector v1(arr1,arr1+4); + vector v2(arr2,arr2+4); + vector v3(arr3,arr3+4); + vector v4(arr4,arr4+4); + + vector > v; + v.push_back(v1); + v.push_back(v2); + v.push_back(v3); + v.push_back(v4); + + for(int i = 0;i < 9;i++) + cout << EXTRACT_MIN(v) << endl; + for(int i = 0;i < 9;i++) + INSERT(v, i); + print_young(v); + cout << EXIST(v, 8) << endl; + + + + return 0; +} diff --git a/Chapter6:Heapsort/6.1/6.1.pdf b/Chapter6:Heapsort/6.1/6.1.pdf deleted file mode 100644 index 9781c22e..00000000 Binary files a/Chapter6:Heapsort/6.1/6.1.pdf and /dev/null differ diff --git a/Chapter6:Heapsort/6.1/6.1.tex b/Chapter6:Heapsort/6.1/6.1.tex deleted file mode 100644 index 2419cd42..00000000 --- a/Chapter6:Heapsort/6.1/6.1.tex +++ /dev/null @@ -1,97 +0,0 @@ -% XeLaTeX can use any Mac OS X font. See the setromanfont command below. -% Input to XeLaTeX is full Unicode, so Unicode characters can be typed directly into the source. - -% The next lines tell TeXShop to typeset with xelatex, and to open and save the source with Unicode encoding. - -%!TEX TS-program = xelatex -%!TEX encoding = UTF-8 Unicode - -\documentclass[12pt]{article} -\usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots. -\geometry{letterpaper} % ... or a4paper or a5paper or ... -%\geometry{landscape} % Activate for for rotated page geometry -%\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent -\usepackage{graphicx} -\usepackage{amssymb} -\usepackage[colorlinks,linkcolor=black,anchorcolor=blue,citecolor=green]{hyperref} - -% Will Robertson's fontspec.sty can be used to simplify font choices. -% To experiment, open /Applications/Font Book to examine the fonts provided on Mac OS X, -% and change "Hoefler Text" to any of these choices. - -\usepackage{fontspec,xltxtra,xunicode} -\defaultfontfeatures{Mapping=tex-text} -\setromanfont[Mapping=tex-text]{Hoefler Text} -\setsansfont[Scale=MatchLowercase,Mapping=tex-text]{Gill Sans} -\setmonofont[Scale=MatchLowercase]{Andale Mono} - -\title{算法导论习题6.1} -\author{Louis1992 \\ 有需要可以\href{mailto:zhenchaogan@hotmail.com}{联系我} -\\ 我的github\href{https://github.com/gzc}{欢迎大家帮我完成算法导论} \\ \href{https://gzc.github.io}{我的博客}} -%\date{} % Activate to display a given date or no date - -\usepackage[slantfont,boldfont]{xeCJK} -\usepackage{xcolor} -\setCJKmainfont{SimSun} -\setCJKfamilyfont{song}{SimSun} - - -\begin{document} -\maketitle - -% For many users, the previous commands will be enough. -% If you want to directly input Unicode, add an Input Menu or Keyboard to the menu bar -% using the International Panel in System Preferences. -% Unicode must be typeset using a font containing the appropriate characters. -% Remove the comment signs below for examples. - -% \newfontfamily{\A}{Geeza Pro}\sqrt[n]{} -% \newfontfamily{\H}[Scale=0.9]{Lucida Grande} -% \newfontfamily{\J}[Scale=0.85]{Osaka} - -% Here are some multilingual Unicode fonts: this is Arabic text: {\A السلام عليكم}, this is Hebrew: {\H שלום}, -% and here's some Japanese: {\J 今日は}. - -\noindent Exercises 6.1-1 What are the minimum and maximum numbers of elements in a heap of height h? \\ -最多就是一颗很完美的二叉树,是 $2^{h+1}-1$ ; -最少的话最后一层只有一个,是 $2^{h}$ -\\ -\\ - -\noindent Exercises 6.1-2 -Show that an n-element heap has height $\llcorner\lg{n}\lrcorner$ \\ -直接利用第一题的结论:$2^{h+1}-1\geq x \geq 2^{h} \rightrightarrows \lg{x} \geq h \geq \lg{(x+1)}-1 $ \\ -所以 h = $\llcorner\lg{n}\lrcorner$ -\\ -\\ - -\noindent Exercises 6.1-3 -Show that in any subtree of a max-heap, the root of the subtree contains the largest value occurring anywhere in that subtree. \\ -这就是最大堆的性质! -\\ -\\ - -\noindent Exercises 6.1-4 -Where in a max-heap might the smallest element reside, assuming that all elements are distinct? \\ -肯定是在叶子节点 -\\ -\\ - -\noindent Exercises 6.1-5 -Is an array that is in sorted order a min-heap? \\ -没有说明是递增数组还是递减数组,所以不一定 -\\ -\\ - -\noindent Exercises 6.1-6 -Is the sequence [23, 17, 14, 6, 13, 10, 1, 5, 7, 12] a max-heap? \\ -不是,7 > 6 -\\ -\\ - -\noindent Exercises 6.1-7 -Show that, with the array representation for storing an n-element heap, the leaves are the nodes indexed by $\llcorner{n/2}\lrcorner$ + 1, $\llcorner{n/2}\lrcorner$ + 2, ... , n. \\ -也是很简单的性质 - - -\end{document} \ No newline at end of file diff --git a/README.md b/README.md index 9adb6875..231ee2cd 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,17 @@ 4 p + + + + VI + 1 + 2 + 3 + 4 + 4 + p +