12
12
* https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
13
13
*
14
14
* @author Bartlomiej Drozd <mail@bartlomiejdrozd.pl>
15
+ * @author Justin Wetherell <phishman3579@gmail.com>
15
16
*/
16
17
public class Kruskal {
17
18
18
- private Kruskal () {
19
- }
19
+ private Kruskal () { }
20
20
21
21
public static Graph .CostPathPair <Integer > getMinimumSpanningTree (Graph <Integer > graph ) {
22
22
if (graph == null )
@@ -29,23 +29,22 @@ public static Graph.CostPathPair<Integer> getMinimumSpanningTree(Graph<Integer>
29
29
int cost = 0 ;
30
30
final List <Graph .Edge <Integer >> path = new ArrayList <Graph .Edge <Integer >>();
31
31
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
34
33
HashMap <Graph .Vertex <Integer >, HashSet <Graph .Vertex <Integer >>> membershipMap = new HashMap <Graph .Vertex <Integer >, HashSet <Graph .Vertex <Integer >>>();
35
34
for (Graph .Vertex <Integer > v : graph .getVertices ()) {
36
35
HashSet <Graph .Vertex <Integer >> set = new HashSet <Graph .Vertex <Integer >>();
37
36
set .add (v );
38
37
membershipMap .put (v , set );
39
38
}
40
39
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,
42
41
// it is important that Edge's class comparator is not natural (ex. sorting is from the biggest to the lowest)
43
42
PriorityQueue <Graph .Edge <Integer >> edgeQueue = new PriorityQueue <Graph .Edge <Integer >>(graph .getEdges ());
44
43
45
44
while (!edgeQueue .isEmpty ()) {
46
45
Graph .Edge <Integer > edge = edgeQueue .poll ();
47
46
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
49
48
if (!isTheSamePart (edge .getFromVertex (), edge .getToVertex (), membershipMap )) {
50
49
union (edge .getFromVertex (), edge .getToVertex (), membershipMap );
51
50
path .add (edge );
@@ -64,11 +63,10 @@ private static boolean isTheSamePart(Graph.Vertex<Integer> v1, Graph.Vertex<Inte
64
63
private static void union (Graph .Vertex <Integer > v1 , Graph .Vertex <Integer > v2 , HashMap <Graph .Vertex <Integer >, HashSet <Graph .Vertex <Integer >>> membershipMap ) {
65
64
HashSet <Graph .Vertex <Integer >> firstSet = membershipMap .get (v1 ); //first set is the bigger set
66
65
HashSet <Graph .Vertex <Integer >> secondSet = membershipMap .get (v2 );
67
- HashSet <Graph .Vertex <Integer >> tempSet ;
68
66
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
70
68
if (secondSet .size () > firstSet .size ()) {
71
- tempSet = firstSet ;
69
+ HashSet < Graph . Vertex < Integer >> tempSet = firstSet ;
72
70
firstSet = secondSet ;
73
71
secondSet = tempSet ;
74
72
}
0 commit comments