首页 文章

一般图形周期检测(请确认我的答案)[关闭]

提问于
浏览
1

问题:

如果无向图只包含一个周期,则它是单向的 . 描述用于确定给定图G是否是单圈的O(| V | | E |)算法 .

我的解决方案

int i = 0

在G上运行修改后的DFS,每当我们决定不访问顶点时我们就会增加i,因为它已经被访问过了 .

DFS完成后,如果i == 1,则图形是单圈形的 .

我认为这个解决方案可行,但我想知道是否有一个反例可以证明它是错误的 . 如果有人能澄清那将是伟大的,谢谢 .

2 回答

  • 0

    您的图表是否由单个连接组件组成?

    在这种情况下,只需计算顶点和边并检查| V | - | E | = 0

    否则计算连接组件的数量O(| V | | E |),并检查| V | - | E | =连接组件的数量 - 1 .

    备注:拥有多个连接组件是算法的反例 .

  • 0
    "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,那么其他的不是独轮车 .

    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
    

相关问题