Skip to content

Commit cab37ec

Browse files
committed
Some code clean-up in Kruskal
1 parent 56812cc commit cab37ec

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/com/jwetherell/algorithms/graph/Kruskal.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
* https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
1313
*
1414
* @author Bartlomiej Drozd <mail@bartlomiejdrozd.pl>
15+
* @author Justin Wetherell <phishman3579@gmail.com>
1516
*/
1617
public class Kruskal {
1718

18-
private Kruskal() {
19-
}
19+
private Kruskal() { }
2020

2121
public static Graph.CostPathPair<Integer> getMinimumSpanningTree(Graph<Integer> graph) {
2222
if (graph == null)
@@ -29,23 +29,22 @@ public static Graph.CostPathPair<Integer> getMinimumSpanningTree(Graph<Integer>
2929
int cost = 0;
3030
final List<Graph.Edge<Integer>> path = new ArrayList<Graph.Edge<Integer>>();
3131

32-
33-
// prepare data to store information which part of tree given vertex is
32+
// Prepare data to store information which part of tree given vertex is
3433
HashMap<Graph.Vertex<Integer>, HashSet<Graph.Vertex<Integer>>> membershipMap = new HashMap<Graph.Vertex<Integer>, HashSet<Graph.Vertex<Integer>>>();
3534
for (Graph.Vertex<Integer> v : graph.getVertices()) {
3635
HashSet<Graph.Vertex<Integer>> set = new HashSet<Graph.Vertex<Integer>>();
3736
set.add(v);
3837
membershipMap.put(v, set);
3938
}
4039

41-
// we make queue of edges to consider all of them, starting with edge with the lowest cost
40+
// We make queue of edges to consider all of them, starting with edge with the lowest cost,
4241
// it is important that Edge's class comparator is not natural (ex. sorting is from the biggest to the lowest)
4342
PriorityQueue<Graph.Edge<Integer>> edgeQueue = new PriorityQueue<Graph.Edge<Integer>>(graph.getEdges());
4443

4544
while (!edgeQueue.isEmpty()) {
4645
Graph.Edge<Integer> edge = edgeQueue.poll();
4746

48-
// if from vertex and to vertex are from different parts of tree then add this edge to result and union vertices' parts
47+
// If from vertex and to vertex are from different parts of tree then add this edge to result and union vertices' parts
4948
if (!isTheSamePart(edge.getFromVertex(), edge.getToVertex(), membershipMap)) {
5049
union(edge.getFromVertex(), edge.getToVertex(), membershipMap);
5150
path.add(edge);
@@ -64,11 +63,10 @@ private static boolean isTheSamePart(Graph.Vertex<Integer> v1, Graph.Vertex<Inte
6463
private static void union(Graph.Vertex<Integer> v1, Graph.Vertex<Integer> v2, HashMap<Graph.Vertex<Integer>, HashSet<Graph.Vertex<Integer>>> membershipMap) {
6564
HashSet<Graph.Vertex<Integer>> firstSet = membershipMap.get(v1); //first set is the bigger set
6665
HashSet<Graph.Vertex<Integer>> secondSet = membershipMap.get(v2);
67-
HashSet<Graph.Vertex<Integer>> tempSet;
6866

69-
// we want to include smaller set into bigger so second set cannot be bigger than first
67+
// we want to include smaller set into bigger, so second set cannot be bigger than first
7068
if (secondSet.size() > firstSet.size()) {
71-
tempSet = firstSet;
69+
HashSet<Graph.Vertex<Integer>> tempSet = firstSet;
7270
firstSet = secondSet;
7371
secondSet = tempSet;
7472
}

0 commit comments

Comments
 (0)