[关闭]
@rg070836rg 2015-12-28T12:31:53.000000Z 字数 3095 阅读 1549

算法概论实验十

算法概论实验

  1. 实验十
  2. 实验目的与要求:
  3. 1 掌握使用图的深度优先搜索算法实现对有向图中是否包含环的判断;
  4. 2 掌握使用图的深度优先搜索算法实现对有向图的强连通分量的划分。
  5. 1.有向图中环的判断问题
  6. 【问题描述】
  7. 给定一个有向图,要求使用深度优先搜索策略,判断图中是否存在环。
  8. 2.有向图的强连通分量问题
  9. 【问题描述】
  10. 给定一个有向图,设计一个算法,求解并输出该图的各个强连通分量。
  1. import java.util.ArrayList;
  2. public class exam10 {
  3. //1.有向图判环
  4. public static String havecycle(int[][] a)
  5. {
  6. if(DFS(a)) //如果没环
  7. return "no cycle";
  8. else
  9. return "have cycle";
  10. }
  11. public static boolean DFS(int[][] a)
  12. //有环返回false
  13. {
  14. boolean test = true;
  15. int length = a.length;
  16. String[] color = new String[length];
  17. for(int i=0;i<length;i++)
  18. color[i] = "white";
  19. //int clock = 1;
  20. for(int i=0;i<length;i++)
  21. if(color[i] == "white")
  22. {
  23. test = explore(a,i,color);
  24. if(test == false)
  25. return false;
  26. }
  27. return true;
  28. }
  29. public static boolean explore
  30. (int[][] a,int u,String[] color)
  31. //以u为顶点进行探索
  32. {
  33. color[u] = "gray";
  34. for(int v=0;v<a.length;v++)
  35. {
  36. if(a[u][v]!=0 && color[v]=="white")
  37. explore(a,v,color);
  38. if(a[u][v]!=0 && color[v]=="gray")
  39. //如果有一条回边,表示有一个环
  40. return false;
  41. }
  42. color[u] = "black";
  43. return true;
  44. }
  45. //2.找强连通分量
  46. public static void findSC(int[][] a1)
  47. {
  48. int length = a1.length;
  49. int[][] a = new int[length][length];
  50. int[][] b = new int[length][length];
  51. getGR(a1,b);
  52. //b是反向图
  53. getGR(b,a);
  54. //防止修改原图a=a1
  55. Set[] set = new Set[length];
  56. int count = 0;
  57. int sum = 0;
  58. //要在反向图中找一个Post值最大的点,以这个点为起点,在原图上进行DFS,则找到一个强连通部件
  59. do
  60. {
  61. System.out.print("第"+(count+1)+"个连通分量为:");
  62. getGR(a,b);
  63. int maxpost = findmaxpost(b);
  64. set[count] = DFS2(a,maxpost); //第一个强连通部件set[0]
  65. //去掉这个连通部件中的点
  66. for(int i=0;i<set[count].value.size();i++)
  67. {
  68. int p = set[count].value.indexOf(i);
  69. delete(a,p);
  70. }
  71. sum = sum + set[count].value.size(); //sum计算去掉的点数
  72. count++;
  73. System.out.println();
  74. }while(sum<length);
  75. }
  76. //在一个图中找出post值最大的节点点
  77. public static int findmaxpost(int[][] a)
  78. {
  79. int max = 0;
  80. int maxindex = 0;
  81. int[] post = new int[a.length];
  82. DFS1(a,post);
  83. for(int i=0;i<a.length;i++)
  84. {
  85. int temp = max;
  86. max = Math.max(max, post[i]);
  87. if(max != temp)
  88. maxindex = i;
  89. }
  90. return maxindex;
  91. }
  92. public static void DFS1(int[][] a,int[] post)
  93. {
  94. int length = a.length;
  95. String[] color = new String[length];
  96. for(int i=0;i<length;i++)
  97. color[i] = "white";
  98. int[] clock = new int[1];
  99. clock[0] = 1;
  100. for(int i=0;i<length;i++)
  101. if(color[i] == "white")
  102. {
  103. explore1(a,i,color,post,clock);
  104. }
  105. }
  106. public static void explore1
  107. (int[][] a,int u,String[] color,int[] post,int[] clock)//以u为顶点进行探索
  108. {
  109. color[u] = "gray";
  110. clock[0]++;
  111. for(int v=0;v<a.length;v++)
  112. {
  113. if(a[u][v]!=0 && color[v]=="white")
  114. explore1(a,v,color,post,clock);
  115. }
  116. color[u] = "black";
  117. post[u] = clock[0];
  118. //System.out.print("post["+u+"]="+post[u]+" ");
  119. clock[0]++;
  120. }
  121. //获得一个图的反向图
  122. public static void getGR(int[][] a,int[][] b)
  123. {
  124. for(int i=0;i<a.length;i++)
  125. for(int j=0;j<a.length;j++)
  126. b[i][j] = a[j][i];
  127. }
  128. //返回u能访问到的点集
  129. public static Set DFS2(int[][] a,int u)
  130. {
  131. Set s = new Set();
  132. int length = a.length;
  133. String[] color = new String[length];
  134. for(int i=0;i<length;i++)
  135. color[i] = "white";
  136. //初始化完成
  137. explore2(a,u,color,s);
  138. return s;
  139. }
  140. public static void explore2(int[][] a,int u,String[] color,Set s)//以u为顶点进行探索
  141. {
  142. s.value.add(u);
  143. System.out.print(u+" ");
  144. color[u] = "gray";
  145. for(int v=0;v<a.length;v++)
  146. {
  147. if(a[u][v]!=0 && color[v]=="white")
  148. explore2(a,v,color,s);
  149. }
  150. color[u] = "black";
  151. }
  152. //删除a中所有与p相连的边
  153. public static void delete(int[][] a,int p)
  154. {
  155. for(int i=0;i<a.length;i++)
  156. {
  157. a[i][p] = 0;
  158. a[p][i] = 0;
  159. }
  160. }
  161. public static void main(String[] args) {
  162. //测环
  163. int[][] a = new int[6][6];
  164. for(int i=0;i<a.length;i++)
  165. for(int j=0;j<a.length;j++)
  166. a[i][j] = 0;
  167. a[1][0] = 1;a[1][3] = 1;a[2][1] = 1;a[3][4] = 1;
  168. a[4][5] = 1;a[5][2] = 1;a[5][3] = 1;
  169. System.out.println(havecycle(a));
  170. //强连通分量
  171. findSC(a);
  172. }
  173. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注