数据结构之图【深搜,广搜】

心已赠人 2022-08-11 15:29 295阅读 0赞
  1. package com.zhiru;
  2. import java.util.Queue;
  3. import java.util.LinkedList;
  4. public class MyGraph {
  5. private int maxVertices;// 最大顶点数
  6. private int numVertices;// 当前顶点数
  7. private int numEdges;// 当期边数
  8. private int[] verticesList;// 顶点表
  9. private int[][] edge;// 边邻接矩阵
  10. public MyGraph(int size) {
  11. maxVertices = size;
  12. numVertices = 0;
  13. numEdges = 0;
  14. int i, j;
  15. verticesList = new int[maxVertices];
  16. edge = new int[maxVertices][];
  17. for (i = 0; i < maxVertices; i++) {
  18. edge[i] = new int[maxVertices];
  19. }
  20. for (i = 0; i < maxVertices; i++) {
  21. for (j = 0; j < maxVertices; j++)
  22. edge[i][j] = (i == j) ? 0 : Integer.MAX_VALUE;
  23. }
  24. }
  25. public int getPos(int v) {
  26. for (int i = 0; i < numVertices; i++) {
  27. if (verticesList[i] == v)
  28. return i;
  29. }
  30. return -1;// 没找到
  31. }
  32. // 插入顶点v
  33. public void insertVertex(int v) {
  34. if (numVertices == maxVertices)
  35. return;
  36. verticesList[numVertices++] = v;
  37. }
  38. // 删除顶点v
  39. public void deleteVertex(int v) {
  40. if (v < 0 || v >= numVertices)
  41. return;
  42. if (numVertices == 1)
  43. return;// 只有一个顶点不删除
  44. int i, j, k, m;
  45. k = getPos(v);
  46. m = k;
  47. for (; k < numVertices - 1; k++) {
  48. verticesList[k] = verticesList[k + 1];
  49. }
  50. for (i = 0; i < numVertices; i++)
  51. if (edge[i][v] > 0 && edge[i][v] < Integer.MAX_VALUE)
  52. numEdges--;
  53. for (i = 0; i < numVertices; i++) {
  54. for (j = m; j < numVertices - 1; j++) {
  55. edge[i][j] = edge[i][j + 1];
  56. }
  57. }
  58. int n = numVertices;
  59. numVertices--;
  60. for (j = 0; j < numVertices; j++) {
  61. for (i = m; i < n - 1; i++) {
  62. edge[i][j] = edge[i + 1][j];
  63. }
  64. }
  65. }
  66. // 插入边v1,v2,权值cost
  67. public void insertEdge(int v1, int v2, int cost) {
  68. if (v1 > -1 && v1 < numVertices && v2 > -1 && v2 < numVertices
  69. && edge[v1][v2] == Integer.MAX_VALUE) {
  70. edge[v1][v2] = edge[v2][v1] = cost;
  71. numEdges++;
  72. } else {
  73. return;
  74. }
  75. }
  76. public void deleteEdge(int v1, int v2) {
  77. if (v1 > -1 && v1 < numVertices && v2 > -1 && v2 < numVertices
  78. && edge[v1][v2] > 0 && edge[v1][v2] < Integer.MAX_VALUE) {
  79. edge[v1][v2] = edge[v2][v1] = Integer.MAX_VALUE;
  80. numEdges--;
  81. } else
  82. return;
  83. }
  84. // 建立图
  85. /*
  86. * l是顶点一维矩阵,a是邻接矩阵,n是顶点数,m是边数
  87. */
  88. public void buildGraph(int[] l, int[][] a, int n, int m) {
  89. numVertices = n;
  90. numEdges = m;
  91. for (int k = 0; k < numVertices; k++) {
  92. verticesList[k] = l[k];
  93. }
  94. for (int i = 0; i < numVertices; i++) {
  95. for (int j = 0; j < numVertices; j++) {
  96. edge[i][j] = a[i][j];
  97. }
  98. }
  99. }
  100. public void prtGraph() {
  101. for (int i = 0; i < numVertices; i++) {
  102. for (int j = 0; j < numVertices; j++) {
  103. System.out.print(edge[i][j] + " ");
  104. }
  105. System.out.print("\n");
  106. }
  107. }
  108. // 给出顶点v的第一个邻接顶点的位置,找不到返回-1
  109. public int getFirstNeighbor(int v) {
  110. if (v != -1) {
  111. for (int col = 0; col < numVertices; col++)
  112. if (edge[v][col] > 0 && edge[v][col] < Integer.MAX_VALUE)
  113. return col;
  114. }
  115. return -1;
  116. }
  117. // 给出顶点v的邻接顶点w的下一个邻接顶点
  118. public int getNextNeighbor(int v, int w) {
  119. if (v != -1 && w != -1) {
  120. for (int col = w + 1; col < numVertices; col++)
  121. if (edge[v][col] > 0 && edge[v][col] < Integer.MAX_VALUE)
  122. return col;
  123. }
  124. return -1;
  125. }
  126. public int getValue(int i) {
  127. if (i >= 0 && i < numVertices)
  128. return verticesList[i];
  129. return -1;
  130. }
  131. // 图的深度优先搜索
  132. public void traversal(int k) {
  133. int loc, n = numVertices;
  134. boolean[] visited = new boolean[n];
  135. loc = getPos(k);
  136. dfs(loc, visited);
  137. }
  138. private void dfs(int loc, boolean[] visited) {
  139. prt("" + getValue(loc));
  140. visited[loc] = true;
  141. int w = getFirstNeighbor(loc);
  142. while (w != -1) {
  143. if (visited[w] == false)
  144. dfs(w, visited);
  145. w = getNextNeighbor(loc, w);
  146. }
  147. }
  148. // 图的广度优先搜索算法,从节点k开始遍历图
  149. public void bfs(int k) {
  150. int i, j, loc;
  151. Queue<Integer> q = new LinkedList<Integer>();
  152. loc = getPos(k);
  153. boolean[] visited = new boolean[numVertices];
  154. prt(getValue(loc) + "");
  155. visited[k] = true;
  156. q.offer(k);// k进队列
  157. while (!q.isEmpty()) {
  158. i = q.poll();
  159. for (j = 0; j < numVertices; j++) {
  160. if (!visited[j]) {
  161. prt(getValue(j) + "");
  162. visited[j] = true;// 标记已被访问
  163. q.offer(j);// 访问过的j入队
  164. }
  165. }
  166. }
  167. }
  168. public static void prt(String s) {
  169. System.out.print(s + "\n");
  170. }
  171. public static void main(String[] args) {
  172. // TODO Auto-generated method stub
  173. MyGraph mg = new MyGraph(10);
  174. int[][] a = { { 0, 1, Integer.MAX_VALUE, 4 },
  175. { Integer.MAX_VALUE, 0, 9, 2 }, { 3, 5, 0, 8 },
  176. { Integer.MAX_VALUE, Integer.MAX_VALUE, 6, 0 } };
  177. int[] vl = { 0, 1, 2, 3 };
  178. mg.buildGraph(vl, a, 4, 8);
  179. mg.prtGraph();
  180. mg.insertVertex(4);
  181. mg.insertEdge(0, 4, 100);
  182. prt("插入顶点4和0-->4边后");
  183. mg.prtGraph();
  184. // prt("删除顶点1后:");
  185. // mg.deleteVertex(1);
  186. //mg.prtGraph();
  187. prt("深度优先搜索:");
  188. mg.traversal(3);
  189. prt("广度优先搜索:");
  190. mg.bfs(4);
  191. }
  192. }

0 1 2147483647 4
2147483647 0 9 2
3 5 0 8
2147483647 2147483647 6 0
插入顶点4和0—>4边后
0 1 2147483647 4 100
2147483647 0 9 2 2147483647
3 5 0 8 2147483647
2147483647 2147483647 6 0 2147483647
100 2147483647 2147483647 2147483647 0
深度优先搜索:
3
2
0
1
4
广度优先搜索:
4
0
1
2
3

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读

    相关 广

    一、深搜 属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次;

    相关 广(初学者)

    搜索入门     最近对搜索有了一点浅显的了解,想跟大家分享分享。     说起来我也是初学者,恰巧有些自己的理解,想起来自己开始学习搜索的情况,真是一把鼻子一把

    相关 DFS\广BFS 初步入门

    首先,不管是BFS还是DFS,由于时间和空间的局限性,它们只能解决数据量比较小的问题。 深搜,顾名思义,它从某个状态开始,不断的转移状态,直到无法转移,然后退回到上一步的状态