无向图:
法1:
- 如果存在回路,则必存在一个子图,是一个环路。环路中所有顶点的度>=2。
- n算法:
- 第一步:删除所有度<=1的顶点及相关的边,并将另外与这些边相关的其它顶点的度减一。
- 第二步:将度数变为1的顶点排入队列,并从该队列中取出一个顶点重复步骤一。
- 如果最后还有未删除顶点,则存在环,否则没有环。
- n算法分析:
- 由于有m条边,n个顶点。如果m>=n,则根据图论知识可直接判断存在环路。
- (证明:如果没有环路,则该图必然是k棵树 k>=1。根据树的性质,边的数目m = n-k。k>=1,所以:m<n)
- 如果m<n 则按照上面的算法每删除一个度为0的顶点操作一次(最多n次),或每删除一个度为1的顶点(同时删一条边)操作一次(最多m次)。这两种操作的总数不会超过m+n。由于m<n,所以算法复杂度为O(n)
另:
该方法,算法复杂度不止O(V),首先初始时刻统计所有顶点的度的时候,复杂度为(V + E),即使在后来的循环中E>=V,这样算法的复杂度也只能为O(V + E)。其次,在每次循环时,删除度为1的顶点,那么就必须将与这个顶点相连的点的度减一,并且执行delete node from list[list[node]],这里查找的复杂度为list[list[node]]的长度,只有这样才能保证当degree[i]=1时,list[i]里面只有一个点。这样最差的复杂度就为O(EV)了。
法2:
DFS搜索图,图中的边只可能是树边或反向边,一旦发现反向边,则表明存在环。该算法的复杂度为O(V)。
有向图:
主要有深度优先和拓扑排序2中方法
1、拓扑排序,如果能够用拓扑排序完成对图中所有节点的排序的话,就说明这个图中没有环,而如果不能完成,则说明有环。
2、可以用Strongly Connected Components来做,我们可以回忆一下强连通子图的概念,就是说对于一个图的某个子图,该子图中的任意u->v,必有v->u,则这是一个强连通子图。这个限定正好是环的概念。所以我想,通过寻找图的强连通子图的方法应该可以找出一个图中到底有没有环、有几个环。
3、就是用一个改进的DFS
刚看到这个问题的时候,我想单纯用DFS就可以解决问题了。但细想一下,是不能够的。如果题目给出的是一个无向图,那么OK,DFS是可以解决的。但无向图得不出正确结果的。比如:A->B,A->C->B,我们用DFS来处理这个图,我们会得出它有环,但其实没有。
我们可以对DFS稍加变化,来解决这个问题。解决的方法如下:
图中的一个节点,根据其C[N]的值,有三种状态:
0,此节点没有被访问过
-1,被访问过至少1次,其后代节点正在被访问中
1,其后代节点都被访问过。
按照这样的假设,当按照DFS进行搜索时,碰到一个节点时有三种可能:
1、如果C[V]=0,这是一个新的节点,不做处理
2、如果C[V]=-1,说明是在访问该节点的后代的过程中访问到该节点本身,则图中有环。
3、如果C[V]=1,类似于2的推导,没有环。 在程序中加上一些特殊的处理,即可以找出图中有几个环,并记录每个环的路径
判断有向图是否有环有三种方法:拓扑排序、深度遍历+回溯、深度遍历 + 判断后退边
这里使用 拓扑排序 和 深度遍历 + 回溯判断是不是环。使用 深度遍历 + 判断后退边找出环个数 以及环中元素
1、拓扑排序
思想:找入度为0的顶点,输出顶点,删除出边。循环到无顶点输出。
若:输出所有顶点,则课拓扑排序,无环;反之,则不能拓扑排序,有环
使用:可以使用拓扑排序为有向无环图每一个结点进行编号,拓扑排序输出的顺序可以为编号顺序
源代码:
- #include <iostream>
- using namespace std;
- const int MAX_Vertex_Num = 20;
- template<class VexType,class ArcType>
- class MGraph
- {
- public:
- void CreateGraph();//创建图
- int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
- void CheckCircle();
- private:
- VexType vexs[MAX_Vertex_Num];//顶点向量
- ArcType arcs[MAX_Vertex_Num][MAX_Vertex_Num]; //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
- int vexnum;//顶点数
- int arcnum;//边数
- private:
- bool TopSort();
- };
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::CreateGraph()
- {
- VexType first;
- VexType Secend;
- cout<<"请输入顶点数:";
- cin>>vexnum;
- cout<<"请输入边数:";
- cin>>arcnum;
- cout<<"请输入各个顶点值:";
- for (int i=0;i<vexnum;i++)
- {
- cin>>vexs[i];
- }
- //初始化邻接矩阵
- for (int i=0;i<arcnum;i++)
- {
- for (int j=0;j<arcnum;j++)
- {
- arcs[i][j]=0;
- }
- }
- cout<<"请输入边的信息:"<<endl;
- for (int i=0;i<arcnum;i++)
- {
- cin>>first>>Secend;
- //如果边有权值的话,则还应该输入权值
- int x = LocateVex(first);
- int y = LocateVex(Secend);
- arcs[x][y]=1;//如果是有权的话,这里应该是arc[x][y]=权值
- }
- }
- /*
- 参数:v:表示顶点向量中一个值
- 函数返回值:函数返回v在顶点向量中的下标
- */
- template<class VexType,class ArcType>
- int MGraph<VexType,ArcType>::LocateVex(VexType v)
- {
- for (int i=0;i<vexnum;i++)
- {
- if (vexs[i]==v)
- {
- return i;
- }
- }
- return -1;
- }
- /*
- 有向图可以拓扑排序的条件是:图中没有环。
- 具体方法:
- ⑴ 从图中选择一个入度为0的点加入拓扑序列。
- ⑵ 从图中删除该结点以及它的所有出边(即与之相邻点入度减1)。
- */
- template<class VexType,class ArcType>
- bool MGraph<VexType,ArcType>::TopSort()
- {
- int count = 0;//拓扑排序输出顶点的个数
- int top = -1;
- int stack[MAX_Vertex_Num];
- int indegree[MAX_Vertex_Num]={0};
- //求各个顶点的入度--邻接矩阵要查询该元素的列(记录入度情况)--
- //如果是邻接表,就是麻烦在这里,查询结点入度很不方便
- for (int i=0;i<vexnum;i++)
- {
- int num=0;
- for (int j=0;j<vexnum;j++)
- {
- if (arcs[j][i]!=0)
- {
- num++;
- }
- }
- indegree[i]=num;
- }
- //把入度为0的顶点入栈
- for (int i=0;i<vexnum;i++)
- {
- if (!indegree[i])
- {
- stack[++top]=i;//顶点的下标
- }
- }
- //处理入度为0的结点:把入度为0的结点出栈,删除与之有关的边
- while (top>-1)
- {
- int x = stack[top--];
- cout<<vexs[x];
- count++;
- //把与下标为x的顶点有关的边都去掉(出边),并改变对应结点的入度
- for (int i=0;i<vexnum;i++)
- {
- if (arcs[x][i]!=0)
- {
- arcs[x][i]=0;//删除到下标为i的顶点的边,这时此顶点的入度减一
- indegree[i]--;
- if (!indegree[i])//顶点的入度为0,则入栈
- {
- stack[++top]=i;
- }
- }
- }
- }
- cout<<endl;
- if (count == vexnum) //能拓扑排序
- {
- return true;
- }
- return false;
- }
- /*
- 检查图中是不是有环
- 思想:
- 能进行拓扑排序,则无环,反之有环
- */
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::CheckCircle()
- {
- if (TopSort())
- {
- cout<<"无环!"<<endl;
- }
- else
- {
- cout<<"有环!"<<endl;
- }
- }
- int main()
- {
- MGraph<char,int> G;
- G.CreateGraph();
- G.CheckCircle();
- system("pause");
- return 1;
- }
#include <iostream> using namespace std; const int MAX_Vertex_Num = 20; template<class VexType,class ArcType> class MGraph { public: void CreateGraph();//创建图 int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标) void CheckCircle(); private: VexType vexs[MAX_Vertex_Num];//顶点向量 ArcType arcs[MAX_Vertex_Num][MAX_Vertex_Num]; //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数 int vexnum;//顶点数 int arcnum;//边数 private: bool TopSort(); }; template<class VexType,class ArcType> void MGraph<VexType,ArcType>::CreateGraph() { VexType first; VexType Secend; cout<<"请输入顶点数:"; cin>>vexnum; cout<<"请输入边数:"; cin>>arcnum; cout<<"请输入各个顶点值:"; for (int i=0;i<vexnum;i++) { cin>>vexs[i]; } //初始化邻接矩阵 for (int i=0;i<arcnum;i++) { for (int j=0;j<arcnum;j++) { arcs[i][j]=0; } } cout<<"请输入边的信息:"<<endl; for (int i=0;i<arcnum;i++) { cin>>first>>Secend; //如果边有权值的话,则还应该输入权值 int x = LocateVex(first); int y = LocateVex(Secend); arcs[x][y]=1;//如果是有权的话,这里应该是arc[x][y]=权值 } } /* 参数:v:表示顶点向量中一个值 函数返回值:函数返回v在顶点向量中的下标 */ template<class VexType,class ArcType> int MGraph<VexType,ArcType>::LocateVex(VexType v) { for (int i=0;i<vexnum;i++) { if (vexs[i]==v) { return i; } } return -1; } /* 有向图可以拓扑排序的条件是:图中没有环。 具体方法: ⑴ 从图中选择一个入度为0的点加入拓扑序列。 ⑵ 从图中删除该结点以及它的所有出边(即与之相邻点入度减1)。 */ template<class VexType,class ArcType> bool MGraph<VexType,ArcType>::TopSort() { int count = 0;//拓扑排序输出顶点的个数 int top = -1; int stack[MAX_Vertex_Num]; int indegree[MAX_Vertex_Num]={0}; //求各个顶点的入度--邻接矩阵要查询该元素的列(记录入度情况)-- //如果是邻接表,就是麻烦在这里,查询结点入度很不方便 for (int i=0;i<vexnum;i++) { int num=0; for (int j=0;j<vexnum;j++) { if (arcs[j][i]!=0) { num++; } } indegree[i]=num; } //把入度为0的顶点入栈 for (int i=0;i<vexnum;i++) { if (!indegree[i]) { stack[++top]=i;//顶点的下标 } } //处理入度为0的结点:把入度为0的结点出栈,删除与之有关的边 while (top>-1) { int x = stack[top--]; cout<<vexs[x]; count++; //把与下标为x的顶点有关的边都去掉(出边),并改变对应结点的入度 for (int i=0;i<vexnum;i++) { if (arcs[x][i]!=0) { arcs[x][i]=0;//删除到下标为i的顶点的边,这时此顶点的入度减一 indegree[i]--; if (!indegree[i])//顶点的入度为0,则入栈 { stack[++top]=i; } } } } cout<<endl; if (count == vexnum) //能拓扑排序 { return true; } return false; } /* 检查图中是不是有环 思想: 能进行拓扑排序,则无环,反之有环 */ template<class VexType,class ArcType> void MGraph<VexType,ArcType>::CheckCircle() { if (TopSort()) { cout<<"无环!"<<endl; } else { cout<<"有环!"<<endl; } } int main() { MGraph<char,int> G; G.CreateGraph(); G.CheckCircle(); system("pause"); return 1; }
测试:
有向图:
结果:
2、深度遍历 + 回溯
思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。
代码:
- #include <iostream>
- using namespace std;
- const int MAX_Vertex_Num = 20;
- template<class VexType,class ArcType>
- class MGraph
- {
- public:
- void CreateGraph();//创建图
- int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
- bool CheckCircle();//检查图中有无环
- private:
- VexType vexs[MAX_Vertex_Num];//顶点向量
- ArcType arcs[MAX_Vertex_Num][MAX_Vertex_Num]; //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
- int vexnum;//顶点数
- int arcnum;//边数
- private:
- void CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool Isvisited[MAX_Vertex_Num]);
- };
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::CreateGraph()
- {
- VexType first;
- VexType Secend;
- cout<<"请输入顶点数:";
- cin>>vexnum;
- cout<<"请输入边数:";
- cin>>arcnum;
- cout<<"请输入各个顶点值:";
- for (int i=0;i<vexnum;i++)
- {
- cin>>vexs[i];
- }
- //初始化邻接矩阵
- for (int i=0;i<arcnum;i++)
- {
- for (int j=0;j<arcnum;j++)
- {
- arcs[i][j]=0;
- }
- }
- cout<<"请输入边的信息:"<<endl;
- for (int i=0;i<arcnum;i++)
- {
- cin>>first>>Secend;
- //如果边有权值的话,则还应该输入权值
- int x = LocateVex(first);
- int y = LocateVex(Secend);
- arcs[x][y]=1;//如果是有权的话,这里应该是arc[x][y]=权值
- }
- }
- /*
- 参数:v:表示顶点向量中一个值
- 函数返回值:函数返回v在顶点向量中的下标
- */
- template<class VexType,class ArcType>
- int MGraph<VexType,ArcType>::LocateVex(VexType v)
- {
- for (int i=0;i<vexnum;i++)
- {
- if (vexs[i]==v)
- {
- return i;
- }
- }
- return -1;
- }
- /*
- 思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。
- */
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool Isvisited[MAX_Vertex_Num])
- {
- visited[u]=true;
- Isvisited[u]=true;
- for (int j=0;j<vexnum;j++)
- {
- if (arcs[u][j]==1)
- {
- if (visited[j]==false)
- {
- CheckCircle(j,isExist,visited,Isvisited);
- }
- else
- {
- isExist = true;
- }
- }
- }
- visited[u]=false;//回溯,如果不写就变成一半的深度遍历,不能进行判断是否有边存在
- }
- template<class VexType,class ArcType>
- bool MGraph<VexType,ArcType>::CheckCircle()
- {
- bool isExist = false;
- bool Isvisited[MAX_Vertex_Num]={false};
- bool visited[MAX_Vertex_Num]={false};
- for (int i=0;i<vexnum;i++)
- {
- if (Isvisited[i]==false)
- {
- CheckCircle(i,isExist,visited,Isvisited);
- if (isExist)
- {
- return true;
- }
- }
- }
- return isExist;
- }
- int main()
- {
- MGraph<char,int> G;
- G.CreateGraph();
- if (G.CheckCircle())
- {
- cout<<"图存在环!"<<endl;
- }
- else
- {
- cout<<"图不存在环!"<<endl;
- }
- system("pause");
- return 1;
- }
#include <iostream> using namespace std; const int MAX_Vertex_Num = 20; template<class VexType,class ArcType> class MGraph { public: void CreateGraph();//创建图 int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标) bool CheckCircle();//检查图中有无环 private: VexType vexs[MAX_Vertex_Num];//顶点向量 ArcType arcs[MAX_Vertex_Num][MAX_Vertex_Num]; //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数 int vexnum;//顶点数 int arcnum;//边数 private: void CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool Isvisited[MAX_Vertex_Num]); }; template<class VexType,class ArcType> void MGraph<VexType,ArcType>::CreateGraph() { VexType first; VexType Secend; cout<<"请输入顶点数:"; cin>>vexnum; cout<<"请输入边数:"; cin>>arcnum; cout<<"请输入各个顶点值:"; for (int i=0;i<vexnum;i++) { cin>>vexs[i]; } //初始化邻接矩阵 for (int i=0;i<arcnum;i++) { for (int j=0;j<arcnum;j++) { arcs[i][j]=0; } } cout<<"请输入边的信息:"<<endl; for (int i=0;i<arcnum;i++) { cin>>first>>Secend; //如果边有权值的话,则还应该输入权值 int x = LocateVex(first); int y = LocateVex(Secend); arcs[x][y]=1;//如果是有权的话,这里应该是arc[x][y]=权值 } } /* 参数:v:表示顶点向量中一个值 函数返回值:函数返回v在顶点向量中的下标 */ template<class VexType,class ArcType> int MGraph<VexType,ArcType>::LocateVex(VexType v) { for (int i=0;i<vexnum;i++) { if (vexs[i]==v) { return i; } } return -1; } /* 思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。 */ template<class VexType,class ArcType> void MGraph<VexType,ArcType>::CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool Isvisited[MAX_Vertex_Num]) { visited[u]=true; Isvisited[u]=true; for (int j=0;j<vexnum;j++) { if (arcs[u][j]==1) { if (visited[j]==false) { CheckCircle(j,isExist,visited,Isvisited); } else { isExist = true; } } } visited[u]=false;//回溯,如果不写就变成一半的深度遍历,不能进行判断是否有边存在 } template<class VexType,class ArcType> bool MGraph<VexType,ArcType>::CheckCircle() { bool isExist = false; bool Isvisited[MAX_Vertex_Num]={false}; bool visited[MAX_Vertex_Num]={false}; for (int i=0;i<vexnum;i++) { if (Isvisited[i]==false) { CheckCircle(i,isExist,visited,Isvisited); if (isExist) { return true; } } } return isExist; } int main() { MGraph<char,int> G; G.CreateGraph(); if (G.CheckCircle()) { cout<<"图存在环!"<<endl; } else { cout<<"图不存在环!"<<endl; } system("pause"); return 1; }
结果测试:
图:
结果:
3、深度遍历 + 判断后退边
思想:用DFS(深度优先遍历),判断是否有后退边,若有,则存在环
具体来说,在遍历顶点的每一条边时,判断一下这个边的顶点是不是在栈中,如果在栈中,说明之前已经访问过了,这里再次访问,说明有环存在
判断后退边时,借助一个栈和一个数组
栈:即可以用来输出环
数组:inStack判断是否在栈中
源代码:
- #include <iostream>
- using namespace std;
- const int MAX_Vertex_Num = 20;
- template<class VexType,class ArcType>
- class MGraph
- {
- public:
- void CreateGraph();//创建图
- int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
- void CheckCircle();
- private:
- VexType vexs[MAX_Vertex_Num];//顶点向量
- ArcType arcs[MAX_Vertex_Num][MAX_Vertex_Num]; //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
- int vexnum;//顶点数
- int arcnum;//边数
- private:
- void DFS(int x,bool visited[MAX_Vertex_Num],int stack[MAX_Vertex_Num],int& top,bool inStack[MAX_Vertex_Num],int& count);
- };
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::CreateGraph()
- {
- VexType first;
- VexType Secend;
- cout<<"请输入顶点数:";
- cin>>vexnum;
- cout<<"请输入边数:";
- cin>>arcnum;
- cout<<"请输入各个顶点值:";
- for (int i=0;i<vexnum;i++)
- {
- cin>>vexs[i];
- }
- //初始化邻接矩阵
- for (int i=0;i<arcnum;i++)
- {
- for (int j=0;j<arcnum;j++)
- {
- arcs[i][j]=0;
- }
- }
- cout<<"请输入边的信息:"<<endl;
- for (int i=0;i<arcnum;i++)
- {
- cin>>first>>Secend;
- //如果边有权值的话,则还应该输入权值
- int x = LocateVex(first);
- int y = LocateVex(Secend);
- arcs[x][y]=1;//如果是有权的话,这里应该是arc[x][y]=权值
- }
- }
- /*
- 参数:v:表示顶点向量中一个值
- 函数返回值:函数返回v在顶点向量中的下标
- */
- template<class VexType,class ArcType>
- int MGraph<VexType,ArcType>::LocateVex(VexType v)
- {
- for (int i=0;i<vexnum;i++)
- {
- if (vexs[i]==v)
- {
- return i;
- }
- }
- return -1;
- }
- /*
- 检查图中是不是有回向边
- 思想:
- 如果有回向边,则无环,反之有环
- */
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::CheckCircle()
- {
- int count=0;//环的个数
- int top=-1;
- int stack[MAX_Vertex_Num];
- bool inStack[MAX_Vertex_Num]={false};
- bool visited[MAX_Vertex_Num]={false};
- for (int i=0;i<vexnum;i++)
- {
- if (!visited[i])
- {
- DFS(i,visited,stack,top,inStack,count);
- }
- }
- }
- template<class VexType,class ArcType>
- void MGraph<VexType,ArcType>::DFS(int x,bool visited[MAX_Vertex_Num],int stack[MAX_Vertex_Num],int& top,bool inStack[MAX_Vertex_Num],int& count)
- {
- visited[x]=true;
- stack[++top]=x;
- inStack[x]=true;
- for (int i=0;i<vexnum;i++)
- {
- if (arcs[x][i]!=0)//有边
- {
- if (!inStack[i])
- {
- DFS(i,visited,stack,top,inStack,count);
- }
- else //条件成立,表示下标为x的顶点到 下标为i的顶点有环
- {
- count++;
- cout<<"第"<<count<<"环为:";
- //从i到x是一个环,top的位置是x,下标为i的顶点在栈中的位置要寻找一下
- //寻找起始顶点下标在栈中的位置
- int t=0;
- for (t=top;stack[t]!=i;t--);
- //输出环中顶点
- for (int j=t;j<=top;j++)
- {
- cout<<vexs[stack[j]];
- }
- cout<<endl;
- }
- }
- }
- //处理完结点后,退栈
- top--;
- inStack[x]=false;
- }
- int main()
- {
- MGraph<char,int> G;
- G.CreateGraph();
- G.CheckCircle();
- system("pause");
- return 1;
- }
#include <iostream> using namespace std; const int MAX_Vertex_Num = 20; template<class VexType,class ArcType> class MGraph { public: void CreateGraph();//创建图 int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标) void CheckCircle(); private: VexType vexs[MAX_Vertex_Num];//顶点向量 ArcType arcs[MAX_Vertex_Num][MAX_Vertex_Num]; //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数 int vexnum;//顶点数 int arcnum;//边数 private: void DFS(int x,bool visited[MAX_Vertex_Num],int stack[MAX_Vertex_Num],int& top,bool inStack[MAX_Vertex_Num],int& count); }; template<class VexType,class ArcType> void MGraph<VexType,ArcType>::CreateGraph() { VexType first; VexType Secend; cout<<"请输入顶点数:"; cin>>vexnum; cout<<"请输入边数:"; cin>>arcnum; cout<<"请输入各个顶点值:"; for (int i=0;i<vexnum;i++) { cin>>vexs[i]; } //初始化邻接矩阵 for (int i=0;i<arcnum;i++) { for (int j=0;j<arcnum;j++) { arcs[i][j]=0; } } cout<<"请输入边的信息:"<<endl; for (int i=0;i<arcnum;i++) { cin>>first>>Secend; //如果边有权值的话,则还应该输入权值 int x = LocateVex(first); int y = LocateVex(Secend); arcs[x][y]=1;//如果是有权的话,这里应该是arc[x][y]=权值 } } /* 参数:v:表示顶点向量中一个值 函数返回值:函数返回v在顶点向量中的下标 */ template<class VexType,class ArcType> int MGraph<VexType,ArcType>::LocateVex(VexType v) { for (int i=0;i<vexnum;i++) { if (vexs[i]==v) { return i; } } return -1; } /* 检查图中是不是有回向边 思想: 如果有回向边,则无环,反之有环 */ template<class VexType,class ArcType> void MGraph<VexType,ArcType>::CheckCircle() { int count=0;//环的个数 int top=-1; int stack[MAX_Vertex_Num]; bool inStack[MAX_Vertex_Num]={false}; bool visited[MAX_Vertex_Num]={false}; for (int i=0;i<vexnum;i++) { if (!visited[i]) { DFS(i,visited,stack,top,inStack,count); } } } template<class VexType,class ArcType> void MGraph<VexType,ArcType>::DFS(int x,bool visited[MAX_Vertex_Num],int stack[MAX_Vertex_Num],int& top,bool inStack[MAX_Vertex_Num],int& count) { visited[x]=true; stack[++top]=x; inStack[x]=true; for (int i=0;i<vexnum;i++) { if (arcs[x][i]!=0)//有边 { if (!inStack[i]) { DFS(i,visited,stack,top,inStack,count); } else //条件成立,表示下标为x的顶点到 下标为i的顶点有环 { count++; cout<<"第"<<count<<"环为:"; //从i到x是一个环,top的位置是x,下标为i的顶点在栈中的位置要寻找一下 //寻找起始顶点下标在栈中的位置 int t=0; for (t=top;stack[t]!=i;t--); //输出环中顶点 for (int j=t;j<=top;j++) { cout<<vexs[stack[j]]; } cout<<endl; } } } //处理完结点后,退栈 top--; inStack[x]=false; } int main() { MGraph<char,int> G; G.CreateGraph(); G.CheckCircle(); system("pause"); return 1; }
结果测试:
有向图:
结果:
- //求图中环的个数
- //由于图中每个点的出度只有1,所以不存在一个点处于两个环的交点
- //因此,求环的个数时每个只需要考虑一次便可得出结果
- //由于数据规模庞大,写成递归形式容易暴栈
- //在读边的过程中先对自环进行预处理,之后对每个点进行不同的染色,对它的下一个点也染同样的颜色
- //这样染下去如果发现下一个要染的点和正在染的颜色相同,则说明存在一个环
- //换染色起点的同时也需要更换新的染色,才能保证对环的判断正确
- #include<iostream>
- #include<cstring>
- using namespace std;
- int next[1000001];//指向下一结点的指针
- int vis[1000001];//对每个结点进行不同的标记
- int ans,n,ringID,p;
- void search()
- {
- for(int i = 1;i <= n;++i)
- {
- if(vis[i] > 0) continue;
- p = i;
- ++ringID;//对每一种环进行一种不同的标记,新的起点必须更换新的染色
- while(vis[p] == 0)//当前结点未被染色
- {
- vis[p] = ringID;//染色
- p = next[p];//指向下一个点
- if(vis[p] == ringID)//下一个点的颜色和当前染色相同,则说明存在一个环
- ++ans;
- }
- }
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- while(scanf("%d",&n) != EOF)
- {
- ans = 0;
- ringID = 1;
- memset(vis,0,sizeof(vis));
- for(int i = 1;i <= n;++i)
- {
- scanf("%d",&next[i]);
- if(next[i] == i)
- {
- vis[i] = ringID++;//先对自环进行预处理
- ans++;
- }
- }
- search();
- printf("%d/n",ans);
- }
- return 0;
- }