You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Kruskal.java
+6-5Lines changed: 6 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,15 @@ public class Kruskal {
6
6
privateArrayList<Edge> graphEdges; //edge list, not adjacency list
7
7
8
8
publicstaticvoidmain(String[] args) {
9
-
Kruskalgraph = newKruskal();
9
+
intnodeCount = 8;
10
+
Kruskalgraph = newKruskal(nodeCount); //CAREFUL: nodeCount must be correct. No error checking between nodeCount & graphEdges to see how many nodes actually exist
10
11
graph.kruskalMST(); //run Kruskal's algorithm to find a MST
11
12
}
12
13
13
-
publicKruskal(){
14
-
graphEdges=newArrayList<Edge>();
14
+
publicKruskal(intnodeCount){
15
+
this.nodeCount=nodeCount;
16
+
17
+
graphEdges = newArrayList<Edge>();
15
18
graphEdges.add(newEdge(0, 0, 0)); //dummy edge to ignore 0th position in ArrayList
16
19
graphEdges.add(newEdge(3, 5, 2));
17
20
graphEdges.add(newEdge(6, 7, 5));
@@ -28,8 +31,6 @@ public Kruskal(){
28
31
graphEdges.add(newEdge(2, 3, 24));
29
32
graphEdges.add(newEdge(7, 8, 44)); //I create these in the constructor in "almost sorted order". (Just the final 2 edges should be switched). This is more for ease of input, but I call Collections.sort() on my edge list before the algorithm begins so it doesn't matter
30
33
graphEdges.add(newEdge(6, 5, 30));
31
-
32
-
nodeCount=8; //CAREFUL: nodeCount must be correct. No error checking between nodeCount & graphEdges to see how many nodes actually exist
0 commit comments