GVSU CIS 263

Week 9 / Day 2

Union of Disjoint sets

// both node numbers and set numbers are integers.
// An array S maps nodes to their parent node.
// Root nodes have a negative value.
int  find(int x) {

    // x is a root.  Return the set name.
    if (S[x] < 0) {
      return x;
    } else {        
      S[x] = find(S[x]);
      return S[x];
    }
}