Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class controllador
- {
- static Set<WordNode> openList = new HashSet<WordNode>();
- public static void main(String[] args)
- {
- generator g = new generator();
- g.generate();
- g.writeToFile();
- Scanner s = new Scanner(System.in);
- try
- {
- BufferedWriter out = new BufferedWriter(new FileWriter("graphtionary.txt"));
- for(WordNode w1 : g.graph)
- {
- for(WordNode w2 : g.graph)
- {
- if(w1 != w2)
- {
- System.out.println(w1 + " " + w2);
- int diff = w1.estimate(w2);
- int neighbors1 = w1.neighbors.size();
- int neighbors2 = w2.neighbors.size();
- double startTime = System.currentTimeMillis();
- boolean success = dijkstraSearch(w1,w2);
- double stopTime = System.currentTimeMillis();
- double time = stopTime-startTime;
- int steps = 0;
- if(success == true) steps = ladderLength(w2);
- else steps = Integer.MAX_VALUE;
- String str = "" + diff + " " + neighbors1 + " " + neighbors2 + " " + time + " " + steps;
- System.out.println(str);
- out.write(str + "\n");
- openList.clear();
- for(WordNode wn : g.graph)
- {
- wn.distance = Integer.MAX_VALUE;
- wn.visited = false;
- wn.parent = null;
- }
- }
- }
- }
- out.close();
- }
- catch (Exception e)
- {
- }
- // while(true)
- // {
- // System.out.print("Starting word: ");
- // String start = s.nextLine();
- // System.out.print("Ending Word: ");
- // String end = s.nextLine();
- //
- // if(start.length() == 5 && end.length() == 5)
- // {
- //
- // double startTime = System.currentTimeMillis();
- // boolean success = dijkstraSearch(g.reference.get(start),g.reference.get(end));
- // double stopTime = System.currentTimeMillis();
- //
- // if(success == false) System.out.println("No Path Found");
- // else printPath(g.reference.get(end));
- //
- // System.out.println(stopTime - startTime);
- // System.out.println(ladderLength(g.reference.get(end)));
- //
- // openList.clear();
- // for(WordNode wn : g.graph)
- // {
- // wn.distance = Integer.MAX_VALUE;
- // wn.visited = false;
- // wn.parent = null;
- // }
- // }
- // else System.out.println("Words must be 5 characters long");
- // }
- }
- public static boolean dijkstraSearch(WordNode start, WordNode end)
- {
- start.distance = 0;
- WordNode current = start;
- while(current != end)
- {
- current.visited = true;
- openList.remove(current);
- for(WordNode wn : current.neighbors)
- {
- if(wn.equals(end))
- {
- wn.parent = current;
- return true;
- }
- else if(!wn.visited)
- {
- openList.add(wn);
- if(current.distance + 1 < wn.distance)
- {
- wn.distance = current.distance + 1;
- wn.parent = current;
- }
- }
- }
- Iterator it = openList.iterator();
- if(it.hasNext())
- {
- WordNode best = (WordNode)it.next();
- while(it.hasNext())
- {
- WordNode temp = (WordNode)it.next();
- if(temp.distance+temp.estimate(end) < best.distance+best.estimate(end)) best = temp;
- }
- current = best;
- }
- else return false;
- }
- return false;
- }
- public static WordNode findClosestNode()
- {
- Iterator it = openList.iterator();
- WordNode best = (WordNode)it.next();
- while(it.hasNext())
- {
- WordNode temp = (WordNode)it.next();
- if(temp.distance < best.distance) best = temp;
- }
- return best;
- }
- public static int ladderLength(WordNode w)
- {
- if(w.parent == null) return 0;
- else return 1+ladderLength(w.parent);
- }
- public static void printPath(WordNode w)
- {
- if(w.parent != null) printPath(w.parent);
- System.out.println(w.word);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement