使用有限内存的迭代加深深度优先搜索

发布于 2024-07-25 20:21:31 字数 804 浏览 2 评论 0原文

这是在二进制文件中查找第一个空值的后续内容内存有限的树

维基百科说,迭代深化深度优先搜索将找到最短路径。 我想要一个将内存限制为 k 个节点并且访问树的次数最少的实现。

例如,如果我的二叉树是:

           0
    1             2
 3    4        5      6
7 8  9 10    11 12  13 14

我的内存限制为 5 个节点,而我的搜索顺序是:

mem[0] = read node 0
mem[1] = read node 1
mem[2] = read node 2
mem[3] = read node 3
mem[4] = read node 4 //Now my memory is full.  I continue...
mem[3] = read node 5 //overwrite where I stored node 3
mem[4] = read node 6 //overwrite where I stored node 4

现在,如果我的下一次读取是 7,我需要重新读取 3。但是如果我下一次读取到14,那么我还不需要重新阅读3。 如果解是 14,这将使我的算法更快一些!

我正在寻找一个通用的解决方案; 适用于任何大小的内存和每个节点的分支数量的东西。

This is a follow-up to Find first null in binary tree with limited memory.

Wikipedia says that iterative-deepening depth first search will find the shortest path. I would like an implementation that is limited in memory to k nodes and accesses the tree the least number of times.

For instance, if my binary tree is:

           0
    1             2
 3    4        5      6
7 8  9 10    11 12  13 14

And I'm limited to 5 nodes of memory than my search order is:

mem[0] = read node 0
mem[1] = read node 1
mem[2] = read node 2
mem[3] = read node 3
mem[4] = read node 4 //Now my memory is full.  I continue...
mem[3] = read node 5 //overwrite where I stored node 3
mem[4] = read node 6 //overwrite where I stored node 4

Now if my next read is to 7, I need to re-read 3. But if I make my next read to 14, then I don't need to re-read 3 just yet. If the solution is at 14, this will make my algorithm a bit faster!

I'm looking for a general solution; something that will work for any size memory and number of branches per node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-08-01 20:21:31

如果您的节点链接到其父节点,并且节点的子节点将始终以相同的顺序枚举,则您可以跟踪您的步骤而无需保存它们。

If your nodes link to their parents, and the children of a node will always be enumerated in the same order, you can trace your steps without having to save them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文