Advertisement
Guest User

Untitled

a guest
May 26th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import CITS2200.*;
  4.  
  5. public class CITS2200ProjectTester {
  6. public static void loadGraph(CITS2200Project project, String path) {
  7. // The graph is in the following format:
  8. // Every pair of consecutive lines represent a directed edge.
  9. // The edge goes from the URL in the first line to the URL in the second line.
  10. try {
  11. BufferedReader reader = new BufferedReader(new FileReader(path));
  12. while (reader.ready()) {
  13. String from = reader.readLine();
  14. String to = reader.readLine();
  15. System.out.println("Adding edge from " + from + " to " + to);
  16. project.addEdge(from, to);
  17. }
  18. } catch (Exception e) {
  19. System.out.println("There was a problem:");
  20. System.out.println(e.toString());
  21. }
  22. }
  23.  
  24. public static void main(String[] args) {
  25. // Change this to be the path to the graph file.
  26. String pathToGraphFile = "E:\\lab2\\example_graph.txt";
  27. // Create an instance of your implementation.
  28. CITS2200Project proj = new CITS2200Project();
  29. // Load the graph into the project.
  30. loadGraph(proj, pathToGraphFile);
  31.  
  32. proj.getShortestDistance("/wiki/Flow_network", "/wiki/Braess%27_paradox");
  33. proj.getShortestDistance("/wiki/Flow_network", "/wiki/Minimum-cost_flow_problem");
  34. proj.hamlitonianPath("/wiki/Flow_network");
  35. }
  36. }
  37.  
  38. class CITS2200Project {
  39. static HashMap<String, Vert> vertMap;
  40. public int mapSize, pathSize;
  41. public Vert[] path;
  42. public Vert pathStart;
  43.  
  44. public CITS2200Project() {
  45. this.vertMap = new HashMap<String, Vert>();
  46. this.mapSize = 0;
  47. this.pathSize = 0;
  48. }
  49.  
  50. public void addEdge(String source, String dest) {
  51. Vert v = getVert(source);
  52. Vert w = getVert(dest);
  53. v.edgeList.add(w);
  54.  
  55. }
  56.  
  57. public Vert getVert(String vertexname) {
  58. Vert v = vertMap.get(vertexname);
  59. if (v == null) {
  60. v = new Vert(vertexname);
  61. vertMap.put(vertexname, v);
  62. mapSize++;
  63. }
  64. return v;
  65. }
  66.  
  67. public int getShortestDistance(String source, String dest) {
  68. HashMap<Vert, Integer> distance = new HashMap<Vert, Integer>();
  69. LinkedList<Vert> uncheckedVert = new LinkedList<Vert>();
  70. ArrayList<Vert> checkedVert = new ArrayList<Vert>();
  71. Vert start = vertMap.get(source);
  72. Vert end = vertMap.get(dest);
  73. if (start == null) {
  74. throw new Underflow("Source not in graph");
  75. }
  76. if (end == null) {
  77. throw new Underflow("Destination not in graph");
  78. }
  79.  
  80. distance.put(start, 0);
  81. uncheckedVert.add(start);
  82. while (!uncheckedVert.isEmpty()) {
  83. Vert vertex = uncheckedVert.removeLast();
  84. if (checkedVert.contains(vertex)) {
  85. continue;
  86. }
  87. checkedVert.add(vertex);
  88.  
  89. for (Vert neighbour : vertex.edgeList) {
  90. int current = distance.get(vertex) + 1;
  91. if (distance.get(neighbour) == null) {
  92. distance.put(neighbour, current);
  93. if (neighbour == end) {
  94. System.out.println("The distance between " + source + " and " + dest + " is " + distance.get(end));
  95. return distance.get(end);
  96. }
  97. }
  98. }
  99. }
  100. System.out.println("You cannot reach " + dest + " from " + source);
  101. return -1;
  102. }
  103.  
  104. public Vert[] hamlitonianPath(String startpoint) {
  105. path = new Vert[mapSize];
  106. if (mapSize > 20) {
  107. System.out.println("Graph too big to find Hamiltonian Path");
  108. return null;
  109. }
  110. Vert start = vertMap.get(startpoint);
  111. if (start == null) {
  112. throw new Underflow("Source not in graph");
  113. }
  114. pathStart = start;
  115. try {
  116. path[0] = start;
  117. pathSize = 1;
  118. solvePath(start);
  119. } catch (Exception e) {
  120. System.out.println("There was a problem:");
  121. System.out.println(e.toString());
  122. }
  123. if (pathSize == mapSize) {
  124. if (path[pathSize].edgeList.contains(pathStart)) {
  125. System.out.println("Path found: ");
  126. for (int i = 0; i < mapSize; i++) {
  127. System.out.println(path[i].getID());
  128. }
  129. return path;
  130. }
  131. }
  132. System.out.println("No path found");
  133. return path;
  134. }
  135.  
  136. public void solvePath(Vert vertex) {
  137. if (pathSize == mapSize) {
  138. if (path[pathSize].edgeList.contains(pathStart)) {
  139. return;
  140. }
  141. }
  142. for (Vert neighbour : vertex.edgeList) {
  143. path[pathSize] = neighbour;
  144. pathSize++;
  145. if (!isPresent(neighbour)) {
  146. solvePath(neighbour);
  147. }
  148. if (pathSize == mapSize) {
  149. if (path[pathSize].edgeList.contains(pathStart)) {
  150. return;
  151. }
  152. }
  153. path[pathSize] = null;
  154. pathSize--;
  155. }
  156. }
  157.  
  158. public boolean isPresent(Vert v){
  159. for (int i = 0; i < pathSize - 1; i++){
  160. if(path[i] == v){
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. }
  167.  
  168.  
  169. class Vert {
  170. final private String id;
  171. public ArrayList<Vert> edgeList;
  172.  
  173.  
  174. public Vert(String id) {
  175. this.id = id;
  176. this.edgeList = new ArrayList<Vert>();
  177. }
  178.  
  179. public String getID() {
  180. return id;
  181. }
  182.  
  183.  
  184. @Override
  185. public String toString() {
  186. return id;
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement