Table of content

## Dis-Joint Set in Kotlin | Data Structures

A disjoint-set data structure keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets.

A union-find algorithm performs two useful operations on such a data structure:

• Find: Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset.
• Union : Join two subsets into a single subset.
``````class DisjointSet(val size: Int) {
private val parent = IntArray(size)
private val rank = ByteArray(size)
public var count = size
private set
init {
for (i in parent.indices) {
parent[i] = i
}
}
public fun connected(v: Int, w: Int): Boolean {
return find(v) == find(w)
}
public fun find(v: Int): Int {
var v = v
while (parent[v] != v) {
parent[v] = parent[parent[v]]
v = parent[v]
}
return v
}
public fun union(v: Int, w: Int) {
val rootV = find(v)
val rootW = find(w)
if (rootV == rootW) return
if (rank[rootV] > rank[rootW]) {
parent[rootW] = rootV
} else if (rank[rootW] > rank[rootV]) {
parent[rootV] = rootW
} else {
parent[rootV] = rootW
rank[rootW]++
}
count--
}
}``````