Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. public static Percurso shortestPathTransbordoNaoClone(Graph <Cais , Linha> g , Cais caisInicio , Cais caisFim , int instanteInicial ){
  2. LinkedList <Cais> listaCais = new LinkedList <> ();
  3. AlgoritmosMap2.shortestPathTransbordos(g, caisInicio, caisFim, listaCais);
  4. System.out.println(listaCais.size());
  5. return new Percurso (listaCais , instanteInicial);
  6. }
  7.  
  8.  
  9. //shortest-path between vOrig and vDest
  10. public static <V, E> double shortestPathTransbordos(Graph<Cais, Linha> g, Cais vOrig, Cais vDest, LinkedList<Cais> shortPath) {
  11. if (!g.validVertex(vDest) || !g.validVertex(vOrig)) {
  12. return 0;
  13. } else if (g.getKey(vDest) == g.getKey(vOrig)) {
  14. shortPath.add(vDest);
  15. return 1;
  16. }
  17. // int nverts = g.numVertices();
  18. // boolean[] visited = new boolean[nverts]; //default value: false
  19. // int[] pathKeys = new int[nverts];
  20. // double[] dist = new double[nverts];
  21. // V[] vertices = g.allkeyVerts();
  22. // shortestPathLength(g, vOrig, vertices, visited, pathKeys, dist);
  23. // int dest = g.getKey(vDest);
  24. // getPath(g, vOrig, vDest, vertices, pathKeys, shortPath);
  25. //
  26. // return dist [dest];
  27. //
  28. shortPath.clear();
  29. ArrayList<LinkedList<Cais>> paths = new ArrayList<>();
  30. ArrayList<Double> dists = new ArrayList<>();
  31. shortestPathsTransbordos(g, vOrig, paths, dists,vDest);
  32.  
  33. // for (Double dist : dists) {
  34. // System.out.println(dist);
  35. // }
  36. // System.out.println("\n");
  37. for (LinkedList<Cais> path : paths) {
  38. if (path.size() > 2) {
  39. if (g.getKey(path.getFirst()) == g.getKey(vOrig) && g.getKey(vDest) == g.getKey(path.getLast())) {
  40. for (Cais v : path) {
  41. shortPath.add(v);
  42. }
  43.  
  44. return dists.get(paths.indexOf(path));
  45. }
  46. }
  47. }
  48. return 0;
  49. }
  50.  
  51. //shortest-path between voInf and all other
  52. public static <V, E> boolean shortestPathsTransbordos(Graph<Cais, Linha> g, Cais vOrig, ArrayList<LinkedList<Cais>> paths, ArrayList<Double> dists,Cais VDest) {
  53.  
  54. if (!g.validVertex(vOrig)) {
  55. return false;
  56. }
  57.  
  58. int nverts = g.numVertices();
  59. boolean[] visited = new boolean[nverts]; //default value: false
  60. int[] pathKeys = new int[nverts];
  61. double[] dist = new double[nverts];
  62. Cais[] vertices = g.allkeyVerts();
  63.  
  64. for (int i = 0; i < nverts; i++) {
  65. dist[i] = Double.MAX_VALUE;
  66. pathKeys[i] = -1;
  67. }
  68.  
  69. AlgoritmosMap2.shortestPathTransbordosMethod(g, vOrig, vertices, visited, pathKeys, dist,vOrig,VDest);
  70.  
  71. dists.clear();
  72. paths.clear();
  73. for (int i = 0; i < nverts; i++) {
  74. paths.add(null);
  75. dists.add(null);
  76. }
  77. for (int i = 0; i < nverts; i++) {
  78. LinkedList<Cais> shortPath = new LinkedList<>();
  79. if (dist[i] != Double.MAX_VALUE) {
  80. getPath(g, vOrig, vertices[i], vertices, pathKeys, shortPath);
  81. }
  82. paths.set(i, shortPath);
  83. dists.set(i, dist[i]);
  84. }
  85. return true;
  86. }
  87. protected static <V, E> void shortestPathTransbordosMethod(Graph<Cais, Linha> g, Cais vOrig, Cais[] vertices,
  88. boolean[] visited, int[] pathKeys, double[] dist,Cais caisog,Cais caisdes) {
  89.  
  90. for (Cais vertice : g.vertices()) {
  91. dist[g.getKey(vOrig)] = Double.MAX_VALUE;
  92. pathKeys[g.getKey(vOrig)] = -1;
  93. visited[g.getKey(vOrig)] = false;
  94. }
  95. dist[g.getKey(vOrig)] = 0;
  96. while (vOrig != null) {
  97. // while(g.getKey(vOrig)!= -1){
  98.  
  99. visited[g.getKey(vOrig)] = true;
  100. for (Cais adjVertice : g.adjVertices(vOrig)) {
  101.  
  102. Edge<Cais,Linha> edge = g.getEdge(vOrig, adjVertice);
  103. double aux=edge.getWeight();
  104. if (!((edge.getVOrig().getEstacao().getNomeEstacao().equals(caisog.getEstacao().getNomeEstacao()))&&(edge.getVDest().getEstacao().getNomeEstacao().equals(caisog.getEstacao().getNomeEstacao())))) {
  105. if (!((edge.getVOrig().getEstacao().getNomeEstacao().equals(caisdes.getEstacao().getNomeEstacao()))&&(edge.getVDest().getEstacao().getNomeEstacao().equals(caisdes.getEstacao().getNomeEstacao())))) {
  106. if (edge.getElement().getIdentificacao().equals("0")) {
  107. aux=TRANSBORDO_HIGH_VALUE;
  108. }
  109. }
  110.  
  111. }
  112. if (!visited[g.getKey(adjVertice)] && dist[g.getKey(adjVertice)] > dist[g.getKey(vOrig)] + aux) {
  113. dist[g.getKey(adjVertice)] = dist[g.getKey(vOrig)] + aux;
  114. pathKeys[g.getKey(adjVertice)] = g.getKey(vOrig);
  115. }
  116. }
  117. vOrig = getVertMinDist(g, dist, visited);
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement