Skip to content

Commit 6c9873f

Browse files
authored
Merge pull request iam-abbas#3 from srezzag/srezzag-kruskals-algorithm
Add files via upload
2 parents 88d73a5 + f194b56 commit 6c9873f

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package edu.metrostate.ics340.p4.msr812;
2+
3+
public class GraphNode {
4+
5+
private String src;
6+
private String dest;
7+
private int weight;
8+
9+
GraphNode(String src, String dest, int weight) {
10+
this.src = src;
11+
this.dest = dest;
12+
this.weight = weight;
13+
}
14+
15+
public String getSrc() {
16+
return src;
17+
}
18+
19+
public String getDest() {
20+
return dest;
21+
}
22+
23+
public int getWeight() {
24+
return weight;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return this.src + " " + this.dest + " " + this.weight;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Chellcargate|Hamuelland|20
2+
Chellcargate|Dalrayss|85
3+
Dalrayss|Hamuelland|100
4+
Ikmam|Kalvinville|35
5+
Ikmam|Lyhalson|70
6+
Kalvinville|Lyhalson|5
7+
Nevaehgate|Ranaskeep|45
8+
Nevaehgate|Syccvarka|85
9+
Ranaskeep|Syccvarka|100
10+
Hamuelland|Zathysquol|90
11+
Lyhalson|Zathysquol|95
12+
Syccvarka|Zathysquol|20
13+
Ikmam|Ranaskeep|15
14+
Chellcargate|Kalvinville|40
15+
Dalrayss|Nevaehgate|95

0 commit comments

Comments
 (0)