如何理解 DFS 上这篇写得很糟糕的文章?
作为一个母语不是英语的人(俄罗斯),我在维基百科上读到了这篇文章:http://en.wikibooks.org/wiki/Artificial_Intelligence/Search/Heuristic_search/Depth-first_search
,我尝试遵循这个用硬核英语编写的伪代码示例,几乎没有任何解释或评论。
特别是,我不明白他们试图用这句话表达什么:
DFS(u):
visit(u);
time = time + 1;
d[u] = time;
color[u] = grey;
for all nodes v adjacent to u do
if color[v] == white then
p[v] = u;
DFS(u);
time = time + 1;
f[u] = time;
color[u] = black;
对于与 u 相邻的所有节点 v
我对这句话的问题是“相邻”部分。我的字典说这意味着“邻居”之类的东西。那么我必须迭代u的超节点的子节点吗?请注意,u 是图中的一个节点。
或者他们是想说我必须迭代 u 的所有子节点?因为这会产生巨大的变化。
除了那些英语问题之外,他们还忘记提及 d 和 p 的意思,这让我把我所有的头发都扯掉了(是的,甚至是我胡子上的头发)。
文章中的链接只是重复了这个没什么可说的神秘内容。也许有人能够以更易于理解的方式实际重写此代码,并添加更多注释和有意义的变量?我没有找到任何真正好的解释,而不仅仅是为了炫耀作者与 DFS 相关的主导智慧。
因此,如果有人能以一种更好的方式即时重写它,并具有更大的学习价值,这将拯救我的日子,拯救我的胡子。保存一切。谢谢。
As someone who has not english as moms language (Russia) I read this article at Wikipedia: http://en.wikibooks.org/wiki/Artificial_Intelligence/Search/Heuristic_search/Depth-first_search
and I try to follow this pseudo code example written in hardcore english with less to nothing explanations or comments.
In particular, I don't get what they try to say with this sentence:
DFS(u):
visit(u);
time = time + 1;
d[u] = time;
color[u] = grey;
for all nodes v adjacent to u do
if color[v] == white then
p[v] = u;
DFS(u);
time = time + 1;
f[u] = time;
color[u] = black;
for all nodes v adjacent to u do
My problem with this sentence is the "adjacent" part. My dictionary says this means something like "neighbor". So I have to iterate over the subnodes of the supernode of u? Note that u is a node in the graph.
Or are they trying to say I must iterate over all the subnodes of u? Because this would make a huge difference.
Besides those english problems they forgot to mention what d and p mean, which makes me ripp all my hairs out (yes, even those from my mustache).
The links in the article just repeat this not-much-telling cryptic stuff. Maybe someone was able to actually re-write this in a way that is more humanly readable, with more comments and meaningful variables? I didn't find any really good explanation that isn't just there to show off the dominating intelligence of the writer related to DFS.
So if someone can re-write that on the fly in a better way with greater learning value that would save my day, save my mustache. Save everything. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它的意思是:“对于所有直接连接到u的节点v”。
http://en.wikipedia.org/wiki/Depth-first_search 中的伪代码很多更简单。希望这个对你来说效果更好。
如果我没记错的话,
d
、p
和f
来自 Cormen 的算法书籍。它们分别指的是发现节点的时刻、DFS 遍历树上的前一个节点以及节点完成的时刻。其中一些对于某些应用程序很有用(例如拓扑排序、查找组件和其他围绕 DFS 的算法),但它们对于 DFS 本身并不重要。It means: "For all nodes v directly connected to u".
Pseudocode in http://en.wikipedia.org/wiki/Depth-first_search is much simpler. Hope this one works better for you.
If I remember correctly, the
d
,p
andf
come from Cormen's book on algorithms. They mean (respectively) the moment the node was discovered, previous node on the DFS traversal tree and the moment the node was finished. Some of them are useful for some applications (such as topological sorting, finding components and other algorithms around DFS), but they're not crucial for DFS itself.我来自类似问题的代码可能对您有帮助:
My code from a similar question might be helpful to you:
当你遇到语言障碍时,我会少一点责备。
边。
总之,“相邻”就是直接相连的意思。在此图中:
A
与B
和C
相邻,B
与A
相邻code>、C
相邻对于
A
、E
和D
,D
与C
相邻,并且E
与C
相邻。这是相同的代码,但更详细一些:
此代码中有几件事纯粹是说明性的
目的。中心主题是这样的:
I would be a bit less reproachful when the language barrier is on your
side.
Anyway, "adjacent" means directly connected. In this graph:
A
is adjacent toB
andC
,B
is adjacent toA
,C
is adjacentto
A
,E
, andD
,D
is adjacent toC
, andE
is adjacent toC
.Here is the same code, a bit more verbose:
There are several things in this code that serve purely illustrative
purposes. The central theme is this: