当前位置:Gxlcms > mysql > Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一

Crackingcodinginterview(4.2)有向图判断任意2点之间是否有一

时间:2021-07-01 10:21:17 帮助过:13人阅读

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 1.图的存储使用邻接矩阵 2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...) 3.可以写一个map函数给按一定规则所有节点编号(本例未实现) 4.alg

4.2 Given a directed graph, design an algorithm to find out whether

there is a route between two nodes.

1.图的存储使用邻接矩阵

2.使用邻接矩阵的过程中,必须给每个节点编号(0,1...)

3.可以写一个map函数给按一定规则所有节点编号(本例未实现)

4.algorithm:通过bfs或dfs遍历从其中一个节点开始遍历图,看是否对可达另一个节点。

  1. import java.util.Queue;
  2. import java.util.LinkedList;
  3. import java.util.Stack;
  4. //vertex in the graph must have serial number
  5. //from 0 to n, if graph isn't suitable for this
  6. //write map() to map.
  7. //directed no-weight graph
  8. class Graph1{
  9. private static boolean[][] Matrix;
  10. private static int vertexNum;
  11. public static void generator(int[][] G, int vNum){
  12. vertexNum = vNum;
  13. Matrix = new boolean[vertexNum][vertexNum];
  14. for(int i=0;i < G.length;i++){
  15. Matrix[G[i][0]][G[i][1]] = true;
  16. }
  17. }
  18. public static boolean isRouteDFS(int v1, int v2){
  19. if(v1 < 0 || v2 < 0 ||
  20. v1 >= Matrix.length || v2 >= Matrix.length)
  21. return false;
  22. boolean[] isVisited = new boolean[vertexNum];
  23. Stack<integer> s = new Stack<integer>();
  24. int v = -1;
  25. if(v1 != v2){
  26. s.push(v1);
  27. isVisited[v1] = true;
  28. } else
  29. return true;
  30. while(!s.empty()){
  31. v = s.peek();//when v's all next node have been invisted, then pop
  32. //
  33. System.out.println("["+v+"]");//
  34. boolean Marked = false;
  35. for(int j=0;j < Matrix[v].length;j++)
  36. if(Matrix[v][j] && !isVisited[j])
  37. if(j == v2){
  38. return true;
  39. } else{
  40. s.push(j);
  41. isVisited[j] = true;
  42. Marked = true;
  43. break;
  44. }
  45. if(!Marked)
  46. s.pop();
  47. }
  48. return false;
  49. }
  50. public static boolean isRouteBFS(int v1, int v2){
  51. if(v1 < 0 || v2 < 0 ||
  52. v1 >= Matrix.length || v2 >= Matrix.length)
  53. return false;
  54. boolean[] isVisited = new boolean[vertexNum];
  55. Queue<integer> queue = new LinkedList<integer>();
  56. int v = -1;
  57. queue.offer(v1);
  58. isVisited[v1] = true;
  59. while(!queue.isEmpty()){
  60. v = queue.poll();
  61. if(v == v2)
  62. return true;
  63. for(int j = 0;j < Matrix[v].length;j++)
  64. if(Matrix[v][j] == true && isVisited[j] == false)
  65. queue.offer(j);
  66. }
  67. return false;
  68. }
  69. public static void printMatrix(){
  70. for(int i=0;i < Matrix.length;i++){
  71. System.out.print(i+": ");
  72. for(int j=0;j < Matrix[i].length;j++)
  73. System.out.print((Matrix[i][j] == true ? 1 : 0) + " ");
  74. System.out.println();
  75. }
  76. }
  77. }
  78. public class Solution{
  79. public static void main(String[] args){
  80. int[][] G = {
  81. {0, 1}, {0, 2},
  82. {1, 3}, {1, 4},
  83. {2, 4},
  84. {4, 0}
  85. };
  86. Graph1.generator(G, 5);
  87. Graph1.printMatrix();
  88. System.out.println("R<bfs>:"+Graph1.isRouteBFS(2, 3));
  89. System.out.println("R<dfs>:"+Graph1.isRouteDFS(2, 3));
  90. }
  91. }</dfs></bfs></integer></integer></integer></integer>

人气教程排行