Advertisement
daskalot

Untitled

May 14th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package aps2.shortestpath;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Vector;
  6. class Edge{
  7. String from;
  8. String to;
  9. int weight;
  10. Edge(String f,String s,int w){
  11. from = f;
  12. to = s;
  13. weight = w;
  14. }
  15. }
  16. public class ShortestPath {
  17. boolean negCycle;
  18. Vector<Edge> adj;
  19. Map<String,Integer> idxEdges;
  20. Map<String,Integer> idx;
  21. int shortestDistances[];
  22. String parent[];
  23. int totalNodes, totalEdges;
  24. int INF;
  25. Vector<String> vecNodes;
  26. ShortestPath(){
  27. this.totalNodes = 0;
  28. this.totalEdges = 0;
  29. this.adj = new Vector<Edge>();
  30. this.idxEdges = new HashMap<String,Integer>();
  31. this.idx = new HashMap<String,Integer>();
  32. this.vecNodes = new Vector<String>();
  33. this.INF = Integer.MAX_VALUE;
  34. }
  35. /**
  36. * In this task you need to find a shortest path on a given directed graph
  37. * with arbitrary edge lengths (including negative weights!) from a single
  38. * source node to all other nodes in the graph.
  39. *
  40. * Your algorithm should also detect, if there is a negative cycle in the
  41. * graph and return null in this case.
  42. */
  43.  
  44. /**
  45. * Adds a new node named s to the graph.
  46. *
  47. * @param s Name of the node
  48. * @return True, if a node is unique and successfully added; False otherwise.
  49. */
  50. public boolean addNode(String s) {
  51. if(vecNodes.contains(s)) return false;
  52. idx.put(s,totalNodes);
  53. vecNodes.add(s);
  54. totalNodes++;
  55. return true;
  56. }
  57.  
  58. /**
  59. * Returns names of all nodes in the graph.
  60. *
  61. * @return Names of all nodes in the graph.
  62. */
  63. public Vector<String> getNodes() {
  64. return vecNodes;
  65. }
  66.  
  67. /**
  68. * Adds an edge from node a to node b.
  69. *
  70. * @param a Source node.
  71. * @param b Target node.
  72. * @param w Edge weight. Warning: The weight can also be negative!
  73. */
  74. public void addEdge(String a, String b, int w) {
  75. if(idxEdges.containsKey(a+b)) {
  76. int i = idxEdges.get(a+b);
  77. adj.set(i,new Edge(a,b,w));
  78. }
  79. else {
  80. adj.add(new Edge(a,b,w));
  81. idxEdges.put(a+b,totalEdges);
  82. totalEdges++;
  83. }
  84. }
  85.  
  86. /**
  87. * Computes and locally stores shortest paths from the given origin node
  88. * to all other nodes in the graph.
  89. *
  90. * @param start Origin node.
  91. */
  92. public void computeShortestPaths(String start) {
  93. negCycle = false;
  94. shortestDistances = new int[totalNodes+1];
  95. parent = new String[totalNodes+1];
  96. for(int i=0;i<totalNodes+1;i++) shortestDistances[i] = INF;
  97. for(int i=0;i<totalNodes+1;i++) parent[i] = null;
  98. shortestDistances[idx.get(start)]=0;
  99. parent[idx.get(start)] = start;
  100. for(int i=1;i<totalNodes;i++) {
  101. for(int j=0;j<totalEdges;j++) {
  102. Edge e = adj.elementAt(j);
  103. int from = idx.get(e.from), to = idx.get(e.to);
  104. if(shortestDistances[from]!=INF && shortestDistances[from]+e.weight<shortestDistances[to])
  105. shortestDistances[to] = shortestDistances[from] + e.weight;
  106. parent[to] = e.from;
  107. }
  108. }
  109. for(int i=0;i<totalEdges;i++) {
  110. Edge e = adj.elementAt(i);
  111. int from = idx.get(e.from);
  112. int to = idx.get(e.to);
  113. if(shortestDistances[from]!=INF && shortestDistances[from] + e.weight < shortestDistances[to]) {
  114. negCycle = true;
  115. return;
  116. }
  117. }
  118. }
  119.  
  120. /**
  121. * Returns a list of nodes on the shortest path from the given origin to
  122. * destination node. Returns null, if a path does not exist or there is a
  123. * negative cycle in the graph.
  124. *
  125. * @param start Origin node
  126. * @param dest Destination node
  127. * @return List of nodes on the shortest path from start to dest or null, if the nodes are not connected or there is a negative cycle in the graph.
  128. */
  129. public Vector<String> getShortestPath(String start, String dest) {
  130. computeShortestPaths(start);
  131. System.out.println(idx.get(start)+" "+idx.get(dest));
  132. if(negCycle || shortestDistances[idx.get(dest)] == INF) return null;
  133. Vector<String> res = new Vector<String>();
  134. for(int i=0;i<totalNodes;i++) {
  135. System.out.print(i+":"+idx.get(parent[i])+" ");
  136. }
  137. String current = start;
  138. return null;
  139. }
  140.  
  141. /**
  142. * Returns the distance of the shortest path from the given origin to
  143. * destination node. Returns Integer.MIN_VALUE, if a path does not exist
  144. * or there is a negative cycle in the graph.
  145. *
  146. * @param start Origin node
  147. * @param dest Destination nodes
  148. * @return Distance of the shortest path from start to dest or Integer.MIN_VALUE, if the nodes are not connected or there is a negative cycle in the graph.
  149. */
  150. public int getShortestDist(String start, String dest) {
  151. computeShortestPaths(start);
  152. if(negCycle || shortestDistances[idx.get(dest)] == INF) return Integer.MIN_VALUE;
  153. return shortestDistances[idx.get(dest)];
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement