简体   繁体   English

如果记忆是自上而下的深度优先,而 DP 是自下而上的广度优先,那么自上而下的广度优先/自下而上的深度优先等价物是什么?

[英]If memoization is top-down depth-first, and DP is bottom-up breadth-first, what are the top-down breadth-first / bottom-up depth-first equivalents?

I just read this short post about mental models for Recursive Memoization vs Dynamic Programming, written by professor Krishnamurthi .我刚刚阅读了 Krishnamurthi 教授撰写的关于递归记忆与动态编程的心智模型的简短文章 In it, Krishnamurthi represents memoization's top-down structure as a recursion tree, and DP's bottom-up structure as a DAG where the source vertices are the first – likely smallest – subproblems solved, and the sink vertex is the final computation (essentially the graph is the same as the aforementioned recursive tree, but with all the edges flipped).在其中, Krishnamurthi将记忆化的自顶向下结构表示为递归树,将 DP 的自底向上结构表示为 DAG,其中源顶点是第一个(可能是最小的)子问题解决,而汇顶点是最终计算(本质上是图与前面提到的递归树相同,但所有边都翻转了)。 Fair enough, that makes perfect sense.很公平,这完全有道理。

Anyways, towards the end he gives a mental exercise to the reader:不管怎样,最后他给读者一个心理练习:

Memoization is an optimization of a top-down, depth-first computation for an answer.记忆化是对答案的自上而下、深度优先计算的优化。 DP is an optimization of a bottom-up, breadth-first computation for an answer. DP 是对答案的自底向上、广度优先计算的优化。

We should naturally ask, what about我们自然应该问,那呢?

  • top-down, breadth-first自顶向下,广度优先
  • bottom-up, depth-first自下而上,深度优先

Where do they fit into the space of techniques for avoiding recomputation by trading off space for time?它们在哪里适合通过权衡空间来避免重新计算的技术空间?

  • Do we already have names for them?我们已经为他们取了名字吗? If so, what?, or如果是,是什么?,或者
  • Have we been missing one or two important tricks?, or我们是否遗漏了一两个重要的技巧?,或者
  • Is there a reason we don't have names for these?有什么原因我们没有这些名字吗?

However, he stops there, without giving his thoughts on these questions.然而,他停在那里,对这些问题没有给出他的想法。


I'm lost, but here goes:我迷路了,但这里是:

My interpretation is that a top-down, breadth-first computation would require a separate process for each function call.我的解释是,自上而下、广度优先的计算需要对每个函数调用进行单独的处理。 A bottom-up, depth-first approach would somehow piece together the final solution, as each trace reaches the "sink vertex".自底向上、深度优先的方法会以某种方式将最终解决方案拼凑在一起,因为每个跟踪都到达“汇顶点”。 The solution would eventually "add up" to the right answer once all calls are made.一旦完成所有呼叫,该解决方案最终将“累加”到正确答案。

How off am I?我怎么样了? Does anyone know the answer to his three questions?有人知道他三个问题的答案吗?

Let's analyse what the edges in the two graphs mean.让我们分析一下这两个图中的边是什么意思。 An edge from subproblem a to b represents a relation where a solution of b is used in the computation of a and must be solved before it.从子问题ab的边表示一种关系,其中b的解用于计算a并且必须在它之前求解。 (The other way round in the other case.) (在另一种情况下反过来。)

Does topological sort come to mind?会想到拓扑排序吗?

One way to do a topological sort is to perform a Depth First Search and on your way out of every node, process it.进行拓扑排序的一种方法是执行深度优先搜索,然后在离开每个节点的途中对其进行处理。 This is essentially what Recursive memoization does.这基本上就是递归记忆的作用。 You go down Depth First from every subproblem until you encounter one that you haven't solved (or a node you haven't visited) and you solve it.您从每个子问题开始使用深度优先,直到遇到您尚未解决的问题(或您尚未访问的节点)并解决它。

Dynamic Programming, or bottom up - breadth first problem solving approach involves solving smaller problems and constructing solutions to larger ones from them.动态规划,或自下而上 - 广度优先解决问题的方法涉及解决较小的问题并从中构建解决较大问题的方法。 This is the other approach to doing a topological sort, where you visit the node with a in-degree of 0, process it, and then remove it.这是进行拓扑排序的另一种方法,您访问入度为 0 的节点,对其进行处理,然后将其删除。 In DP, the smallest problems are solved first because they have a lower in-degree.在 DP 中,最小的问题首先被解决,因为它们的入度较低。 (Smaller is subjective to the problem at hand.) (较小是对手头问题的主观感受。)

The problem here is the generation of a sequence in which the set of subproblems must be solved.这里的问题是生成必须解决子问题集的序列。 Both top-down breadth-first and bottom-up depth-first can't do that.自上而下的广度优先和自下而上的深度优先都不能做到这一点。 Top-down Breadth-first will still end up doing something very similar to the depth-first counter part even if the process is separated into threads.即使进程被分成线程,自上而下的广度优先最终仍然会做一些与深度优先计数器非常相似的事情。 There is an order in which the problems must be solved.有一个必须解决问题的顺序。 A bottom-up depth-first approach MIGHT be able to partially solve problems but the end result would still be similar to the breadth first counter part.自底向上的深度优先方法可能能够部分解决问题,但最终结果仍然类似于广度优先的对应部分。 The subproblems will be solved in a similar order.子问题将以类似的顺序解决。

Given that these approaches have almost no improvements over the other approaches, do not translate well with analogies and are tedious to implement, they aren't well established.鉴于这些方法与其他方法相比几乎没有任何改进,不能很好地与类比转化并且实施起来很乏味,因此它们还没有很好地建立。

@AndyG's comment is pretty much on the point here. @AndyG 的评论在这里非常重要。 I also like @shebang's answer , but here's one that directly answers these questions in this context, not through reduction to another problem.我也喜欢@shebang 的回答,但这里有一个在这种情况下直接回答这些问题的回答,而不是通过归结为另一个问题。

It's just not clear what a top-down, breadth -first solution would look like.只是不清楚自上而下、广度优先的解决方案会是什么样子。 But even if you somehow paused the computation to not do any sub-computations (one could imagine various continuation-based schemes that might enable this), there would be no point to doing so, because there would be sharing of sub-problems.但是,即使您以某种方式暂停计算以不进行任何子计算(人们可以想象可能实现这一点的各种基于延续的方案),这样做也没有意义,因为会共享子问题。

Likewise, it's unclear that a bottom-up, depth -first solution could solve the problem at all.同样,自下而上、深度优先的解决方案能否解决这个问题也不清楚。 If you proceed bottom-up but charge all the way up some spine of the computation, but the other sub-problems' solutions aren't already ready and lying in wait, then you'd be computing garbage.如果您自下而上进行,但一直在计算的某些脊椎上充电,但其他子问题的解决方案尚未准备好并等待,那么您就是在计算垃圾。

Therefore, top-down, breadth-first offers no benefit, while bottom-up, depth-first doesn't even offer a solution.因此,自上而下的广度优先没有任何好处,而自下而上的深度优先甚至没有提供解决方案。

Incidentally, a more up-to-date version of the above blog post is now a section in my text (this is the 2014 edition; expect updates .顺便说一句,上述博客文章的更新版本现在是我文本中的一个部分(这是 2014 版;期待更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从上到下到自下而上的算法(DP) - Going from a top-down to a bottom-up algorithm (DP) 通过深度优先或宽度优先发现文件夹树 - Discovering folder tree via depth-first or breadth-first 深度优先搜索与广度优先搜索 - Depth-First Search vs. Breadth-First Search Mergesort - 自上而下快于自上而下吗? - Mergesort - Is Bottom-Up faster than Top-Down? 何时使用自下而上 DP 何时使用自上而下 DP - when to use bottom-up DP and when to use top-down DP 哪个图具有相同的广度优先遍历和深度优先遍历? - Which graph has same Breadth-First traversal and Depth-First Traversal? 就时间复杂度而言,自下而上的 DP 解决方案是否优于自上而下? - Is Bottom-up DP solution better than Top-down in terms of Time complexity? 广度优先与深度优先搜索的输入/输出 - Input/output of breadth-first vs. depth-first search Java中的Farmer,W​​olf,Goat和Cabbage广度优先和深度优先搜索 - Farmer, Wolf, Goat and Cabbage Breadth-first and Depth-first Search in Java POSIX ls -R是否规定了特定的遍历顺序? 如果没有,那么哪个假设更可能是可移植的:深度优先还是广度优先? - Does POSIX ls -R dictate a specific traversal order? If not, then which assumption is more likely to be portable: depth-first or breadth-first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM