使用深度优先搜索检测图中的循环:2种不同的方法及其区别

56

注意,图形表示为邻接列表。

我听说过有两种方法可以在图中找到循环:

  1. 保留一个布尔值数组来跟踪您以前是否访问过节点。 如果您用尽了要去的新节点(没有命中已经访问过的节点),则回溯并尝试不同的分支。

  2. 来自Cormen的CLRS或Skiena的方法:对于未定向图中的深度优先搜索,有两种类型的边缘,树形和反向。当且仅当存在反向边时,图形才具有循环。

有人能够解释一下图的反向边是什么,以及上述两种方法之间的区别吗?谢谢。

更新:下面是检测这两种情况下循环的代码。 Graph是一个简单的类,用于将所有图形节点表示为唯一数字以简化,每个节点都有其相邻的邻居节点(g.getAdjacentNodes(int)):

public class Graph {

  private int[][] nodes; // all nodes; e.g. int[][] nodes = {{1,2,3}, {3,2,1,5,6}...};

  public int[] getAdjacentNodes(int v) {
    return nodes[v];
  }

  // number of vertices in a graph
  public int vSize() {
    return nodes.length;
  }

}

用 Java 编写来检测无向图中循环的代码:

    public class DFSCycle {

    private boolean marked[];
    private int s;
    private Graph g;
    private boolean hasCycle;

    // s - starting node
    public DFSCycle(Graph g, int s) {
        this.g = g;
        this.s = s;
        marked = new boolean[g.vSize()];
        findCycle(g,s,s);
    }

    public boolean hasCycle() {
        return hasCycle;
    }

    public void findCycle(Graph g, int v, int u) {

        marked[v] = true;

        for (int w : g.getAdjacentNodes(v)) {
            if(!marked[w]) {
                marked[w] = true;
                findCycle(g,w,v);
            } else if (v != u) {
                hasCycle = true;
                return;
            }
        }

    }  
}

检测有向图中循环的Java代码:

public class DFSDirectedCycle {

    private boolean marked[];
    private boolean onStack[];
    private int s;
    private Graph g;
    private boolean hasCycle;

    public DFSDirectedCycle(Graph g, int s) {
        this.s = s
        this.g = g;
        marked = new boolean[g.vSize()];
        onStack = new boolean[g.vSize()];
        findCycle(g,s);
    }

    public boolean hasCycle() {
        return hasCycle;
    }

    public void findCycle(Graph g, int v) {

        marked[v] = true;
        onStack[v] = true;

        for (int w : g.adjacentNodes(v)) {
            if(!marked[w]) {
                findCycle(g,w);
            } else if (onStack[w]) {
                hasCycle = true;
                return;
            }
        }

        onStack[v] = false;
    }
}

我认为有一个有序图代码中的错误。在遍历形状为O的图时,根位于顶部并且所有边缘都指向底部,此算法将检测到循环(首先将遍历O的左侧到底部并标记所有节点为已标记,然后是O的右侧直到我到达底部,这已经被标记)。我会在findCycle(g,w)之后添加marked[v]= false; 你觉得呢? - Matej Briškár
DFSCycle 中检查循环的条件是不正确的。应该是 if(i != u) // 如果相邻节点被访问且不是当前顶点的父节点,则存在一个循环。 { hasCycle = true; return; } - akhil_mittal
2
@IvanVoroshilin,} else if (v != u) { 应该改为 } else if (w != u) {,对吗? - Etherealone
5个回答

74

回答我的问题:

当且仅当存在一条反向边时,图形才具有循环。 反向边是指从节点到自身(自环)或到DFS生成的树中其祖先之一的边,形成一个循环。

上述两种方法实际上意思相同。但是,此方法只能应用于无向图

该算法不适用于有向图的原因是,在有向图中,到同一顶点的2条不同路径并不构成循环。例如:A-->B,B-->C,A-->C - 不构成循环,而在无向图中:A--B,B--C,C--A 则构成循环。

查找无向图中的循环

当深度优先搜索(DFS)找到指向已访问过的节点(即反向边)的边时,无向图具有循环。

查找有向图中的循环

除了已访问过的顶点外,我们还需要跟踪当前在函数DFS遍历的递归堆栈中的顶点。如果我们到达已在递归堆栈中的顶点,则树中存在循环。

更新: 工作代码在上面的问题部分中。


14
在无向图中,仅当存在一条反向边(不包括从反向边所在的边的父节点)时才存在一个循环。否则,如果我们在查找反向边时不排除父边,则每个无向图默认都有一个循环。 - crackerplace
DFS方法用于在有向图中查找循环,因此可以在线性时间内运行。Python3实现可返回第一个作为子图的循环,带有测试。基于10分钟的视频解释 - ruhong
如果我们到达一个已经在递归堆栈中的顶点,我对在有向图中查找循环的这部分感到困惑。你的第一行说除了访问过的顶点,我们还必须考虑递归堆栈。但接下来的一行只是关于递归堆栈的。你能澄清这个困惑吗? - avijit

11

为了完整起见,可以使用深度优先搜索(来自维基百科)在有向图中找到循环:

为了完整性,可以使用DFS在有向图中找到循环。(来源:维基百科)
 L ← Empty list that will contain the sorted nodes
while there are unmarked nodes do
    select an unmarked node n
    visit(n) 
function visit(node n)
    if n has a temporary mark then stop (not a DAG)
    if n is not marked (i.e. has not been visited yet) then
        mark n temporarily
        for each node m with an edge from n to m do
            visit(m)
        mark n permanently
        unmark n temporarily
        add n to head of L

3

我认为上面的代码仅适用于有向连通图,因为我们仅从源节点开始进行深度优先搜索,如果图不连通,则另一个部分中可能会存在未被发现的环!


我不知道为什么这个评论会被踩,但他确实提出了一个重要的观点:如果一个有向图不是强连通的,即存在一些顶点无法从其他顶点到达,则使用DFS搜索可能会失败,因为该图可能被分成多个强连通分量。请参考https://dev59.com/kY_ea4cB1Zd3GeqPRa1-#32893154以查找有向图中的循环。 - Ray Jasson

1

针对这个问题的一个子集,提供一些观察:如果 num_edges >= num_nodes,那么连通的无向图将会有一个环。

更新:还提供使用DFS检测无向图中循环的Python代码。非常希望能够帮助优化此代码。

def detect_cycle(graph, root):
    # Create an empty stack
    stack = []
    
    # Track visited nodes
    visited = [False] * graph.num_nodes

    # Track the order of traversal
    traversal = []

    # Track parents of traversed nodes
    parent = [None] * graph.num_nodes
    
    # Begin Traversal
    stack.append(root)
    parent[root] = root

    while len(stack) > 0:
        # Pop the stack
        node = stack.pop()
        if not visited[node]:
            visited[node] = True
            traversal.append(node)
            
            # Check the neighbors for visited nodes, or continue traversal
            for neighbor in graph.edges[node]:
                if not visited[neighbor]:
                    stack.append(neighbor)
                    parent[neighbor] = node
                
                # If the neighbor is visited, check for back edge 
                # If a back edge exists, neighbor will have been visited before 
                # the parent of the current node
                elif traversal.index(neighbor) < traversal.index(parent[node]):
                    return (f'Cycle at node {node}!')
                                    
    return ('Acyclic graph!')

1

这是我基于深度优先搜索编写的C代码,用于判断给定的无向图是否连通/有环。最后还附有一些样例输出。希望对你有所帮助 :)

