[英]Existence of cycle between any two vertices of graph
查找有向图的两个特定顶点之间的路径数的问题,并且如果它们之间存在循环,则路径数是无限的,因此我知道在整个图中查找循环的算法,但找不到任何两个特定的顶点,所以如果有人解释它对我会有所帮助。
So, we can divide this problem into two sub parts. 因此,我们可以将此问题分为两个子部分。
If there is a cycle between U (source) and V (destination) then the answer will be infinite. 如果在U (源)和V (目标)之间存在一个循环,则答案将是无限的。 So in one DFS we will start from U until we get V . 因此,在一个DFS中,我们将从U开始直到获得V。 Similarly starting from V until we get U. If we get reach both from both the ways then the desired answer is infinite. 类似地,从V开始直到获得U。如果我们通过两种方式都达到,则所需答案是无限的。
Now the main part. 现在是主要部分。 Run a normal DFS from source U and start visiting every node as true, if you encounter the destination node that is V don't mark it as true and then simply continue from there. 从源U运行正常的DFS,然后开始访问每个节点为true,如果遇到目标节点V,则不要将其标记为true,然后直接从那里继续。 (Cycles in between this process can easily be detected). (此过程之间的周期很容易检测到)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.