Graph 一般图形周期检测(请确认我的答案)
问题是: 如果一个无向图恰好包含一个圈,则它是单圈的。描述一个O | V |+| E |算法,用于确定给定图G是否为单圈图 我的解决方案: int i=0Graph 一般图形周期检测(请确认我的答案),graph,cycle,depth-first-search,Graph,Cycle,Depth First Search,问题是: 如果一个无向图恰好包含一个圈,则它是单圈的。描述一个O | V |+| E |算法,用于确定给定图G是否为单圈图 我的解决方案: int i=0 在G上运行修改后的DFS,每次我们决定不访问顶点时,都会增加i,因为它已经被访问过 完成DFS后,若i==1,则该图为单圈图 我以为这个解决方案会奏效,但我想知道是否有一个反例可以证明它是错误的。如果有人能澄清,那就太好了,谢谢 "Increment i everytime we decide not to visit a vertex be
在G上运行修改后的DFS,每次我们决定不访问顶点时,都会增加i,因为它已经被访问过 完成DFS后,若i==1,则该图为单圈图 我以为这个解决方案会奏效,但我想知道是否有一个反例可以证明它是错误的。如果有人能澄清,那就太好了,谢谢
"Increment i everytime we decide not to visit a vertex because
it has already been visited."
我很不清楚你在这里想干什么
与其这样,不如这样:
执行DFS并计算后边缘的数量
A back edge is an edge that is from a node to itself (selfloop) or one of its
ancestor in the tree produced by DFS.
如果后边缘数==1,则unicycle else不是
To count number of back edges, follow this algorithm.
To detect a back edge, we can keep track of vertices currently in recursion stack of
function for DFS traversal. If we reach a vertex that is already in the recursion stack,
then there is a cycle in the tree. The edge that connects current vertex to the
vertex in the recursion stack is back edge
图形是否由单个连接的组件组成 在这种情况下,只需计算顶点和边,并检查| V |-| E |=0 否则,计算连接部件的数量O | V |+| E |, 并检查| V |-| E |=连接组件的数量-1
备注:连接多个组件是算法的反例 每次我们决定不访问顶点时,都会增加i,因为它已经被访问过了。这一行是什么意思?我的意思是,如果顶点v未被探测,我将修改DFS代码的部分,然后将e标记为发现的边,递归地调用DFSG,否则I++增量I,因为我们决定不访问一个已经访问过的聪明节点,但它不会被检查| v |-| e |=0,因为包含2个顶点和1条边的图不是循环图,2-1=1。另外,如果我在每个组件中重新启动修改后的DFS,我的算法会工作吗?@user2931097谢谢你的评论,我刚刚编辑过