#include<stdio.h>
#include<stdlib.h>

/****Global Variables****/
int A[20][20],visited[20],count=0,n;
int seq[20],connected=1,acyclic=1;

/****DFS Function Declaration****/
void DFS();

/****DFSearch Function Declaration****/
void DFSearch(int cur);

/****Main Function****/
int main() 
   {    
    int i,j;

    printf("\nEnter no of Vertices: ");
    scanf("%d",&n);

    printf("\nEnter the Adjacency Matrix(1/0):\n");
    for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
        scanf("%d",&A[i][j]);

    printf("\nThe Depth First Search Traversal:\n");

    DFS();

    for(i=1;i<=n;i++)
        printf("%c,%d\t",'a'+seq[i]-1,i);

    if(connected && acyclic)    printf("\n\nIt is a Connected, Acyclic Graph!");
    if(!connected && acyclic)   printf("\n\nIt is a Not-Connected, Acyclic Graph!");
    if(connected && !acyclic)   printf("\n\nGraph is a Connected, Cyclic Graph!");
    if(!connected && !acyclic)  printf("\n\nIt is a Not-Connected, Cyclic Graph!");

    printf("\n\n");
    return 0;
   }

/****DFS Function Definition****/
void DFS()
    { 
    int i;
    for(i=1;i<=n;i++)
        if(!visited[i])
          {
        if(i>1) connected=0;
        DFSearch(i);    
              } 
    }

/****DFSearch Function Definition****/
void DFSearch(int cur) 
    {
    int i,j;
    visited[cur]=++count;

        seq[count]=cur; 
        for(i=1;i<count-1;i++)
                if(A[cur][seq[i]]) 
                   acyclic=0;

    for(i=1;i<=n;i++)
        if(A[cur][i] && !visited[i])
           DFSearch(i);

    }

样例输出:

majid@majid-K53SC:~/Desktop$ gcc BFS.c

majid@majid-K53SC:~/Desktop$ ./a.out
************************************

Enter no of Vertices: 10

Enter the Adjacency Matrix(1/0):

0 0 1 1 1 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 
0 0 0 1 0 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 1 0 0 1 0 0 0 0 0 
0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 0 1 
0 0 0 0 0 0 1 0 0 0 

The Depdth First Search Traversal:
a,1 c,2 d,3 f,4 b,5 e,6 g,7 h,8 i,9 j,10    

It is a Not-Connected, Cyclic Graph!


majid@majid-K53SC:~/Desktop$ ./a.out
************************************

Enter no of Vertices: 4

Enter the Adjacency Matrix(1/0):
0 0 1 1
0 0 1 0
1 1 0 0
0 0 0 1

The Depth First Search Traversal:
a,1 c,2 b,3 d,4 

It is a Connected, Acyclic Graph!


majid@majid-K53SC:~/Desktop$ ./a.out
************************************

Enter no of Vertices: 5

Enter the Adjacency Matrix(1/0):
0 0 0 1 0
0 0 0 1 0
0 0 0 0 1
1 1 0 0 0 
0 0 1 0 0

The Depth First Search Traversal:
a,1 d,2 b,3 c,4 e,5 

It is a Not-Connected, Acyclic Graph!

*/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接