Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.*;
- public class spanning {
- public static void main(String[] args) {
- String inputFile = args[0];
- double time1 = System.currentTimeMillis();
- List<Vertices> cities = readFromFile(inputFile);
- System.out.println(System.currentTimeMillis() - time1);
- /*for(int i = breakpoint; i < input.length -1; i++) {
- //Read from file content
- String line = input[i];
- //Split line by --
- String[] sides = line.split("--");
- //get the index of the [ on the right hand side of "--"
- int indexOfOpenBracket = sides[1].indexOf("[");
- //remove the end "]" to extract the length-integer
- int distance = Integer.parseInt(sides[1].substring(indexOfOpenBracket+1 ,sides[1].length() -1));
- //remove everything past and including [ in the string to get the city string.
- String firstCity = trimQuote(sides[0]);
- String secondCity = trimQuote(sides[1].substring(0,indexOfOpenBracket - 1));
- //Retrieve city on left hand side and add an edge with destination set to left hand city and the distance specified.
- Vertices city = getCityWithName(firstCity, cities);
- Vertices cityOther = getCityWithName(secondCity, cities);
- cityOther.addEdge(new Edge(distance,city, cityOther));
- city.addEdge(new Edge(distance, cityOther,city));
- }*/
- //Prim/kruskals algorithm
- //TODO: Implement algorithm for spanning tree.
- time1 = System.currentTimeMillis();
- System.out.println(primsAlgorithm(cities.get(0),cities));
- System.out.println(System.currentTimeMillis() - time1);
- }
- public static int primsAlgorithm(Vertices root, List<Vertices> graph)
- {
- int sumOfEdges = 0;
- //List<Edge> visitedEdges = new ArrayList<>();
- List<Vertices> visited = new ArrayList<>(graph.size());
- PriorityQueue<Edge> possibleEdges = new PriorityQueue<>(graph.size(),(Edge e1, Edge e2)-> e1.length - e2.length);
- //PriorityQueue<Vertices> Q = new PriorityQueue<>(graph.size(), (Vertices v1, Vertices v2)-> v1.shortestDistance - v2.shortestDistance);
- visited.add(root);
- possibleEdges.addAll(root.connectedEdges);
- while(!possibleEdges.isEmpty()) {
- if (!visited.contains(possibleEdges.peek().destination) && visited.contains(possibleEdges.peek().origin) || (visited.contains(possibleEdges.peek().destination) && !visited.contains(possibleEdges.peek().origin))) {
- Edge e = possibleEdges.poll();
- sumOfEdges += e.length;
- //visitedEdges.add(e);
- //System.out.println(e.length);
- visited.add(e.destination);
- possibleEdges.addAll(e.destination.connectedEdges);
- } else {
- possibleEdges.poll();
- }
- }
- return sumOfEdges;
- }
- public static Vertices getCityWithName(String name, List<Vertices> cities)
- {
- for (Vertices v: cities) {
- if(v.getName().equals(name)) {
- return v;
- }
- }
- return null;
- }
- public static List<Vertices> readFromFile(String fileName)
- {
- try{
- BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
- List<Vertices> cities = new ArrayList<>();
- Vertices prevCity1 = null;
- Vertices prevCity2 = null;
- String line = null;
- double time = System.currentTimeMillis();
- while((line = reader.readLine()) != null)
- {
- if(line.charAt(line.length() -1) == ']')
- {
- break;
- }
- line = trimQuote(line);
- cities.add(new Vertices(line));
- }
- System.out.println(System.currentTimeMillis() - time);
- while((line = reader.readLine()) != null)
- {
- String[] citiesArray = line.split("\\[")[0].split("--");
- int distance = Integer.parseInt(line.split("\\[")[1].split("\\]")[0]);
- citiesArray[0] = trimQuote(citiesArray[0]);
- citiesArray[1] = trimQuote(citiesArray[1]);
- Vertices city1 = getCityWithName(citiesArray[0],cities);
- Vertices city2 = getCityWithName(citiesArray[1], cities);
- city1.addEdge(new Edge(distance, city2, city1));
- city2.addEdge(new Edge(distance, city1, city2));
- }
- reader.close();
- return cities;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- System.out.println("Error reading file");
- }
- return null;
- }
- public static List<Vertices> getCitiesFromInput(String[] input)
- {
- List<Vertices> cities = new ArrayList<>();
- int counter = 0;
- for(String city : input)
- {
- if(city.charAt(city.length() - 1) == ']')
- {
- break;
- }
- city = trimQuote(city);
- counter++;
- cities.add(new Vertices(city));
- }
- cities.add(new Vertices("" + counter));
- return cities;
- }
- public static String trimQuote(String input) {
- input = input.trim();
- if(input.charAt(0) == '"')
- {
- input = input.substring(1,input.length() -1);;
- }
- return input;
- }
- }
- class Edge {
- int length;
- Vertices origin;
- Vertices destination;
- public Edge(int length, Vertices dest, Vertices orig) {
- this.origin = orig;
- this.length = length;
- this.destination = dest;
- }
- }
- class Vertices {
- String name;
- List<Edge> connectedEdges;
- public Vertices(String name)
- {
- this.name = name;
- this.connectedEdges = new ArrayList<>();
- }
- public String getName()
- {
- return this.name;
- }
- public void addEdge(Edge edge)
- {
- connectedEdges.add(edge);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement