Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 75cd99b

Browse files
Constructor pass in nodeCount
1 parent ee9a6af commit 75cd99b

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

‎Kruskal.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ public class Kruskal {
66
private ArrayList<Edge> graphEdges; //edge list, not adjacency list
77

88
public static void main(String[] args) {
9-
Kruskal graph = new Kruskal();
9+
int nodeCount = 8;
10+
Kruskal graph = new Kruskal(nodeCount); //CAREFUL: nodeCount must be correct. No error checking between nodeCount & graphEdges to see how many nodes actually exist
1011
graph.kruskalMST(); //run Kruskal's algorithm to find a MST
1112
}
1213

13-
public Kruskal(){
14-
graphEdges=new ArrayList<Edge>();
14+
public Kruskal(int nodeCount){
15+
this.nodeCount=nodeCount;
16+
17+
graphEdges = new ArrayList<Edge>();
1518
graphEdges.add(new Edge(0, 0, 0)); //dummy edge to ignore 0th position in ArrayList
1619
graphEdges.add(new Edge(3, 5, 2));
1720
graphEdges.add(new Edge(6, 7, 5));
@@ -28,8 +31,6 @@ public Kruskal(){
2831
graphEdges.add(new Edge(2, 3, 24));
2932
graphEdges.add(new Edge(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
3033
graphEdges.add(new Edge(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
3334
}
3435

3536
public void kruskalMST(){

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /