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 608664d

Browse files
Remove dummy edge for node 0
1 parent 29f1122 commit 608664d

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

‎Kruskal.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
public class Kruskal {
55
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
87
graphEdges.add(new Edge(3, 5, 2));
98
graphEdges.add(new Edge(6, 7, 5));
109
graphEdges.add(new Edge(3, 4, 6));
@@ -35,7 +34,7 @@ public void kruskalMST(ArrayList<Edge> graphEdges, int nodeCount){
3534

3635
DisjointSet nodeSet = new DisjointSet(nodeCount+1); //Initialize singleton sets for each node in graph. (nodeCount +1) to account for arrays indexing from 0
3736

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)
3938
Edge currentEdge = graphEdges.get(i);
4039
int root1 = nodeSet.find(currentEdge.getVertex1()); //Find root of 1 vertex of the edge
4140
int root2 = nodeSet.find(currentEdge.getVertex2());

0 commit comments

Comments
(0)

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