Why depth-first search can determine whether there is a cycle in a simple graph, but breadth-first search cannot?

I. Introduction

This article is an original work, there may be some deficiencies, please correct me, communicate politely, thank you very much.

Second, prerequisite knowledge

1. Simple diagram

A simple graph is a graph that satisfies the following conditions: there are no edges connecting vertices to itself, and no parallel edges exist.

2, ring

Here we are talking about simple loops (rings), that is, loops in which vertices do not repeat except for the first and last vertices.

3. The definition of a tree (single-pointed undirected tree) is an undirected graph that is connected and has no loops.

agreement:

1. The word "broad search" below means breadth-first search, and deep search means depth-first search.

2. The following graphs refer to simple connected graphs

Three, the text

1. First of all, it is certain that for undirected graphs, both wide search and deep search can determine whether there is a cycle.

Briefly explain, if an undirected graph has a cycle, then in the process of wide search, the nodes that have been visited can be searched. If an undirected graph has no cycles (refer to undirected tree), then its breadth-first search process will not visit the visited nodes.

For Deep Search, if a simple undirected graph has a cycle, then the path formed by the nodes saved in the Deep Search stack (hereinafter referred to as the Deep Search node path) will have back edges (edges pointing to the nodes in the stack). But without the ring, this would not be the case.

2. For directed graphs, only deep search can determine whether there is a cycle among them.

First of all, for directed graphs, whether there is a cycle or not, it is possible for Kuansearch to find the nodes that have been visited. As shown below:

When nodes B and C have entered the queue of wide search, they will visit node D successively, and at this time, the situation of visiting nodes that have been visited appears. But at this time the graph has no cycles. 

Therefore, the previous method cannot be judged.

But deep search is still possible. If there is a cycle in the simple directed graph, there will still be back edges in the deep search node path.

Four, write at the end

If there is any mistake, please correct me, communicate politely, thank you very much

In the appendix, some friends mentioned that Shensou judges whether an undirected graph has a cycle. I wrote a pseudocode. The logic is not necessarily strict, but it should be correct in general. If there is any error, please correct me.

#include<iostream>
#define N 5
using namespace std;
bool DFS(int node, int tag[],int graph[][5], int parent[])//通过深搜判断无向图是否有环
{//node代表当前结点,tag[i]等于0代表该结点没有在深搜的栈中,tag[i]等于1代表当前结点在深搜的栈中。
//graph数组代表图的邻接矩阵,N代表结点个数,
//parent[i]等于j,代表当前深搜的栈中结点i的前驱是j ,也即访问i之后就立马访问j了 
//该函数返回false代表有环,否则代表无环 
	if(tag[node] == 1)return false;//遇到回边就返回false 
	else tag[node] = 1;
	bool res = true;
	for(int i=0;i<N;i++)
	{
		if(graph[node][i] > 0 && parent[node] != i)//如果结点node和i之间存在一条边,当前结点的父节点不应该访问 
		{
			parent[i] = node;
			res = DFS( i,  tag, graph, parent);
			if(res == false)break;//只要有一条回边,就代表整个图有环 
			parent[i] = -1;
			tag[i] = 0; 
		} 
	}
	return res;
} 
int main()
{
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/fly_view/article/details/127429059