无需递归即可编写迭代深化的 DFS
所以目前我有一个具有以下伪代码的 DFS
procedure DFS(Graph,source):
create a stack S
push source onto S
mark source
while S is not empty:
pop an item from S into v
for each edge e incident on v in Graph:
let w be the other end of e
if w is not marked:
mark w
push w onto S
如何更改此函数以接受限制搜索深度的第三个参数?
So currently i have a DFS with the following pseudocode
procedure DFS(Graph,source):
create a stack S
push source onto S
mark source
while S is not empty:
pop an item from S into v
for each edge e incident on v in Graph:
let w be the other end of e
if w is not marked:
mark w
push w onto S
How do I alter this function to accept a third argument that limits the depth of the search?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
令 Node 为图的每个节点的结构,添加一个名为 level 的字段,然后按如下方式处理图:
Let Node be a structure for each node of the graph, add a field called level, then process the graph as follows:
成功
。S
将包含路径 [root, source) 中的节点。 (不包括源
本身。)success
when an object is found.S
will contain nodes in the path [root, source). (thesource
itself is not included.)