|
3 | 3 |
|
4 | 4 | public class Kruskal {
|
5 | 5 | public static void main(String[] args) {
|
6 | | - ArrayList<Edge> graphEdges = new ArrayList<Edge>();; //edge list, not adjacency list |
7 | | - graphEdges.add(new Edge(0, 0, 0)); //dummy edge to ignore 0th position in ArrayList |
| 6 | + ArrayList<Edge> graphEdges = new ArrayList<Edge>(); //edge list, not adjacency list |
8 | 7 | graphEdges.add(new Edge(3, 5, 2));
|
9 | 8 | graphEdges.add(new Edge(6, 7, 5));
|
10 | 9 | graphEdges.add(new Edge(3, 4, 6));
|
@@ -35,7 +34,7 @@ public void kruskalMST(ArrayList<Edge> graphEdges, int nodeCount){
|
35 | 34 |
|
36 | 35 | DisjointSet nodeSet = new DisjointSet(nodeCount+1); //Initialize singleton sets for each node in graph. (nodeCount +1) to account for arrays indexing from 0
|
37 | 36 |
|
38 | | - for(int i=1; i<graphEdges.size() && mstEdges.size()<(nodeCount-1); i++){ //loop over all edges. Start @ 1 (ignore 0th as placeholder). Also early termination when number of edges=(number of nodes-1) |
| 37 | + for(int i=0; i<graphEdges.size() && mstEdges.size()<(nodeCount-1); i++){ //loop over all edges. Start @ 1 (ignore 0th as placeholder). Also early termination when number of edges=(number of nodes-1) |
39 | 38 | Edge currentEdge = graphEdges.get(i);
|
40 | 39 | int root1 = nodeSet.find(currentEdge.getVertex1()); //Find root of 1 vertex of the edge
|
41 | 40 | int root2 = nodeSet.find(currentEdge.getVertex2());
|
|
0 commit comments