![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
高级数据结构
文章平均质量分 71
??
HGGshiwo
这个作者很懒,什么都没留下…
展开
-
理解回溯
解决的问题很难靠循环枚举解决(比如具有不确定的循环次数)循环解决效率太低可以用决策树表示算法理解回溯算法本质上是一个暴力DFS。DFS的对象是决策树。因此如果一个问题可以用决策树表示,那么可以用回溯解决。算法过程递归:使用函数DFS(node)1.如果node达到终止状态,进行验证。如果可行加入解集。退出。2.如果node未达到终止状态,执行DFS(sibling),sibling是DFS的所有未遍历过的兄弟。循环:使用栈1.将第一步所有可能入栈。2.如果栈空则退出,返回fals原创 2021-04-08 23:09:57 · 241 阅读 · 0 评论 -
ADS1:AVL Trees, Splay Trees, and Amortized Analysis
AVL树:struct property:BST,满足左节点小于根小于右节点,子树为BSTorder property:要求左子树和右子树的高度之差不超过2高度:空子树高度为-1,1个节点高度为01.高度计算高度为h的最小节点数:左侧为nh−1n_{h-1}nh−1,右侧为nh−2n_{h-2}nh−2或者互换,因此递推式为:nh=nh−1+nn−2+1n_h=n_{h-1}+n_{n-2}+1nh=nh−1+nn−2+1结果为:nh=Fh+2−1n_h=F_{h+2}-1nh=Fh原创 2021-03-05 20:59:44 · 343 阅读 · 0 评论 -
ADS2: Red-Black Trees and B+ Trees
红黑树对BST进行约束,使得树高在一定的复杂度之中。分为红黑节点,黑节点增加了对树的约束,红节点使得数据结构不是那么严格。规定根一定是黑的,空指针NIL一定是黑的。对黑节点的约束:对于每个点,从该点出发到达叶子经过的所有路径中黑节点个数必须相同对于红节点的约束:不能有连续的两个红节点判断判断能否成为红黑树:看每个节点到叶子的黑节点个数能否相等。注意其实所有的叶子都是一个external节点,且是黑的,实际上数的时候不考虑进去。由于3右侧到叶子黑节点为0,因此左侧为0,发现12是连续红节点,原创 2021-03-07 22:07:11 · 204 阅读 · 0 评论 -
ADS3 Inverted File Index
Term-Document Incidence Matrix行代表关键词,列代表了第几个文档。如果关键词存在这个文档,则为1,否则为0.形成一个矩阵。Inverted File IndexIndex is a mechanism for locating a given term in a text.Inverted file contains a list of pointers (e.g. the number of a page) to all occurrences of that term原创 2021-03-13 20:03:15 · 246 阅读 · 0 评论 -
ADS4 Leftist Heaps and Skew Heaps
Leftist HeapsNpl(X)将树最外侧看成海岸线,离海岸线经过的节点数。Npl(leaf)=0Leftist Heap1.order property:要求满足父亲小于任何一个子节点。2.Structure Property:要求是二叉树即可3.leftist heap property:任意一个节点,左子节点的Npl大于等于右子节点A leftist tree with r nodes on the right path must have at least 2^r – 1 n原创 2021-03-20 21:04:02 · 375 阅读 · 0 评论 -
ADS5 Binomial Queue
二项式队列1.对于Bk根节点孩子的数量:kkk节点的总数:2k2^k2k深度为d的节点的数量为:C(k,d)C(k,d)C(k,d)总的深度为:k+1k+1k+1构造Bk就是把两个Bk-1合并起来。父节点比孩子要小。2.n个节点用二项式树的组合表示:将n写作二进制数,二进制为1的位,说明需要一棵Bk比如13=1101,需要B0,B2,B33.FindMin遍历根的所有孩子,找到最小的4.Merge两个森林Merge:从低位开始,对应位的Bk Merge,变为Bk+1Bk的me原创 2021-03-28 11:25:30 · 112 阅读 · 0 评论 -
ADS08 Dynamic Programming
动态规划DP,就是用分治的方法,但是记录下小问题的解,减少重复计算。Fibonacci Numbers问题描述计算:T(N) = T(N-1) + T(N-2)解法1.令NextoLast = T(0), Next = T(1)2.计算T(i) = NextToLast + Next3.更新Next = NextToLast, NextToLast = T(i)4.重复N次。这个算法需要记录的只是最近的两次。复杂度为O(N)int Fibonacci ( int N ) {原创 2021-04-20 09:44:57 · 83 阅读 · 0 评论 -
ADS09 Greedy Algorithms
只是局部最佳,不是全局最佳贪心策略:制定一个贪心策略,然后对当前的选项选一个,然后解决剩下的子问题。Activity Selection Problem给定两个数组,分别存储这第i个元素开始和结束的时间。求最多完成几个任务。方法:总是找最先结束的,但是不能覆盖已经在解集中的。复杂度O(NlogN)如果每件事有权值,则贪心错误,但是DP还是正确的。DP的递推式:c[1,j]=max{c[1,j-1],c[1,k(j)]+1}c是可以做的事情件数Huffman Codes要求给字符编码,字原创 2021-05-17 23:47:35 · 102 阅读 · 0 评论 -
ADS10 NP-Completeness
TURING MACHINE:Infinite Memory, finite stateNondeterministic Turing Machine: 可以自由选择下一步是什么Deterministic Turing Machine:下一步是什么是不确定的NP:任何解都可以在多项式时间内得到NP-Complete Problems:所有NP问题可以归约为NPC问题如果问题A可以在多项式时间中转换为问题B,问题B是NP,那么问题A是NPabstract problem: 是从问题集合I到解集合S原创 2021-05-25 08:47:03 · 99 阅读 · 0 评论 -
ADS11 Approximation
Approximation Ratioapproximation ratio: 较优解和最优解的比值或者倒数的最大值小于它。ρ\rhoρ(n)-approximation algorithm:有近似率的算法。approximation scheme:较优解和最优解的比值是1+ϵ\epsilonϵPTAS: 复杂度n是多项式级别的。FPTAS: 复杂度n和1/ϵ1/\epsilon1/ϵ都是多项式级别的。Approximate Bin Packing将Si大小在0-1之间,bin的大小为1,问最原创 2021-06-30 22:02:27 · 205 阅读 · 0 评论 -
ADS12LocalSearch
Metropolis Algorithm在邻域中寻找,如果结果更优,则更新状态。如果结果不是更优,有一定概率更新状态。Vertex Cover Problem.找到最小子图S,使得图中所有的边至少有一个点在S中。随机删除或者增加一些点,如果cost变小,则将更新解集S,如果变大,则有一定概率更新解集。注意删除了点,边并没有消失,如果边的两个点都被删除,则是不允许的。Hopfield Neural Networks给图中的点进行标注state,1或者-1,如果边的权值小于0,则两个点倾向于相同的原创 2021-06-30 20:39:52 · 346 阅读 · 0 评论 -
ADS13 Randomized Algorithms
Hiring problem如果此时的对象i是前i个人里面最好的,那么就雇佣他。随机算法:将这些人随机化雇佣的人数:对第i个人,他是前i个人里面最好的概率为1/i, 因此期望为∑1i=O(lnN)\sum \frac1i=O(\ln N)∑i1=O(lnN)hir only once: 只录用1个人,先面试k个人,全部不录用。然后从k+1个人开始,如果比k个人最好的人都好,那么就录用。录于i的条件:1. 第i个人是前i个人里面最好的,2.从第k+1到第i-1没有录于任何人。这意味着前i-1个人原创 2021-07-06 23:07:27 · 163 阅读 · 0 评论 -
ADS14 Parallel Algorithms
summation problem计算A(1)+…+A(N), 并行两个两个加,再并行第一次加法后的两个两个加work load 是N, T(N)=logNPrefix-Sums计算A(1), A(1)+A(2),…, A(1)+A(2)+…+A(N)一共N个值。C(i,j)的含义是:以C(i,j)为根的树,最右边的叶子加到A(1)的值B是从下往上算,C是从上往下计算。work load O(N), T(N)=O(logN)Merging找到A中每个元素在B中排名,B中每个元素在A中排名,原创 2021-07-07 09:33:09 · 264 阅读 · 0 评论 -
ADS期中抱佛脚
1.avl tree:插入看根的三代,是RR还是LL还是RL还是LR,如果是LR的,则最下面的节点作为中间节点,如果是RR,则左右互换。要求通过左右子树到叶子的路径长度相差不超过12.splay tree:要求访问一次就换到根上插入或者访问如何换到根上:该节点往上看三代,如果是RL或者LR,则换到中间如果是LL或者RR,则倒过来连。子树的连接,全部看如何满足二叉树。复杂度总结:树复杂度avlO(logN)splay摊销均为O(logN)binominal原创 2021-05-09 20:55:40 · 121 阅读 · 0 评论 -
[题集]Lecture2.Red-Black Trees and B+ Trees
1.A 2-3 tree with 3 nonleaf nodes must have 18 keys at most.一个叶子节点最多放3个值,然后是两个节点指向3个叶子,然后是一个根指向两个节点。那么一共有2*3个叶子,一个叶子存放3个key,一共18个.注意key只算叶子上的。T2.In the red-black tree that results after successively inserting the keys 41; 38; 31; 12; 19; 8 into an initi原创 2021-05-03 23:05:55 · 1422 阅读 · 0 评论 -
[题集]Lecture3. Inverted File Index
1.In distributed indexing, document-partitioned strategy is to store on each node all the documents that contain the terms in a certain range.磁盘分区索引技术,每个节点存放部分索引,不是所有的。F2.When evaluating the performance of data retrieval, it is important to measure the r原创 2021-05-02 11:31:12 · 1213 阅读 · 1 评论 -
[题集]Lecture 4. Leftist Heaps and Skew Heaps
Lecture 4. Leftist Heaps and Skew Heaps1.A leftist heap with the null path length of the root being r must have at least 2r+1−12^{r+1}-12r+1−1 nodes T用递归:假如r成立,则对r+1,右路径r个点,则右子树有2r+1−12^{r+1}-12r+1−1个点。而左子树Npl至少也为r,则左子树的右路径至少r,则左子树至少2r+1−12^{r+1}-12r+1原创 2021-03-23 08:39:52 · 2396 阅读 · 0 评论 -
[题集]Lecture 5. Binomial Queue
1.Which of the following binomial trees can represent a binomial queue of size 42?A.B0 B1 B2 B3 B4 B5B.B1 B3 B5C.B1 B5D.B2 B442用二进制表示就是B2.Delete the minimum number from the given binomial queues in the following figure. Which one of the following sta原创 2021-05-01 21:26:09 · 662 阅读 · 0 评论 -
[题集]Lecture 6. Backtracking
1.It is guaranteed that an exhaustive search can always find the solution in finite time.F就是遍历整个树的搜索。我猜可能是不一定能找到solution2.In the 4-queens problem, (x1, x2, x3, x4) correspond to the 4 queens’ column indices. During backtracking, (1, 3, 4, ?) will be che原创 2021-05-01 18:52:31 · 1111 阅读 · 1 评论 -
[题集]Lecture 8. Dynamic Programming
1.To solve a problem by dynamic programming instead of recursions, the key approach is to store the results of computations for the subproblems so that we only have to compute each different subproblem once. Those solutions can be stored in an array or a h原创 2021-05-01 18:09:01 · 702 阅读 · 0 评论 -
[题解]Lecture 9. Greedy Algorithms
1.Greedy algorithm works only if the local optimum is equal to the global optimum.T,只有在局部解和全局解相等的时候可以用2.In a greedy algorithm, a decision made in one stage is not changed in a later stage.T,做出选择后不会改变...原创 2021-06-30 17:26:43 · 643 阅读 · 0 评论 -
[题解]ADS10 NP-Completeness
1.If L1 ≤p L2 and L2∈NP, then L1∈NP.T注意<=p等价于reduce to,复杂的如果是Np,那么简单的也是NP2.All NP-complete problems are NP problems.T3.All the languages can be decided by a non-deterministic machine.F不确定图灵机可以用来验证NP问题的解是否是正确的,确定图灵机可以用来求解P问题。NP hard问题无法通过不确定图灵机.原创 2021-06-30 20:38:45 · 2179 阅读 · 1 评论 -
[题解]Lecture 11. Approximatio
1.An approximation scheme that runs in O(N23ϵN^23\epsilonN23ϵ)for any fixed is a polynomial-time approximation scheme.T如果n是多项式就可以。2.An 1+ϵ1+\epsilon1+ϵ-approximation scheme of time complexity (n+1ϵ)3(n+\frac1\epsilon)^3(n+ϵ1)3 is a PTAS but not an F原创 2021-06-01 10:46:56 · 321 阅读 · 0 评论 -
[题解]ADS11 Approximation
1.Suppose ALG is an α-approximation algorithm for an optimization problem Π whose approximation ratio is tight. Then for every ϵ>0 there is no (α−ϵ)-approximation algorithm for Π unless P = NP.F, 按照我的理解,可能会有近似比更小的算法。2.As we know there is a 2-approxima原创 2021-07-06 21:44:44 · 2099 阅读 · 2 评论 -
[题解]ADS12LocalSearch
1.In local search, if the optimization function has a constant value in a neighborhood, there will be a problem.T2.Greedy method is a special case of local search.F3.Random restarts can help a local search algorithm to better find global maxima that ar原创 2021-06-30 22:02:04 · 1212 阅读 · 0 评论 -
[题集]ADS13 Randomized Algorithms
1.Let a=(a1,a2,…,ai,…,aj,…,an) denote the list of elements we want to sort. In the quicksort algorithm, if the pivot is selected uniformly at random. Then any two elements get compared at most once and the probability of ai and aj being compared is 2/(j原创 2021-07-06 23:57:22 · 1962 阅读 · 0 评论 -
[题解]ADS14 Parallel Algorithms
1.While comparing a serial algorithm with its parallel counterpart, we just concentrate on reducing the work load.F, 不仅关注work load 而且还有时间复杂度2.To evaluate the Prefix-Sums of a sequence of 16 numbers by the parallel algorithm with Balanced Binary Trees, C(原创 2021-07-07 14:15:02 · 1574 阅读 · 0 评论 -
[题解]ADS15 External Sorting
1.Suppose we have the internal memory that can handle 12 numbers at a time, and the following two runs on the tapes:Run 1: 1, 3, 5, 7, 8, 9, 10, 12Run 2: 2, 4, 6, 15, 20, 25, 30, 32Use 2-way merge with 4 input buffers and 2 output buffers for parallel o原创 2021-07-06 20:29:20 · 585 阅读 · 0 评论