|
| 1 | +package edu.metrostate.ics340.p4.msr812; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Comparator; |
| 10 | + |
| 11 | +import com.google.common.graph.Graphs; |
| 12 | +import com.google.common.graph.MutableValueGraph; |
| 13 | +import com.google.common.graph.ValueGraphBuilder; |
| 14 | + |
| 15 | +/* |
| 16 | + * Develop a program that will load in cost estimates for various rail segments between cities. |
| 17 | + * Produce a plan that connects all cities at the minimum construction cost. |
| 18 | + * RailPlanner is designed to take an input of .CSV, pipe-delimited files. |
| 19 | + * Then, performs Kruskal's algorithm on the input and return a value graph of minimum construction costs. |
| 20 | + */ |
| 21 | +public class RailPlanner { |
| 22 | + |
| 23 | + // We place the input in an arraylist to get access to Collections.sort |
| 24 | + ArrayList<GraphNode> inputNodes = new ArrayList<GraphNode>(); |
| 25 | + |
| 26 | + // undirectedGraph, will store and return MST |
| 27 | + private MutableValueGraph<String, Integer> undirectedGraph = ValueGraphBuilder.undirected().allowsSelfLoops(false) |
| 28 | + .build(); |
| 29 | + |
| 30 | + // Total path cost of MST |
| 31 | + private int totalCost = 0; |
| 32 | + |
| 33 | + /* |
| 34 | + * Precondition: file at extimateFilePath must exist and be readable. |
| 35 | + * |
| 36 | + * @param estimateFilePath a file path for the input csv. |
| 37 | + * |
| 38 | + * @returns com.google.common.graph.ValueGraph<String,Integer> undirectedGraph |
| 39 | + * (MST) |
| 40 | + */ |
| 41 | + public com.google.common.graph.ValueGraph<String, Integer> createPlan(String estimateFilePath) { |
| 42 | + // BufferedReader, opens file, scans line by line, creates GraphNodes and inputs |
| 43 | + // in arraylist. |
| 44 | + try (BufferedReader br = new BufferedReader(new FileReader(estimateFilePath))) { |
| 45 | + String line; |
| 46 | + while ((line = br.readLine()) != null) { |
| 47 | + String[] values = line.split("\\|"); |
| 48 | + inputNodes.add(new GraphNode(values[0], values[1], Integer.parseInt(values[2]))); |
| 49 | + // Sort arraylist with Collections.sort comparator. |
| 50 | + sortNodes(inputNodes); |
| 51 | + } |
| 52 | + } catch (FileNotFoundException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } catch (IOException e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * Will input from the sorted ararylist to the undriectedGraph, one at a time. |
| 60 | + * With it, it will also add up the cost that was input. After 1 insertion, it |
| 61 | + * will perform a check to see if the graph contains a cycle. If it does, it |
| 62 | + * will undo the insertion and contiune with the next item in the arraylist. |
| 63 | + */ |
| 64 | + for (int i = 0; i < inputNodes.size(); i++) { |
| 65 | + undirectedGraph.putEdgeValue(inputNodes.get(i).getSrc(), inputNodes.get(i).getDest(), |
| 66 | + inputNodes.get(i).getWeight()); |
| 67 | + totalCost += inputNodes.get(i).getWeight(); |
| 68 | + if (Graphs.hasCycle(undirectedGraph.asGraph())) { |
| 69 | + undirectedGraph.removeEdge(inputNodes.get(i).getSrc(), inputNodes.get(i).getDest()); |
| 70 | + totalCost -= inputNodes.get(i).getWeight(); |
| 71 | + } |
| 72 | + } |
| 73 | + // Returns the MST |
| 74 | + return undirectedGraph; |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + * @returns undirectedGraph.edges().size() the amount of edges in the MST |
| 79 | + */ |
| 80 | + public int getAmountOfEdges() { |
| 81 | + return undirectedGraph.edges().size(); |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + * @returns totalCost the total cost of all edges in the MST. |
| 86 | + */ |
| 87 | + public int getTotalPathCost() { |
| 88 | + return totalCost; |
| 89 | + } |
| 90 | + |
| 91 | + /* |
| 92 | + * Comparator to sort GraphNodes by weighted value. Sorts in ascending order. |
| 93 | + */ |
| 94 | + private void sortNodes(ArrayList<GraphNode> inputNodes) { |
| 95 | + Collections.sort(inputNodes, new Comparator<GraphNode>() { |
| 96 | + @Override |
| 97 | + public int compare(GraphNode g1, GraphNode g2) { |
| 98 | + return g1.getWeight() - g2.getWeight(); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + // Main |
| 104 | + public static void main(String[] args) { |
| 105 | + RailPlanner r1 = new RailPlanner(); |
| 106 | + r1.createPlan("p4_scenario0.csv"); |
| 107 | + r1.getAmountOfEdges(); |
| 108 | + r1.getTotalPathCost(); |
| 109 | + } |
| 110 | +} |
0 commit comments