package algo type Subset struct { parent string rank int } // find is a recursive function that finds the root of the subset that element 'e' belongs to. // It uses path compression technique to optimize future find operations. // The subsets parameter is a map that stores the subsets with their parent information. // The e parameter is the element for which the root is to be found. // The function returns the root of the subset that 'e' belongs to. // see https://ondrej-kvasnovsky-2.gitbook.io/algorithms/finding/union-find-algorithms // or https://jojozhuang.github.io/algorithm/algorithm-union-find/ func find(subsets map[string]*Subset, e string) string { // find root and make root as parent of e // (path compression) if subsets[e].parent != e { subsets[e].parent = find(subsets, subsets[e].parent) } return subsets[e].parent } // Union merges two subsets in the union-find data structure. // It takes a map of subsets, and the names of the two subsets to be merged. // The function finds the root of each subset and performs the union operation based on the rank of the subsets. func Union(subsets map[string]*Subset, x, y string) { xroot := find(subsets, x) yroot := find(subsets, y) // Attach smaller rank tree under root of high // rank tree (Union by Rank) if subsets[xroot].rank < subsets[yroot].rank { subsets[xroot].parent = yroot } else { if subsets[xroot].rank > subsets[yroot].rank { subsets[yroot].parent = xroot } else { // If ranks are same, then make one as root and // increment its rank by one subsets[yroot].parent = xroot subsets[xroot].rank++ } } }