哈尔滨工业大学深圳 高级算法设计 2017 考试 复习题 重点
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Insertion Sort(原理及伪代码学习,第一次出现伪代码)Asymptotic Time Analysis:Big O (upper bound) and Big Ω (lower bound) Merge Sort (Divide-and-Conquer)
Recurrences: three methods.
Substitution method: guess→verify→solve
Recursion-tree method:
Master method: 应用于形如T(n)=aT(n/b)+f(n),a>=1,b>1
证明主方法
Big Integer Multiplication
Strassen matrix multiplication Chessboard Cover
Shell Sort 缩小增量
Comparison Sort ( O(nlgn) the best we can do)
Counting sort: No comparisons between elements.
Counting sort is not a comparison sort.
Counting sort is a stable sort: it preserves the input order among equal elements.
Why don’t we always use counting sort?
Because it depends on range k of elements
Radix Sort
run counting sort on each digit.(从低位往高位排)
Sort on least-significant digit first with auxiliary stable sort
Bucket Sort
In Bucket sort, the range [a,b] of input numbers is divided into m equal sized intervals, called buckets.
Hash Table
给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash) 函数。对不同的关键字可能得到同一散列地址,即k1≠k2,而f(k1)=f(k2),这种现象称为碰撞(英语:Collision)。
Let n be the number of keys in the table, and
let m be the number of slots.
Define the load factor of T to be α= n/m =average number of keys per slot.
The expected time for an unsuccessful search for a record with a given key is = Θ(1 + α) .
Hash function:
Division method h(k) = k mod m.
Don’t pick an m that has a small remainder d. If m= 2r, then the hash
doesn’t even depend on all the bits of k.
Pick m to be a prime not too close to a power of 2 or 10 and not otherwise used prominently
in the computing environment. Sometimes, making the table size a prime is inconvenient
Multiplication Method:
Resolving collisions by open addressing
Binary Search Trees
属性: key[left(x)]≤key[x]≤key[right(x)]
traversa l遍历
Inorder Tree Walk: print left, then root, then right.
Preorder tree walk: print root, then left, then right.
Postorder tree walk: print left, then right, then root.
n个结点子树,时间Θ(n)
Search
第二种迭代的方式更高效。
Insert
The running time of TreeSearch() and TreeInsert() O(h),h is the height of a BST
The worst case of h=O(n) when tree is just a linear string of left or right children.
Successor (顺序遍历当前节点的后继)节点val值大于该节点val值并且值最小的节点
1.x存在右子树,后继是右子树中的最小结点
2.x没有右子树,后继是x的最低的祖先,其左孩子同样是x的祖先。
Delete
1.x has no children→remove x
2.x has one children→splice out(剪接) x
3.x has two children→swap x with successor, then perform case 1 or 2 to delete it.
Sorting with binary search trees
Since time is proportional to the number of comparisons, same time as quicksort O(nlgn)
Red-Black tree
性质1. 节点是红色或黑色。
性质2 每个叶节点(NIL节点,空节点)是黑色的。
性质3 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
性质4. 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。
性质5. 根节点是黑色。
Black-height: # of nodes on path to leaf.
定理:拥有n个结点的红黑树高度h≤2lg(n+1)