Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class controllador
  6. {
  7.  
  8. static Set<WordNode> openList = new HashSet<WordNode>();
  9.  
  10. public static void main(String[] args)
  11. {
  12.  
  13. generator g = new generator();
  14. g.generate();
  15. g.writeToFile();
  16.  
  17. Scanner s = new Scanner(System.in);
  18.  
  19.  
  20.  
  21. try
  22. {
  23. BufferedWriter out = new BufferedWriter(new FileWriter("graphtionary.txt"));
  24.  
  25. for(WordNode w1 : g.graph)
  26. {
  27. for(WordNode w2 : g.graph)
  28. {
  29. if(w1 != w2)
  30. {
  31. System.out.println(w1 + " " + w2);
  32. int diff = w1.estimate(w2);
  33. int neighbors1 = w1.neighbors.size();
  34. int neighbors2 = w2.neighbors.size();
  35.  
  36. double startTime = System.currentTimeMillis();
  37. boolean success = dijkstraSearch(w1,w2);
  38. double stopTime = System.currentTimeMillis();
  39.  
  40. double time = stopTime-startTime;
  41.  
  42. int steps = 0;
  43. if(success == true) steps = ladderLength(w2);
  44. else steps = Integer.MAX_VALUE;
  45.  
  46. String str = "" + diff + " " + neighbors1 + " " + neighbors2 + " " + time + " " + steps;
  47. System.out.println(str);
  48.  
  49. out.write(str + "\n");
  50.  
  51. openList.clear();
  52. for(WordNode wn : g.graph)
  53. {
  54. wn.distance = Integer.MAX_VALUE;
  55. wn.visited = false;
  56. wn.parent = null;
  57. }
  58. }
  59. }
  60. }
  61. out.close();
  62. }
  63. catch (Exception e)
  64. {
  65.  
  66. }
  67.  
  68.  
  69.  
  70. // while(true)
  71. // {
  72. // System.out.print("Starting word: ");
  73. // String start = s.nextLine();
  74. // System.out.print("Ending Word: ");
  75. // String end = s.nextLine();
  76. //
  77. // if(start.length() == 5 && end.length() == 5)
  78. // {
  79. //
  80. // double startTime = System.currentTimeMillis();
  81. // boolean success = dijkstraSearch(g.reference.get(start),g.reference.get(end));
  82. // double stopTime = System.currentTimeMillis();
  83. //
  84. // if(success == false) System.out.println("No Path Found");
  85. // else printPath(g.reference.get(end));
  86. //
  87. // System.out.println(stopTime - startTime);
  88. // System.out.println(ladderLength(g.reference.get(end)));
  89. //
  90. // openList.clear();
  91. // for(WordNode wn : g.graph)
  92. // {
  93. // wn.distance = Integer.MAX_VALUE;
  94. // wn.visited = false;
  95. // wn.parent = null;
  96. // }
  97. // }
  98. // else System.out.println("Words must be 5 characters long");
  99. // }
  100. }
  101.  
  102. public static boolean dijkstraSearch(WordNode start, WordNode end)
  103. {
  104. start.distance = 0;
  105. WordNode current = start;
  106. while(current != end)
  107. {
  108. current.visited = true;
  109. openList.remove(current);
  110. for(WordNode wn : current.neighbors)
  111. {
  112. if(wn.equals(end))
  113. {
  114. wn.parent = current;
  115. return true;
  116. }
  117. else if(!wn.visited)
  118. {
  119. openList.add(wn);
  120. if(current.distance + 1 < wn.distance)
  121. {
  122. wn.distance = current.distance + 1;
  123. wn.parent = current;
  124. }
  125. }
  126. }
  127.  
  128. Iterator it = openList.iterator();
  129. if(it.hasNext())
  130. {
  131. WordNode best = (WordNode)it.next();
  132. while(it.hasNext())
  133. {
  134.  
  135. WordNode temp = (WordNode)it.next();
  136. if(temp.distance+temp.estimate(end) < best.distance+best.estimate(end)) best = temp;
  137. }
  138.  
  139. current = best;
  140. }
  141. else return false;
  142. }
  143. return false;
  144. }
  145.  
  146. public static WordNode findClosestNode()
  147. {
  148. Iterator it = openList.iterator();
  149. WordNode best = (WordNode)it.next();
  150. while(it.hasNext())
  151. {
  152.  
  153. WordNode temp = (WordNode)it.next();
  154. if(temp.distance < best.distance) best = temp;
  155. }
  156. return best;
  157. }
  158.  
  159. public static int ladderLength(WordNode w)
  160. {
  161. if(w.parent == null) return 0;
  162. else return 1+ladderLength(w.parent);
  163. }
  164.  
  165. public static void printPath(WordNode w)
  166. {
  167. if(w.parent != null) printPath(w.parent);
  168. System.out.println(w.word);
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement