|
| 1 | +/** |
| 2 | + * union find data structure for javascript |
| 3 | + * |
| 4 | + * In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, |
| 5 | + * is a data structure that stores a collection of disjoint (non-overlapping) sets. Equivalently, it stores a partition |
| 6 | + * of a set into disjoint subsets. It provides operations for adding new sets, merging sets (replacing them by their union), |
| 7 | + * and finding a representative member of a set. |
| 8 | + * The last operation allows to find out efficiently if any two elements are in the same or different sets. |
| 9 | + * |
| 10 | + * Disjoint-set data structures play a key role in Kruskal's algorithm for finding the minimum spanning tree of a graph. |
| 11 | + * The importance of minimum spanning trees means that disjoint-set data structures underlie a wide variety of algorithms. |
| 12 | + * In addition, disjoint-set data structures also have applications to symbolic computation, as well in compilers, |
| 13 | + * especially for register allocation problems. |
| 14 | + * |
| 15 | + * you can learn more on disjoint-set / union–find data structure at https://en.wikipedia.org/wiki/Disjoint-set_data_structure |
| 16 | + */ |
| 17 | +function UnionFind (n, key) { |
| 18 | + if (!(this instanceof UnionFind)) return new UnionFind(n) |
| 19 | + if (key && typeof key !== 'function') { |
| 20 | + throw new Error('key has to be a function or else left undefined') |
| 21 | + } |
| 22 | + let cnt, length |
| 23 | + // init Union Find with number of distinct groups. Each group will be referred to as index of the array of size 'size' starting at 0. |
| 24 | + // Provide an optional key function that maps these indices. I.e. for the groups starting with 1 provide function(a){return a-1;}. The default value is function(a){return a;}. |
| 25 | + key = key || function (a) { return a } |
| 26 | + cnt = length = n |
| 27 | + const id = new Array(n) |
| 28 | + const sz = new Array(n) |
| 29 | + for (let i = 0; i < n; i++) { |
| 30 | + id[i] = i |
| 31 | + sz[i] = 1 |
| 32 | + } |
| 33 | + // Returns the number of elements of uf object. |
| 34 | + this.size = function () { |
| 35 | + return length |
| 36 | + } |
| 37 | + // Returns the number of distinct groups left inside the object. |
| 38 | + this.count = function () { |
| 39 | + return cnt |
| 40 | + } |
| 41 | + // Return the root (value) of the group in which p is. |
| 42 | + this.find = function (p) { |
| 43 | + p = key(p) |
| 44 | + while (p !== id[p]) { |
| 45 | + id[p] = id[id[p]] |
| 46 | + p = id[p] |
| 47 | + } |
| 48 | + return p |
| 49 | + } |
| 50 | + // Returns true if p and p are both in same group, false otherwise. |
| 51 | + this.connected = function (p, q) { |
| 52 | + p = key(p) |
| 53 | + q = key(q) |
| 54 | + ensureIndexWithinBounds(p, q) |
| 55 | + return this.find(p) === this.find(q) |
| 56 | + } |
| 57 | + // Combine elements in groups p and q into a single group. In other words connect the two groups. |
| 58 | + this.union = function (p, q) { |
| 59 | + p = key(p) |
| 60 | + q = key(q) |
| 61 | + ensureIndexWithinBounds(p, q) |
| 62 | + const i = this.find(p) |
| 63 | + const j = this.find(q) |
| 64 | + if (i === j) return |
| 65 | + if (sz[i] < sz[j]) { |
| 66 | + id[i] = j; sz[j] += sz[i] |
| 67 | + } else { |
| 68 | + id[j] = i; sz[i] += sz[j] |
| 69 | + } |
| 70 | + cnt-- |
| 71 | + } |
| 72 | + function ensureIndexWithinBounds (args) { |
| 73 | + for (let i = arguments.length - 1; i >= 0; i--) { |
| 74 | + const p = arguments[i] |
| 75 | + if (p >= length) throw new Error('Index out of bounds. The maximum index can be length-1') |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export { UnionFind } |
0 commit comments