使用有限内存的迭代加深深度优先搜索
维基百科说,迭代深化深度优先搜索将找到最短路径。 我想要一个将内存限制为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的节点链接到其父节点,并且节点的子节点将始终以相同的顺序枚举,则您可以跟踪您的步骤而无需保存它们。
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.