Quick Find Algorithm In Javascript
Here is my (quickly thrown together) simple implementation of the quick-find algorithm. For more information on the algorithm and the Disjoint-set data structure (quick-find performs some useful operations on this type of data structure) check out the Wikipedia page on Disjoint-set data structure here.
var numArray;
function QuickFindUF(numToFind) {
numArray = new Array(numToFind);
for (var i = 0; i < numToFind; i++) {
numArray[i] = i;
document.write(numArray[i] + "<br/>");
}
}
function connected(p,q) {
return numArray[p] == numArray[q];
}
function union(p,q) {
var pid = numArray[p];
var qid = numArray[q];
for (var i=0; i < numArray.length();i++){
if (numArray[i] == pid) numArray[i] = qid;
}
}
Written on August 25, 2013