无向图:
法1:
- 如果存在回路,则必存在一个子图,是一个环路。环路中所有顶点的度>=2。
- n算法:
- 第一步:删除所有度<=1的顶点及相关的边,并将另外与这些边相关的其它顶点的度减一。
- 第二步:将度数变为1的顶点排入队列,并从该队列中取出一个顶点重复步骤一。
- 如果最后还有未删除顶点,则存在环,否则没有环。
- n算法分析:
- 由于有m条边,n个顶点。如果m>=n,则根据图论知识可直接判断存在环路。
- (证明:如果没有环路,则该图必然是k棵树 k>=1。根据树的性质,边的数目m = n-k。k>=1,所以:m<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的顶点,输出顶点,删除出边。循环到无顶点输出。
若:输出所有顶点,则课拓扑排序,无环;反之,则不能拓扑排序,有环
使用:可以使用拓扑排序为有向无环图每一个结点进行编号,拓扑排序输出的顺序可以为编号顺序
源代码:
1. #include <iostream>
2. using namespace
3. const int
4. template<class VexType,class
5. class
6. {
7. public:
8. void CreateGraph();//创建图
9. int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
10. void
11. private:
12. //顶点向量
13. //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
14. int vexnum;//顶点数
15. int arcnum;//边数
16. private:
17. bool
18. };
19.
20. template<class VexType,class
21. void
22. {
23. VexType first;
24. VexType Secend;
25. "请输入顶点数:";
26. cin>>vexnum;
27. "请输入边数:";
28. cin>>arcnum;
29. "请输入各个顶点值:";
30. for (int
31. {
32. cin>>vexs[i];
33. }
34. //初始化邻接矩阵
35. for (int
36. {
37. for (int
38. {
39. arcs[i][j]=0;
40. }
41. }
42. "请输入边的信息:"<<endl;
43. for (int
44. {
45. cin>>first>>Secend;
46. //如果边有权值的话,则还应该输入权值
47. int
48. int
49. //如果是有权的话,这里应该是arc[x][y]=权值
50. }
51. }
52. /*
53. 参数:v:表示顶点向量中一个值
54. 函数返回值:函数返回v在顶点向量中的下标
55. */
56. template<class VexType,class
57. int
58. {
59. for (int
60. {
61. if
62. {
63. return
64. }
65. }
66. return
67. }
68. /*
69. 有向图可以拓扑排序的条件是:图中没有环。
70. 具体方法:
71. ⑴ 从图中选择一个入度为0的点加入拓扑序列。
72. ⑵ 从图中删除该结点以及它的所有出边(即与之相邻点入度减1)。
73.
74. */
75. template<class VexType,class
76. bool
77. {
78. int count = 0;//拓扑排序输出顶点的个数
79. int
80. int
81. int
82. //求各个顶点的入度--邻接矩阵要查询该元素的列(记录入度情况)--
83. //如果是邻接表,就是麻烦在这里,查询结点入度很不方便
84. for (int
85. {
86. int
87. for (int
88. {
89. if
90. {
91. num++;
92. }
93. }
94. indegree[i]=num;
95. }
96. //把入度为0的顶点入栈
97. for (int
98. {
99. if
100. {
101. //顶点的下标
102. }
103. }
104. //处理入度为0的结点:把入度为0的结点出栈,删除与之有关的边
105. while
106. {
107. int
108. cout<<vexs[x];
109. count++;
110. //把与下标为x的顶点有关的边都去掉(出边),并改变对应结点的入度
111. for (int
112. {
113. if
114. {
115. //删除到下标为i的顶点的边,这时此顶点的入度减一
116. indegree[i]--;
117. if (!indegree[i])//顶点的入度为0,则入栈
118. {
119. stack[++top]=i;
120. }
121. }
122. }
123. }
124. cout<<endl;
125. if (count == vexnum) //能拓扑排序
126. {
127. return true;
128. }
129. return false;
130. }
131. /*
132. 检查图中是不是有环
133. 思想:
134. 能进行拓扑排序,则无环,反之有环
135. */
136. template<class VexType,class
137. void
138. {
139. if
140. {
141. "无环!"<<endl;
142. }
143. else
144. {
145. "有环!"<<endl;
146. }
147. }
148.
149. int
150. {
151. char,int> G;
152. G.CreateGraph();
153. G.CheckCircle();
154. "pause");
155. return
156. }
#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、深度遍历 + 回溯
思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。
代码:
[cpp] view plain copy print ?
- #include <iostream>
- using namespace
- const int
- template<class VexType,class
- class
- {
- public:
- void CreateGraph();//创建图
- int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
- bool CheckCircle();//检查图中有无环
- private:
- //顶点向量
- //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
- int vexnum;//顶点数
- int arcnum;//边数
- private:
- void CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool
- };
- template<class VexType,class
- void
- {
- VexType first;
- VexType Secend;
- "请输入顶点数:";
- cin>>vexnum;
- "请输入边数:";
- cin>>arcnum;
- "请输入各个顶点值:";
- for (int
- {
- cin>>vexs[i];
- }
- //初始化邻接矩阵
- for (int
- {
- for (int
- {
- arcs[i][j]=0;
- }
- }
- "请输入边的信息:"<<endl;
- for (int
- {
- cin>>first>>Secend;
- //如果边有权值的话,则还应该输入权值
- int
- int
- //如果是有权的话,这里应该是arc[x][y]=权值
- }
- }
- /*
- 参数:v:表示顶点向量中一个值
- 函数返回值:函数返回v在顶点向量中的下标
- */
- template<class VexType,class
- int
- {
- for (int
- {
- if
- {
- return
- }
- }
- return
- }
- /*
- 思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。
- */
- template<class VexType,class
- void MGraph<VexType,ArcType>::CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool
- {
- true;
- true;
- for (int
- {
- if
- {
- if (visited[j]==false)
- {
- CheckCircle(j,isExist,visited,Isvisited);
- }
- else
- {
- true;
- }
- }
- }
- false;//回溯,如果不写就变成一半的深度遍历,不能进行判断是否有边存在
- }
- template<class VexType,class
- bool
- {
- bool isExist = false;
- bool Isvisited[MAX_Vertex_Num]={false};
- bool visited[MAX_Vertex_Num]={false};
- for (int
- {
- if (Isvisited[i]==false)
- {
- CheckCircle(i,isExist,visited,Isvisited);
- if
- {
- return true;
- }
- }
- }
- return
- }
- int
- {
- char,int> G;
- G.CreateGraph();
- if
- {
- "图存在环!"<<endl;
- }
- else
- {
- "图不存在环!"<<endl;
- }
- "pause");
- return
- }
#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判断是否在栈中
源代码:
[cpp] view plain copy print ?
- #include <iostream>
- using namespace
- const int
- template<class VexType,class
- class
- {
- public:
- void CreateGraph();//创建图
- int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
- void
- private:
- //顶点向量
- //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
- 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
- void
- {
- VexType first;
- VexType Secend;
- "请输入顶点数:";
- cin>>vexnum;
- "请输入边数:";
- cin>>arcnum;
- "请输入各个顶点值:";
- for (int
- {
- cin>>vexs[i];
- }
- //初始化邻接矩阵
- for (int
- {
- for (int
- {
- arcs[i][j]=0;
- }
- }
- "请输入边的信息:"<<endl;
- for (int
- {
- cin>>first>>Secend;
- //如果边有权值的话,则还应该输入权值
- int
- int
- //如果是有权的话,这里应该是arc[x][y]=权值
- }
- }
- /*
- 参数:v:表示顶点向量中一个值
- 函数返回值:函数返回v在顶点向量中的下标
- */
- template<class VexType,class
- int
- {
- for (int
- {
- if
- {
- return
- }
- }
- return
- }
- /*
- 检查图中是不是有回向边
- 思想:
- 如果有回向边,则无环,反之有环
- */
- template<class VexType,class
- void
- {
- int count=0;//环的个数
- int
- int
- bool inStack[MAX_Vertex_Num]={false};
- bool visited[MAX_Vertex_Num]={false};
- for (int
- {
- if
- {
- DFS(i,visited,stack,top,inStack,count);
- }
- }
- }
- template<class VexType,class
- 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)
- {
- true;
- stack[++top]=x;
- true;
- for (int
- {
- if (arcs[x][i]!=0)//有边
- {
- if
- {
- DFS(i,visited,stack,top,inStack,count);
- }
- else //条件成立,表示下标为x的顶点到 下标为i的顶点有环
- {
- count++;
- "第"<<count<<"环为:";
- //从i到x是一个环,top的位置是x,下标为i的顶点在栈中的位置要寻找一下
- //寻找起始顶点下标在栈中的位置
- int
- for
- //输出环中顶点
- for (int
- {
- cout<<vexs[stack[j]];
- }
- cout<<endl;
- }
- }
- }
- //处理完结点后,退栈
- top--;
- false;
- }
- int
- {
- char,int> G;
- G.CreateGraph();
- G.CheckCircle();
- "pause");
- return
- }
#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. //求图中环的个数
2. //由于图中每个点的出度只有1,所以不存在一个点处于两个环的交点
3. //因此,求环的个数时每个只需要考虑一次便可得出结果
4. //由于数据规模庞大,写成递归形式容易暴栈
5. //在读边的过程中先对自环进行预处理,之后对每个点进行不同的染色,对它的下一个点也染同样的颜色
6. //这样染下去如果发现下一个要染的点和正在染的颜色相同,则说明存在一个环
7. //换染色起点的同时也需要更换新的染色,才能保证对环的判断正确
8. #include<iostream>
9. #include<cstring>
10. using namespace
11. int next[1000001];//指向下一结点的指针
12. int vis[1000001];//对每个结点进行不同的标记
13. int
14. void
15. {
16. for(int
17. {
18. if(vis[i] > 0) continue;
19. p = i;
20. //对每一种环进行一种不同的标记,新的起点必须更换新的染色
21. while(vis[p] == 0)//当前结点未被染色
22. {
23. //染色
24. //指向下一个点
25. if(vis[p] == ringID)//下一个点的颜色和当前染色相同,则说明存在一个环
26. ++ans;
27. }
28. }
29. }
30. int
31. {
32. //freopen("in.txt","r",stdin);
33. while(scanf("%d",&n) != EOF)
34. {
35. ans = 0;
36. ringID = 1;
37. sizeof(vis));
38. for(int
39. {
40. "%d",&next[i]);
41. if(next[i] == i)
42. {
43. //先对自环进行预处理
44. ans++;
45. }
46. }
47. search();
48. "%d/n",ans);
49. }
50. return
51. }
判断有向图是否有环有三种方法:拓扑排序、深度遍历+回溯、深度遍历 + 判断后退边
这里使用 拓扑排序 和 深度遍历 + 回溯判断是不是环。使用 深度遍历 + 判断后退边找出环个数 以及环中元素
1、拓扑排序
思想:找入度为0的顶点,输出顶点,删除出边。循环到无顶点输出。
若:输出所有顶点,则课拓扑排序,无环;反之,则不能拓扑排序,有环
使用:可以使用拓扑排序为有向无环图每一个结点进行编号,拓扑排序输出的顺序可以为编号顺序
源代码:
1. #include <iostream>
2. using namespace
3. const int
4. template<class VexType,class
5. class
6. {
7. public:
8. void CreateGraph();//创建图
9. int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
10. void
11. private:
12. //顶点向量
13. //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
14. int vexnum;//顶点数
15. int arcnum;//边数
16. private:
17. bool
18. };
19.
20. template<class VexType,class
21. void
22. {
23. VexType first;
24. VexType Secend;
25. "请输入顶点数:";
26. cin>>vexnum;
27. "请输入边数:";
28. cin>>arcnum;
29. "请输入各个顶点值:";
30. for (int
31. {
32. cin>>vexs[i];
33. }
34. //初始化邻接矩阵
35. for (int
36. {
37. for (int
38. {
39. arcs[i][j]=0;
40. }
41. }
42. "请输入边的信息:"<<endl;
43. for (int
44. {
45. cin>>first>>Secend;
46. //如果边有权值的话,则还应该输入权值
47. int
48. int
49. //如果是有权的话,这里应该是arc[x][y]=权值
50. }
51. }
52. /*
53. 参数:v:表示顶点向量中一个值
54. 函数返回值:函数返回v在顶点向量中的下标
55. */
56. template<class VexType,class
57. int
58. {
59. for (int
60. {
61. if
62. {
63. return
64. }
65. }
66. return
67. }
68. /*
69. 有向图可以拓扑排序的条件是:图中没有环。
70. 具体方法:
71. ⑴ 从图中选择一个入度为0的点加入拓扑序列。
72. ⑵ 从图中删除该结点以及它的所有出边(即与之相邻点入度减1)。
73.
74. */
75. template<class VexType,class
76. bool
77. {
78. int count = 0;//拓扑排序输出顶点的个数
79. int
80. int
81. int
82. //求各个顶点的入度--邻接矩阵要查询该元素的列(记录入度情况)--
83. //如果是邻接表,就是麻烦在这里,查询结点入度很不方便
84. for (int
85. {
86. int
87. for (int
88. {
89. if
90. {
91. num++;
92. }
93. }
94. indegree[i]=num;
95. }
96. //把入度为0的顶点入栈
97. for (int
98. {
99. if
100. {
101. //顶点的下标
102. }
103. }
104. //处理入度为0的结点:把入度为0的结点出栈,删除与之有关的边
105. while
106. {
107. int
108. cout<<vexs[x];
109. count++;
110. //把与下标为x的顶点有关的边都去掉(出边),并改变对应结点的入度
111. for (int
112. {
113. if
114. {
115. //删除到下标为i的顶点的边,这时此顶点的入度减一
116. indegree[i]--;
117. if (!indegree[i])//顶点的入度为0,则入栈
118. {
119. stack[++top]=i;
120. }
121. }
122. }
123. }
124. cout<<endl;
125. if (count == vexnum) //能拓扑排序
126. {
127. return true;
128. }
129. return false;
130. }
131. /*
132. 检查图中是不是有环
133. 思想:
134. 能进行拓扑排序,则无环,反之有环
135. */
136. template<class VexType,class
137. void
138. {
139. if
140. {
141. "无环!"<<endl;
142. }
143. else
144. {
145. "有环!"<<endl;
146. }
147. }
148.
149. int
150. {
151. char,int> G;
152. G.CreateGraph();
153. G.CheckCircle();
154. "pause");
155. return
156. }
#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、深度遍历 + 回溯
思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。
代码:
1. #include <iostream>
2. using namespace
3. const int
4. template<class VexType,class
5. class
6. {
7. public:
8. void CreateGraph();//创建图
9. int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
10. bool CheckCircle();//检查图中有无环
11. private:
12. //顶点向量
13. //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
14. int vexnum;//顶点数
15. int arcnum;//边数
16. private:
17. void CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool
18. };
19.
20. template<class VexType,class
21. void
22. {
23. VexType first;
24. VexType Secend;
25. "请输入顶点数:";
26. cin>>vexnum;
27. "请输入边数:";
28. cin>>arcnum;
29. "请输入各个顶点值:";
30. for (int
31. {
32. cin>>vexs[i];
33. }
34. //初始化邻接矩阵
35. for (int
36. {
37. for (int
38. {
39. arcs[i][j]=0;
40. }
41. }
42. "请输入边的信息:"<<endl;
43. for (int
44. {
45. cin>>first>>Secend;
46. //如果边有权值的话,则还应该输入权值
47. int
48. int
49. //如果是有权的话,这里应该是arc[x][y]=权值
50. }
51. }
52. /*
53. 参数:v:表示顶点向量中一个值
54. 函数返回值:函数返回v在顶点向量中的下标
55. */
56. template<class VexType,class
57. int
58. {
59. for (int
60. {
61. if
62. {
63. return
64. }
65. }
66. return
67. }
68.
69. /*
70. 思想:用回溯法,遍历时,如果遇到了之前访问过的结点,则图中存在环。
71. */
72. template<class VexType,class
73. void MGraph<VexType,ArcType>::CheckCircle(int u,bool& isExist,bool visited[MAX_Vertex_Num],bool
74. {
75. true;
76. true;
77. for (int
78. {
79. if
80. {
81. if (visited[j]==false)
82. {
83. CheckCircle(j,isExist,visited,Isvisited);
84. }
85. else
86. {
87. true;
88. }
89. }
90. }
91. false;//回溯,如果不写就变成一半的深度遍历,不能进行判断是否有边存在
92. }
93.
94. template<class VexType,class
95. bool
96. {
97. bool isExist = false;
98. bool Isvisited[MAX_Vertex_Num]={false};
99. bool visited[MAX_Vertex_Num]={false};
100. for (int
101. {
102. if (Isvisited[i]==false)
103. {
104. CheckCircle(i,isExist,visited,Isvisited);
105. if
106. {
107. return true;
108. }
109. }
110. }
111. return
112. }
113.
114. int
115. {
116. char,int> G;
117. G.CreateGraph();
118. if
119. {
120. "图存在环!"<<endl;
121. }
122. else
123. {
124. "图不存在环!"<<endl;
125. }
126. "pause");
127. return
128. }
#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判断是否在栈中
源代码:
1. #include <iostream>
2. using namespace
3. const int
4. template<class VexType,class
5. class
6. {
7. public:
8. void CreateGraph();//创建图
9. int LocateVex(VexType v);//返回顶点v所在顶点向量中的位置(下标)
10. void
11. private:
12. //顶点向量
13. //这里把邻接矩阵类型用模板表示,主要是为了处理有权值的情况,比如:权值可以为小数(代价),也可以为整数
14. int vexnum;//顶点数
15. int arcnum;//边数
16. private:
17. void DFS(int x,bool visited[MAX_Vertex_Num],int stack[MAX_Vertex_Num],int& top,bool inStack[MAX_Vertex_Num],int& count);
18.
19. };
20.
21. template<class VexType,class
22. void
23. {
24. VexType first;
25. VexType Secend;
26. "请输入顶点数:";
27. cin>>vexnum;
28. "请输入边数:";
29. cin>>arcnum;
30. "请输入各个顶点值:";
31. for (int
32. {
33. cin>>vexs[i];
34. }
35. //初始化邻接矩阵
36. for (int
37. {
38. for (int
39. {
40. arcs[i][j]=0;
41. }
42. }
43. "请输入边的信息:"<<endl;
44. for (int
45. {
46. cin>>first>>Secend;
47. //如果边有权值的话,则还应该输入权值
48. int
49. int
50. //如果是有权的话,这里应该是arc[x][y]=权值
51. }
52. }
53. /*
54. 参数:v:表示顶点向量中一个值
55. 函数返回值:函数返回v在顶点向量中的下标
56. */
57. template<class VexType,class
58. int
59. {
60. for (int
61. {
62. if
63. {
64. return
65. }
66. }
67. return
68. }
69.
70. /*
71. 检查图中是不是有回向边
72. 思想:
73. 如果有回向边,则无环,反之有环
74. */
75. template<class VexType,class
76. void
77. {
78. int count=0;//环的个数
79. int
80. int
81. bool inStack[MAX_Vertex_Num]={false};
82. bool visited[MAX_Vertex_Num]={false};
83. for (int
84. {
85. if
86. {
87. DFS(i,visited,stack,top,inStack,count);
88. }
89. }
90. }
91.
92. template<class VexType,class
93. 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)
94. {
95. true;
96. stack[++top]=x;
97. true;
98. for (int
99. {
100. if (arcs[x][i]!=0)//有边
101. {
102. if
103. {
104. DFS(i,visited,stack,top,inStack,count);
105. }
106. else //条件成立,表示下标为x的顶点到 下标为i的顶点有环
107. {
108. count++;
109. "第"<<count<<"环为:";
110. //从i到x是一个环,top的位置是x,下标为i的顶点在栈中的位置要寻找一下
111. //寻找起始顶点下标在栈中的位置
112. int
113. for
114. //输出环中顶点
115. for (int
116. {
117. cout<<vexs[stack[j]];
118. }
119. cout<<endl;
120. }
121. }
122. }
123. //处理完结点后,退栈
124. top--;
125. false;
126. }
127. int
128. {
129. char,int> G;
130. G.CreateGraph();
131. G.CheckCircle();
132. "pause");
133. return
134. }
#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. //求图中环的个数
2. //由于图中每个点的出度只有1,所以不存在一个点处于两个环的交点
3. //因此,求环的个数时每个只需要考虑一次便可得出结果
4. //由于数据规模庞大,写成递归形式容易暴栈
5. //在读边的过程中先对自环进行预处理,之后对每个点进行不同的染色,对它的下一个点也染同样的颜色
6. //这样染下去如果发现下一个要染的点和正在染的颜色相同,则说明存在一个环
7. //换染色起点的同时也需要更换新的染色,才能保证对环的判断正确
8. #include<iostream>
9. #include<cstring>
10. using namespace
11. int next[1000001];//指向下一结点的指针
12. int vis[1000001];//对每个结点进行不同的标记
13. int
14. void
15. {
16. for(int
17. {
18. if(vis[i] > 0) continue;
19. p = i;
20. //对每一种环进行一种不同的标记,新的起点必须更换新的染色
21. while(vis[p] == 0)//当前结点未被染色
22. {
23. //染色
24. //指向下一个点
25. if(vis[p] == ringID)//下一个点的颜色和当前染色相同,则说明存在一个环
26. ++ans;
27. }
28. }
29. }
30. int
31. {
32. //freopen("in.txt","r",stdin);
33. while(scanf("%d",&n) != EOF)
34. {
35. ans = 0;
36. ringID = 1;
37. sizeof(vis));
38. for(int
39. {
40. "%d",&next[i]);
41. if(next[i] == i)
42. {
43. //先对自环进行预处理
44. ans++;
45. }
46. }
47. search();
48. "%d/n",ans);
49. }
50. return
51. }