像这样遍历有向图的算法(内图)
我有一个像这样的图表:
一个简单的规则:
图中的每个节点只知道其后继节点。
正如你所看到的,当我们来到 6
时(通过第一个分支,1 → 6
),问题发生了,因此我们不知道什么时候该停止并开始遍历另一个分支 (2 → 6
)。
有人可以建议一种遍历这样的图的算法吗?
当我遍历 1 → 6 → end of graph
,然后返回到 2 → 6
时,我想到了这个想法。
但我认为这不是一个好主意,因为在 1 → 6 → end of graph
方式上可能会有很多分叉。
I have a graph like this:
One simple rule:
Every node in the graph only knows about its successor.
As you can see, the problem occurs when we came to 6
(by the first branch, 1 → 6
), so that we do not know when it is time to stop and start traversing another branch (2 → 6
).
Could anyone suggest an algorithm for traversing graph like this, please?
I came up with the idea when I am traversing 1 → 6 → end of graph
, and then returning to 2 → 6
.
But I think this is not good idea, because there could be a lot of forks on the 1 → 6 → end of graph
way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从根本上来说,只有两种遍历图的方法。
当然,还有很多其他选项,但这些是最简单且最普遍适用的。无论如何,其他算法大多可以被认为是这些主题的变体。选择最适合您问题的一项,然后进城!
Fundamentally, there are only two ways to traverse a graph.
There are plenty of other options, of course, but these are the simplest and most generally applicable. The other algorithms can mostly be thought of as variations on these themes anyway. Choose whichever one is best for your problem and go to town!
递归地,当你穿过每个节点时,你会标记它,当没有什么可以探索时,你会返回。
类似于
C 示例,如果图由相邻矩阵表示,长度为 1000 意味着没有顶点。
Recursively, you mark each node when you cross it and when there is nothing left to explore you go back.
Something that will look like
C example, if the graph is represented by a adjacent matrix, a length of 1000 means there are no vertices.
这不是DFS搜索吗? (深度优先搜索)
Isn't that a DFS search? (Depth First Search)