Advertisement
Guest User

reeee

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.util.*;
  5.  
  6. public class spanning {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. String inputFile = args[0];
  11. double time1 = System.currentTimeMillis();
  12. List<Vertices> cities = readFromFile(inputFile);
  13. System.out.println(System.currentTimeMillis() - time1);
  14. /*for(int i = breakpoint; i < input.length -1; i++) {
  15. //Read from file content
  16. String line = input[i];
  17. //Split line by --
  18. String[] sides = line.split("--");
  19. //get the index of the [ on the right hand side of "--"
  20. int indexOfOpenBracket = sides[1].indexOf("[");
  21. //remove the end "]" to extract the length-integer
  22. int distance = Integer.parseInt(sides[1].substring(indexOfOpenBracket+1 ,sides[1].length() -1));
  23. //remove everything past and including [ in the string to get the city string.
  24. String firstCity = trimQuote(sides[0]);
  25. String secondCity = trimQuote(sides[1].substring(0,indexOfOpenBracket - 1));
  26. //Retrieve city on left hand side and add an edge with destination set to left hand city and the distance specified.
  27. Vertices city = getCityWithName(firstCity, cities);
  28. Vertices cityOther = getCityWithName(secondCity, cities);
  29.  
  30. cityOther.addEdge(new Edge(distance,city, cityOther));
  31. city.addEdge(new Edge(distance, cityOther,city));
  32. }*/
  33. //Prim/kruskals algorithm
  34. //TODO: Implement algorithm for spanning tree.
  35. time1 = System.currentTimeMillis();
  36. System.out.println(primsAlgorithm(cities.get(0),cities));
  37. System.out.println(System.currentTimeMillis() - time1);
  38. }
  39.  
  40. public static int primsAlgorithm(Vertices root, List<Vertices> graph)
  41. {
  42. int sumOfEdges = 0;
  43. //List<Edge> visitedEdges = new ArrayList<>();
  44. List<Vertices> visited = new ArrayList<>(graph.size());
  45. PriorityQueue<Edge> possibleEdges = new PriorityQueue<>(graph.size(),(Edge e1, Edge e2)-> e1.length - e2.length);
  46. //PriorityQueue<Vertices> Q = new PriorityQueue<>(graph.size(), (Vertices v1, Vertices v2)-> v1.shortestDistance - v2.shortestDistance);
  47. visited.add(root);
  48. possibleEdges.addAll(root.connectedEdges);
  49. while(!possibleEdges.isEmpty()) {
  50. if (!visited.contains(possibleEdges.peek().destination) && visited.contains(possibleEdges.peek().origin) || (visited.contains(possibleEdges.peek().destination) && !visited.contains(possibleEdges.peek().origin))) {
  51. Edge e = possibleEdges.poll();
  52. sumOfEdges += e.length;
  53. //visitedEdges.add(e);
  54. //System.out.println(e.length);
  55. visited.add(e.destination);
  56. possibleEdges.addAll(e.destination.connectedEdges);
  57. } else {
  58. possibleEdges.poll();
  59. }
  60. }
  61. return sumOfEdges;
  62. }
  63.  
  64. public static Vertices getCityWithName(String name, List<Vertices> cities)
  65. {
  66. for (Vertices v: cities) {
  67. if(v.getName().equals(name)) {
  68. return v;
  69. }
  70. }
  71. return null;
  72. }
  73. public static List<Vertices> readFromFile(String fileName)
  74. {
  75. try{
  76. BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
  77. List<Vertices> cities = new ArrayList<>();
  78. Vertices prevCity1 = null;
  79. Vertices prevCity2 = null;
  80. String line = null;
  81. double time = System.currentTimeMillis();
  82. while((line = reader.readLine()) != null)
  83. {
  84. if(line.charAt(line.length() -1) == ']')
  85. {
  86. break;
  87. }
  88. line = trimQuote(line);
  89. cities.add(new Vertices(line));
  90. }
  91. System.out.println(System.currentTimeMillis() - time);
  92. while((line = reader.readLine()) != null)
  93. {
  94. String[] citiesArray = line.split("\\[")[0].split("--");
  95. int distance = Integer.parseInt(line.split("\\[")[1].split("\\]")[0]);
  96.  
  97. citiesArray[0] = trimQuote(citiesArray[0]);
  98. citiesArray[1] = trimQuote(citiesArray[1]);
  99.  
  100. Vertices city1 = getCityWithName(citiesArray[0],cities);
  101. Vertices city2 = getCityWithName(citiesArray[1], cities);
  102.  
  103. city1.addEdge(new Edge(distance, city2, city1));
  104. city2.addEdge(new Edge(distance, city1, city2));
  105. }
  106. reader.close();
  107. return cities;
  108. }
  109. catch(Exception e)
  110. {
  111. e.printStackTrace();
  112. System.out.println("Error reading file");
  113. }
  114. return null;
  115. }
  116. public static List<Vertices> getCitiesFromInput(String[] input)
  117. {
  118. List<Vertices> cities = new ArrayList<>();
  119. int counter = 0;
  120. for(String city : input)
  121. {
  122.  
  123. if(city.charAt(city.length() - 1) == ']')
  124. {
  125. break;
  126. }
  127. city = trimQuote(city);
  128. counter++;
  129. cities.add(new Vertices(city));
  130. }
  131. cities.add(new Vertices("" + counter));
  132. return cities;
  133. }
  134.  
  135. public static String trimQuote(String input) {
  136. input = input.trim();
  137.  
  138. if(input.charAt(0) == '"')
  139. {
  140. input = input.substring(1,input.length() -1);;
  141. }
  142. return input;
  143. }
  144.  
  145. }
  146. class Edge {
  147. int length;
  148. Vertices origin;
  149. Vertices destination;
  150. public Edge(int length, Vertices dest, Vertices orig) {
  151. this.origin = orig;
  152. this.length = length;
  153. this.destination = dest;
  154. }
  155. }
  156. class Vertices {
  157. String name;
  158. List<Edge> connectedEdges;
  159.  
  160. public Vertices(String name)
  161. {
  162. this.name = name;
  163. this.connectedEdges = new ArrayList<>();
  164. }
  165.  
  166. public String getName()
  167. {
  168. return this.name;
  169. }
  170.  
  171. public void addEdge(Edge edge)
  172. {
  173. connectedEdges.add(edge);
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement