update words.md and conclude heap&priority queue.

This commit is contained in:
Shine wOng
2019-11-13 16:03:06 +08:00
parent 258c820b6a
commit 0bb66cbf5e
2 changed files with 38 additions and 0 deletions

View File

@@ -1,6 +1,16 @@
完全二叉堆知识总结
================
## 知识脉络
本章的主要内容是完全二叉堆和左式堆,它们都是用于[优先级搜索]这一特定的应用场景的,即每次只是找到其中的最值元素。完全二叉堆的关键在于`堆序性``结构性`——得益于`结构性`,完全二叉堆可以使用`向量`来存储,在相当地程度上简化了它的实现;左式堆主要用于`堆的合并`该操作是通过递归地将堆A的右子堆与堆B合并再将合并后的堆重新连接在堆A的右孩子上面来实现其性能正比于两堆的最右侧通路长度之和。左式堆的关键在于`堆序性``左倾性`,即任意节点左孩子的`空节点路径长度`(npl, null path length)都不小于右孩子,基于此左式堆的最右侧路径即是全局最短路径,从而保证了`堆的合并`高效地进行。由于需要频繁地提取和接入堆的右子堆,左式堆显然不能基于`向量`实现,而是需要使用`二叉树`作为底层结构。
堆在前面讨论过的算法中有不少应用,比如第六章的`优先级搜索`(PFS, Priority First Search)中,每一步迭代都需要找到所有节点中优先级最高的节点,使用一个堆结构可以将该算法的时间复杂度提升至`O((n + e)logn)`,需要指出的是这里的堆结构还应该支持节点的优先级修改(提升)操作,这可以简单地通过`上滤`(percolate up)来实现。可以注意到,在稠密图的情况下,引入了堆结构以后该算法的性能反而下降了,为了解决这个问题,可以引入一个`多叉堆`(d-heap),其`上滤``下滤`操作的时间复杂度分别为`O(log_d^n)`以及`O(dlog_d^n)`,如果令`d = e/n + 2`,此时的整体时间性能为`O(ndlog_d^n + elog_d^n)`,则无论在稀疏图还是稠密图,都可以自适应得到最优的性能。
优先级队列在外部排序中也有相应的应用。例如在归并排序中为了减少I/O的访问次数往往采用`多路归并`的方法,此时每一次归并都需要从`m`路的数据中选择出最值。在实际应用中,往往是使用`锦标赛树`来实现,顾名思义,`锦标赛树`是采用类似于锦标赛的模式,对数据进行两两比较,层层筛选最终得到全局的最值(即冠军)。在这种应用场合下,锦标赛树的性能是优于堆的,尽管只是在常系数的层面上,这是因为堆的`下滤`每次需要两次比较,而锦标赛树的每一步`重赛`(replay)只需要一次,同理在[从n个元素中选取最小的k个](k << n)问题中,锦标赛树也具有更优的性能。
锦标赛树还具有一些问题,例如每次`重赛`都需要沿着路径迂回访问邻居节点,为了避免这些不必要的迂回,可以把每次比较的[败者]记录在父亲节点当中,这样每次`重赛`就只需要和父亲节点进行比较,这就是`败者树`,在`多路归并选择排序`当中,实际上用的就是败者树。
## 优先级队列
在很多具体的应用条件下我们都只关心一组数据中的最大值或者最小值比如考完试大家首先都是看谁是第一名谁又是最后一名比如我只知道世界最高峰是珠穆朗玛峰却不知道后面的第二第三都是什么比如在操作系统中的诸多算法都是基于优先级来进行的像是页面置换算法还有进程调度算法这个时候总是选出其中优先级最高的页面将它换出或者优先级最高的进程让它占用CPU。

View File

@@ -1774,4 +1774,32 @@ Some Words
- The local people like to vaunt the glories of their faded past.
- His much vaunted new plan has serious weaknesses.
## 11th, November
+ shelter
> (n)(a building designed to give) protection from bad weather, danger or attack
- They opened a shelter to provide temporary housing for the city's homeless.
- a shelter for homeless women
+ comprehensive
> (adj)complete and including everything that is necessary
- He has writen a fully comprehensive guide to Rome.
- We offer you a comprehensive training in all aspected of the business.
+ coordination
> (n)the act of making all the people involved in a plan or activity work together in an organized way.
- the lack of coordination between the civilian and military authorities
+ package deal
> (n)a set of arrangements that must be accepted together and not separately.</br>
> (n)the offer of a low price if several things are bought together.
- The package deal under discussion will require every country to be flexible on its immigration laws.
- Many small hotels offer accommodation and breakfast as a package deal.
+