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 0b23b20

Browse files
DisjointSet cleanup (remove spaces, add curly braces)
1 parent 75cd99b commit 0b23b20

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

‎Kruskal.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,33 @@ public String toString() {
114114
// CONSTRUCTION: with int representing initial number of sets
115115
//
116116
// ******************PUBLIC OPERATIONS*********************
117-
// void union(root1, root2) --> Merge two sets
118-
// int find( x ) --> Return set containing x
117+
// void union(root1, root2) --> Merge two sets
118+
// int find(x) --> Return set containing x
119119
// ******************ERRORS********************************
120120
// No error checking is performed
121121
// http://users.cis.fiu.edu/~weiss/dsaajava3/code/DisjSets.java
122122

123123
/**
124-
* Disjoint set class, using union by rank and path compression.
125-
* Elements in the set are numbered starting at 0.
124+
* Disjoint set class, using union by rank and path compression
125+
* Elements in the set are numbered starting at 0
126126
* @author Mark Allen Weiss
127127
*/
128128
class DisjointSet{
129-
private int [] s; //the set field
130-
129+
private int[] set; //the disjoint set as an array
131130

132131
public int[] getSet(){ //mostly debugging method to print array
133-
return s;
132+
return set;
134133
}
135134

136135
/**
137136
* Construct the disjoint sets object.
138137
* @param numElements the initial number of disjoint sets.
139138
*/
140-
public DisjointSet( int numElements ) { //constructor creates singleton sets
141-
s = new int [ numElements ];
142-
for( int i = 0; i < s.length; i++ ) //initialize to -1 so the trees have nothing in them
143-
s[ i ] = -1;
139+
public DisjointSet(int numElements) { //constructor creates singleton sets
140+
set = new int [numElements];
141+
for(int i = 0; i < set.length; i++){ //initialize to -1 so the trees have nothing in them
142+
set[i] = -1;
143+
}
144144
}
145145

146146
/**
@@ -150,13 +150,15 @@ public DisjointSet( int numElements ) { //constructor creates singleton sets
150150
* @param root1 the root of set 1.
151151
* @param root2 the root of set 2.
152152
*/
153-
public void union( int root1, int root2 ) {
154-
if( s[ root2 ] < s[ root1 ] ) // root2 is deeper
155-
s[ root1 ] = root2; // Make root2 new root
153+
public void union(int root1, int root2) {
154+
if(set[root2] < set[root1]){ // root2 is deeper
155+
set[root1] = root2; // Make root2 new root
156+
}
156157
else {
157-
if( s[ root1 ] == s[ root2 ] )
158-
s[ root1 ]--; // Update height if same
159-
s[ root2 ] = root1; // Make root1 new root
158+
if(set[root1] == set[root2]){
159+
set[root1]--; // Update height if same
160+
}
161+
set[root2] = root1; // Make root1 new root
160162
}
161163
}
162164

@@ -166,10 +168,12 @@ public void union( int root1, int root2 ) {
166168
* @param x the element being searched for.
167169
* @return the set containing x.
168170
*/
169-
public int find(int x) {
170-
if(s[ x] < 0 ) //if tree has no elements, then it is its own root
171+
public int find(int x) {
172+
if(set[x] < 0){ //if tree has no elements, then it is its own root
171173
return x;
172-
else
173-
return s[ x ] = find( s[ x ] );
174+
}
175+
else{ //Recursively find the parent of
176+
return set[x] = find(set[x]);
177+
}
174178
}
175179
}

0 commit comments

Comments
(0)

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