如何理解 DFS 上这篇写得很糟糕的文章?

发布于 2024-11-03 12:22:38 字数 1011 浏览 0 评论 0原文

作为一个母语不是英语的人(俄罗斯),我在维基百科上读到了这篇文章: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 的所有子节点?因为这会产生巨大的变化。

除了那些英语问题之外,他们还忘记提及 dp 的意思,这让我把我所有的头发都扯掉了(是的,甚至是我胡子上的头发)。

文章中的链接只是重复了这个没什么可说的神秘内容。也许有人能够以更易于理解的方式实际重写此代码,并添加更多注释和有意义的变量?我没有找到任何真正好的解释,而不仅仅是为了炫耀作者与 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 技术交流群。

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

发布评论

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

评论(3

衣神在巴黎 2024-11-10 12:22:38

它的意思是:“对于所有直接连接到u的节点v”。

http://en.wikipedia.org/wiki/Depth-first_search 中的伪代码很多更简单。希望这个对你来说效果更好。

如果我没记错的话,dpf 来自 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 and f 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.

春风十里 2024-11-10 12:22:38

我来自类似问题的代码可能对您有帮助:

#A tree is a tuple of an int and a tree. 
t = (1, (2,(4, (6), (7, (9)) )), (3, (5, (8)) ))

def dfs(t):
    if type(t) is int:
        print t
        return 
    print t[0]
    dfs(t[1])
    if len(t) > 2: dfs(t[2]) 

dfs(t)

My code from a similar question might be helpful to you:

#A tree is a tuple of an int and a tree. 
t = (1, (2,(4, (6), (7, (9)) )), (3, (5, (8)) ))

def dfs(t):
    if type(t) is int:
        print t
        return 
    print t[0]
    dfs(t[1])
    if len(t) > 2: dfs(t[2]) 

dfs(t)
笑饮青盏花 2024-11-10 12:22:38

当你遇到语言障碍时,我会少一点责备。
边。

总之,“相邻”就是直接相连的意思。在此图中:

    A   E
   / \ /
  B   C
       \
        D

ABC 相邻,BA 相邻code>、C 相邻
对于 AEDDC 相邻,并且EC 相邻。

这是相同的代码,但更详细一些:

{with the following global variable:
    time, which is a single integer, and initially 0;}

{and a node being a structure of:
    value;
    adjacent-nodes;
    colour, initially white;
    previous-node;
    discovery-time;
    finishing-time;}

{depth-first-search of node u is:
    visit the value of u;
    increase time by 1;
    set the discovery-time of u to time;
    set the colour of u to grey;

    {for all nodes v in the adjacent-nodes of u:
        {if the colour of v is white then:
            set the previous-node of v to u;
            depth-first-search of v;}}

    increase time by 1;
    set the finishing-time of u to time;
    set the colour of u to black;}

此代码中有几件事纯粹是说明性的
目的。中心主题是这样的:

{with a node being a structure of:
    value;
    adjacent-nodes;
    colour, initially white;}

{depth-first-search of node u is:
    visit the value of u;
    set the colour of u to black;

    {for all nodes v in the adjacent-nodes of u:
        {if the colour of v is white then:
            depth-first-search of v;}}}

I would be a bit less reproachful when the language barrier is on your
side.

Anyway, "adjacent" means directly connected. In this graph:

    A   E
   / \ /
  B   C
       \
        D

A is adjacent to B and C, B is adjacent to A, C is adjacent
to A, E, and D, D is adjacent to C, and E is adjacent to C.

Here is the same code, a bit more verbose:

{with the following global variable:
    time, which is a single integer, and initially 0;}

{and a node being a structure of:
    value;
    adjacent-nodes;
    colour, initially white;
    previous-node;
    discovery-time;
    finishing-time;}

{depth-first-search of node u is:
    visit the value of u;
    increase time by 1;
    set the discovery-time of u to time;
    set the colour of u to grey;

    {for all nodes v in the adjacent-nodes of u:
        {if the colour of v is white then:
            set the previous-node of v to u;
            depth-first-search of v;}}

    increase time by 1;
    set the finishing-time of u to time;
    set the colour of u to black;}

There are several things in this code that serve purely illustrative
purposes. The central theme is this:

{with a node being a structure of:
    value;
    adjacent-nodes;
    colour, initially white;}

{depth-first-search of node u is:
    visit the value of u;
    set the colour of u to black;

    {for all nodes v in the adjacent-nodes of u:
        {if the colour of v is white then:
            depth-first-search of v;}}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文