当前位置:   article > 正文

有向图的深度和广度遍历_有向图的深度遍历

有向图的深度遍历

题目要求:

1.对于下图所示的有向图(访问顺序按序号从小到大),试写出: 

(1) 从顶点出发进行深度优先搜索所得到的深度优先生成树;

(2) 从顶点出发进行广度优先搜索所得到的广度优先生成树

 

  1. package com.test.tree;
  2. import java.util.*;
  3. public class Graph {
  4. // 存储节点信息
  5. private Object[] vertices;
  6. // 存储边的信息
  7. private int[][] arcs;
  8. private int vexnum;
  9. // 记录第i个节点是否被访问过
  10. private boolean[] visited;
  11. /**
  12. * @param args
  13. */
  14. public static void main(String[] args) {
  15. Graph g = new Graph(5);
  16. Character[] vertices = { '1', '2', '3', '4', '5'};
  17. g.addVertex(vertices);
  18. g.addEdge(0, 1);
  19. g.addEdge(0, 2);
  20. g.addEdge(1, 2);
  21. g.addEdge(1, 3);
  22. g.addEdge(1, 4);
  23. g.addEdge(2, 3);
  24. g.addEdge(3, 4);
  25. g.addEdge(4, 0);
  26. System.out.println("深度优先遍历:");
  27. g.depthTraverse();
  28. System.out.println();
  29. System.out.println("广度优先遍历:");
  30. g.broadTraverse2(1);
  31. System.out.println();
  32. }
  33. public Graph(int n) {
  34. vexnum = n;
  35. vertices = new Object[n];
  36. arcs = new int[n][n];
  37. visited = new boolean[n];
  38. for (int i = 0; i < vexnum; i++) {
  39. for (int j = 0; j < vexnum; j++) {
  40. arcs[i][j] = 0;
  41. }
  42. }
  43. }
  44. public void addVertex(Object[] obj) {
  45. this.vertices = obj;
  46. }
  47. public void addEdge(int i, int j) {
  48. if (i == j)return;
  49. arcs[i][j] = 1; //单独一条表示有向图
  50. //arcs[j][i] = 1; // 这一条打开是无线图
  51. }
  52. public int firstAdjVex(int i) {
  53. for (int j = 0; j < vexnum; j++) {
  54. if (arcs[i][j] > 0)
  55. return j;
  56. }
  57. return -1;
  58. }
  59. public int nextAdjVex(int i, int k) {
  60. for (int j = k + 1; j < vexnum; j++) {
  61. if (arcs[i][j] > 0)
  62. return j;
  63. }
  64. return -1;
  65. }
  66. // 深度优先遍历
  67. public void depthTraverse() {
  68. for (int i = 0; i < vexnum; i++) {
  69. visited[i] = false;
  70. }
  71. for (int i = 0; i < vexnum; i++) {
  72. if (!visited[i])
  73. traverse(i);
  74. }
  75. }
  76. // 一个连通图的深度递归遍历
  77. public void traverse(int i) {
  78. // TODO Auto-generated method stub
  79. visited[i] = true;
  80. visit(i);
  81. for (int j = this.firstAdjVex(i); j >= 0; j = this.nextAdjVex(i, j)) {
  82. if (!visited[j])
  83. this.traverse(j);
  84. }
  85. }
  86. // 广度优先遍历 任意节点开始,这里是第二节点开始
  87. public void broadTraverse2(int n) {
  88. // LinkedList实现了Queue接口
  89. Queue<Integer> q = new LinkedList<Integer>();
  90. for (int i = 0; i < vexnum; i++) {
  91. visited[i] = false;
  92. }
  93. if (!visited[n]) {
  94. q.add(n);
  95. visited[n] = true;
  96. visit(n);
  97. while (!q.isEmpty()) {
  98. int j = (Integer) q.remove().intValue();
  99. int k = this.firstAdjVex(j);
  100. for ( k = this.firstAdjVex(j); k >= 0; k = this
  101. .nextAdjVex(j, k)) {
  102. if (!visited[k]) {
  103. q.add(k);
  104. visited[k] = true;
  105. visit(k);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. // 广度优先遍历 默认从0开始
  112. public void broadTraverse(int n) {
  113. // LinkedList实现了Queue接口
  114. Queue<Integer> q = new LinkedList<Integer>();
  115. for (int i = 0; i < vexnum; i++) {
  116. visited[i] = false;
  117. }
  118. for (int i = 0; i < vexnum; i++) {
  119. if (!visited[i]) {
  120. q.add(i);
  121. visited[i] = true;
  122. visit(i);
  123. while (!q.isEmpty()) {
  124. int j = (Integer) q.remove().intValue();
  125. for (int k = this.firstAdjVex(j); k >= 0; k = this
  126. .nextAdjVex(j, k)) {
  127. if (!visited[k]) {
  128. q.add(k);
  129. visited[k] = true;
  130. visit(k);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. private void visit(int i) {
  138. // TODO Auto-generated method stub
  139. System.out.print(vertices[i] + " ");
  140. }
  141. // 最后一个
  142. public int lastAdjVex(int i) {
  143. for (int j = vexnum - 1; j >= 0; j--) {
  144. if (arcs[i][j] > 0)
  145. return j;
  146. }
  147. return -1;
  148. }
  149. // 上一个
  150. public int lastAdjVex(int i, int k) {
  151. for (int j = k - 1; j >= 0; j--) {
  152. if (arcs[i][j] > 0)
  153. return j;
  154. }
  155. return -1;
  156. }
  157. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/923616
推荐阅读
相关标签
  

闽ICP备14008679号