算法导论第6章
2011-08-03 22:36
106 查看
It was the best of times, it was the worst of times.
This is my third time reading CLRS, and I never finished it. I hope I can put an end on it this time. I think this project will last one year to cover the whole contents of CLRS 3rd. Although I am reading the second version, for some parts, I read counterparts of the third one instead. Meanwhile, I will read and do all of exercises of SICP too. Answers are checked with Eli’s well-known who do all of exercises in Common Lisp and reveived a signed SICP from its author. Awesome.
So, I will do every exercise too, and validate them with solutions given by authors and answers from other blogs or StackOverflow (cool). In addition, thanks to four authors’ notes and soultions. The following notes are not original. I just add some my own understanding.
I believe a good theory could be represented by common sense. So I try to elaborate complicate algorithms and theories into very simple words. 比如:在学习统计学习一课时,为了解释SVM切平面为何选择为两类点的最中间时,老师举了一个impressive的例子,以至于我现在都还记得。走在一条乡间的没有中间线双向车道的路上时,一个人为了避免被两个方向的来车撞到,必然要选择最最中间的路走。一句话省去了SVM误差函数导出的复杂结论。
本章由heap –> heap operations –> priority queue
用数组表示堆 root —— A[1] 父亲parent节点 —— A[\floor{i/2}] 左孩子left child —— A[i*2] 右孩子right child —— A[i*2 + 1] 用移位快速计算下标
Example Fig. 6.1
由传递性可得最大堆的最大元素就是root
heap不一定是binary tree,可以是d-ary tree,见Problem
BUILD-MAX-HEAP HEAP —— O(n),建堆
HEAPSORT —— O(nlgn),sorting in place
MAX-HEAP-INSERT, HEAP-EXTRACT-MAX, HEAP-INCREASE-KEY, HEAP-MAXIMUM —— O(lgn)
之后:A[i]也是一个max-heap
[摈弃伪代码]
MAX-HEAPIFY(A, i, n)
直接的想法是,heap是接近的完全二叉树,算法要处理O(lgn)多的层数,每层一个常数级别,所以有O(lgn)。
上界:worst-case中,MAX-HEAPIFY的调用深度和节点A[i]的高度h线性正比,而且1/2的节点只有高度1。花费时间越多的节点越少,时间越少的节点越多,就有可能不是O(nlgn)
求和:\Sigma_h #高度h的节点(<=\ceiling{n/2^{h+1}})*O(h) = O(n)
HEAP-EXTRACT-MAX(A, n)
This is my third time reading CLRS, and I never finished it. I hope I can put an end on it this time. I think this project will last one year to cover the whole contents of CLRS 3rd. Although I am reading the second version, for some parts, I read counterparts of the third one instead. Meanwhile, I will read and do all of exercises of SICP too. Answers are checked with Eli’s well-known who do all of exercises in Common Lisp and reveived a signed SICP from its author. Awesome.
So, I will do every exercise too, and validate them with solutions given by authors and answers from other blogs or StackOverflow (cool). In addition, thanks to four authors’ notes and soultions. The following notes are not original. I just add some my own understanding.
I believe a good theory could be represented by common sense. So I try to elaborate complicate algorithms and theories into very simple words. 比如:在学习统计学习一课时,为了解释SVM切平面为何选择为两类点的最中间时,老师举了一个impressive的例子,以至于我现在都还记得。走在一条乡间的没有中间线双向车道的路上时,一个人为了避免被两个方向的来车撞到,必然要选择最最中间的路走。一句话省去了SVM误差函数导出的复杂结论。
6. Heapsort 堆排序
O(nlgn) worst case —— merge sort sorts in place —— insertion sort本章由heap –> heap operations –> priority queue
6.1 Heaps
堆是一棵近似的完全二叉树nearly completely binary tree 数组的长度length[A]。堆的长度heap-size[A] 节点的高度height由leaf到node。leaf的高度为0 堆的高度=\Theta(nlgn)用数组表示堆 root —— A[1] 父亲parent节点 —— A[\floor{i/2}] 左孩子left child —— A[i*2] 右孩子right child —— A[i*2 + 1] 用移位快速计算下标
Example Fig. 6.1
6.2 Heap priority
max-heaps: A[PARENT(i)] >= A[i] min-heaps: A[PARENT(i)] <= A[i]由传递性可得最大堆的最大元素就是root
heap不一定是binary tree,可以是d-ary tree,见Problem
6.3 Maintaining the heap priority
MAX-HEAPIFY —— O(lgn),让A[i]成为一个最大堆BUILD-MAX-HEAP HEAP —— O(n),建堆
HEAPSORT —— O(nlgn),sorting in place
MAX-HEAP-INSERT, HEAP-EXTRACT-MAX, HEAP-INCREASE-KEY, HEAP-MAXIMUM —— O(lgn)
MAX-HEAPIFY
之前:left child和right child是max-heap,但是A[i]小于children之后:A[i]也是一个max-heap
[摈弃伪代码]
MAX-HEAPIFY(A, i, n)
从A[i]和他的两个children中找出最大的largest,避免children下标越界
如果largest不是i,交换A[i]和largest,对A[largest]递归调用MAX-HEAPIFY。如此比较,交换下去一直到访问到一个leaf。
P131证明的时候用到了master theorem。worst case是leaf一层half full。这样#最下层的leaf=#右子树node=#左子数node-最下层leaf。如果largest不是i,交换A[i]和largest,对A[largest]递归调用MAX-HEAPIFY。如此比较,交换下去一直到访问到一个leaf。
直接的想法是,heap是接近的完全二叉树,算法要处理O(lgn)多的层数,每层一个常数级别,所以有O(lgn)。
BUILD-MAX-HEAP
BUILD-MAX-HEAP(A, n) 从\floor(n/2)到1调用MAX-HEAPIFY
Loop Invariant:从i到n,都是max-heap,证明从略上界:worst-case中,MAX-HEAPIFY的调用深度和节点A[i]的高度h线性正比,而且1/2的节点只有高度1。花费时间越多的节点越少,时间越少的节点越多,就有可能不是O(nlgn)
求和:\Sigma_h #高度h的节点(<=\ceiling{n/2^{h+1}})*O(h) = O(n)
HEAPSORT
HEAPSORT(A, n) 从Array建max-heap
重复下面n-1次
把堆最大元素和数组最后的元素交换
heap-size减一,对新root来MAX-HEAPIFY
共O(nlgn)重复下面n-1次
把堆最大元素和数组最后的元素交换
heap-size减一,对新root来MAX-HEAPIFY
Priority queue
通过key维护数据集合S,每一个数据有一个key Max-priority operations: INSERT(S, x), MAXIMUM(S), EXTRACT-MAX(S), INCREASE-KEY(S, x, k)HEAP-MAXIMUM
HEAP-MAXIMUM(A) return A[1]
O(1)
O(1)
HEAP-EXTRACT-MAX
抽出root,拷贝数组最后一个元素到root,MAX-HEAPIFYHEAP-EXTRACT-MAX(A, n)
HEAP非空,n<1否?
拷贝A[1]到maximum
数组A
拷贝到A[1],对A[1]来MAX-HEAPIFY(A, 1, n-1)
返回maximum
O(lgn)拷贝A[1]到maximum
数组A
拷贝到A[1],对A[1]来MAX-HEAPIFY(A, 1, n-1)
返回maximum
HEAP-INCREASE-KEY
HEAP-INCREASE-KEY(A, i, key) key大于A[i]否?
A[i]=key
while i > 1, i的PARENT小于i,交换PARENT和i,i=PARENT(i)
O(lgn)A[i]=key
while i > 1, i的PARENT小于i,交换PARENT和i,i=PARENT(i)
MAX-HEAP-INSERT
MAX-HEAP-INSERT(A, key, n) A[n+1]=-infinity
HEAP-INCREASE-KEY(A, n+1, key)
O(lgn)
HEAP-INCREASE-KEY(A, n+1, key)
相关文章推荐
- 《算法导论》第6章 堆排序 个人笔记
- 《算法导论》第6章 堆排序 (3)K路归并
- 《算法导论》笔记 第6章 6.1堆
- 算法导论 第6章 堆排序(简单选择排序、堆排序)
- 《算法导论》笔记 第6章 6.1堆
- 算法导论 第6章 堆排序(简单选择排序、堆排序)
- 《算法导论》第6章 堆排序 (2)优先级队列
- 《算法导论》笔记 第6章 6.2保持堆的性质
- 《算法导论》第6章 堆排序 (2)优先级队列
- 算法导论---第6章---堆排序
- 《算法导论》笔记---第6章 堆排序
- 《算法导论》笔记 第6章 6.2保持堆的性质
- 算法导论代码 第6章 堆排序
- 《算法导论》第6章 堆排序 (2)优先级队列
- 《算法导论》第6章 堆排序 (2)优先级队列
- 《算法导论》学习笔记 第6章 二叉堆
- 《算法导论》笔记 第6章 6.3建堆
- 算法导论第6章 堆排序
- 算法导论学习笔记——第6章 堆排序
- 《算法导论》 - 第6章 - 堆排序 - 习题解答