简体   繁体   English

图的任意两个顶点之间存在循环

[英]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.

相关问题 查找在无向图中包含两个顶点的循环 - Find cycle which contains two vertices in undirected graph 如何检查两个顶点之间的图形连通性 - How to check graph connectivity between two vertices 将边缘添加到树形图中,以便使任意两个顶点之间的新最大距离最小 - Adding an edge to a tree graph so as the new maximum distance between any two vertices is minimum possible 如何找到树的任意两个顶点之间的边或顶点数量? - How to find the number of edges or vertices between any two vertices of a tree? 在两个顶点之间的无向图中找到特定边 - Finding a specific edge in an undirected graph between two vertices 查找树中两个顶点之间的简单路径(无向简单图) - Finding simple path between two vertices in a tree (undirected simple graph) 如何打印图中两个顶点之间的最短路径? - How can I print the shortest path between two vertices in a graph? 寻找两个任意顶点之间所有连接的图算法 - Graph Algorithm To Find All Connections Between Two Arbitrary Vertices 有向非循环图中两个顶点之间的最大加权路径 - Maximum weighted path between two vertices in a directed acyclic Graph 检查有向无环图中两个顶点之间是否存在路径 - 查询 - Check if there exist a path between two vertices in directed acyclic graph - queries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM