广度优先与深度优先搜索的输入/输出

发布于 2024-10-03 08:10:17 字数 774 浏览 0 评论 0原文

我的问题实际上并不是关于这两种搜索类型的机制。我觉得它比这更平凡 - 我不理解其中任何一个的输入和输出。更具体地说,在 CLRS 中,BFS 将图和源节点作为输入,但 DFS 仅采用图。 DFS 不关心你从哪里搜索吗?

这就是输入混乱。输出混乱在于,在 DFS 中,当你完成后,你会得到一个类似表的结构,记录每个节点的发现和完成时间,对吗?如何从中提取解决方案,即从源节点到目标节点的路径?

我希望我说得有道理。谢谢!

编辑:这就是我所说的 DFS 不采用源节点的意思。这是来自 CLRS 的 DFS 伪代码。我没有看到它在任何地方使用源节点。我看到它所做的只是遍历图中的所有节点。

DFS(G)
1 for each vertex u ∈ V[G]
2 do color[u] ← WHITE
3 π[u]← NIL
4 time ← 0
5 for each vertex u ∈ V[G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)

DFS-VISIT(u)
1 color[u] ← GRAY ✄ White vertex u has just been discovered.
2 time ← time+1
3 d[u] ← time
4 for each v ∈ Adj[u] ✄ Explore edge (u,v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ✄ Blacken u;it is finished.
9 f [u] ← time ← time+1

My question isn't really about the mechanism of either search type. I feel it's a lot more mundane than that - I don't understand the input and output of either. More specifically, in CLRS, BFS takes as input a graph and a source node, but DFS takes only a graph. Does DFS not care where you search from then?

So that's the input confusion. The output confusion is that in DFS, when you're done you have a table-like structure recording each node's discovery and finish time, right? How do you extract a solution, i.e. a path from source to destination nodes, from that?

I hope I'm making sense. Thanks!

Edit: here's what I mean by DFS not taking a source node. This is the DFS pseudocode from CLRS. I don't see it taking a source node anywhere. All I see it doing is going through ALL the nodes in the graph.

DFS(G)
1 for each vertex u ∈ V[G]
2 do color[u] ← WHITE
3 π[u]← NIL
4 time ← 0
5 for each vertex u ∈ V[G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)

DFS-VISIT(u)
1 color[u] ← GRAY ✄ White vertex u has just been discovered.
2 time ← time+1
3 d[u] ← time
4 for each v ∈ Adj[u] ✄ Explore edge (u,v).
5 do if color[v] = WHITE
6 then π[v] ← u
7 DFS-VISIT(v)
8 color[u] ← BLACK ✄ Blacken u;it is finished.
9 f [u] ← time ← time+1

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

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

发布评论

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

评论(2

丢了幸福的猪 2024-10-10 08:10:17

输入混乱:

CLRS 给出的特定 DFS 不关心您从哪里搜索。搜索的确切结果将取决于 V[G] 中节点的顺序。通常我会认为 DFS 从一个节点开始,例如:

DFS-Simple(G, s)
1 for each vertex u ∈ V[G]
2   do color[u] ← WHITE
3 π[u]← NIL
4 time ← 0
5 DFS-VISIT(s)

CLRS 的版本会生成一个森林(图的每个组件对应一棵树),而不是仅仅一棵树,这可能更适合其目的。

输出混乱:

路径不是通过时间戳记录的,而是通过父指针π记录的。例如,给定一个节点 v,您可以像这样打印其根节点的路径:

Print-Path-To-Root(v)
1 while v ≠ Nil
2   do print v
3      v ← π[v]

The input confusion:

The particular DFS given by CLRS does not care about where you search from. The exact result of the search will depend on the ordering of the nodes in V[G]. Normally I would think of DFS as starting from a node, e.g.:

DFS-Simple(G, s)
1 for each vertex u ∈ V[G]
2   do color[u] ← WHITE
3 π[u]← NIL
4 time ← 0
5 DFS-VISIT(s)

The version of CLRS produces a forest (one tree for each component of the graph) instead of just a single tree, which presumably suited their purpose better.

The output confusion:

The paths are recorded not by the time stamps, but by the parent pointers π. For example given a node v, you can print the path to its root node like this:

Print-Path-To-Root(v)
1 while v ≠ Nil
2   do print v
3      v ← π[v]
貪欢 2024-10-10 08:10:17

BFS 和 DFS 都将源节点作为输入。

当使用 DFS 进行寻路时,您只需在找到节点时停止,然后一直向上堆栈到原始节点以查找路径。

Both BFS and DFS take as input a source node.

When doing pathfinding with DFS, you simply stop when you find your node, and then work up the stack all the way to the original node to find the path.

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