Disjoint set union (DSU)

Revision en1, by snsokolov, 2015-07-31 13:03:32

For the 566D - Restructuring Company

Basic Disjoint Set implementation (w/o ranks) using int vector

class DisjointSet{ public:

    vector <int> parent;

    DisjointSet(int n): parent(n) { for(int i=0; i<n; i++) parent[i] = i; }

    void join(int a, int b) { parent[find(b)] = find(a); }

    int find(int n){
        int par = parent[n];
        if (par != n) par = parent[n] = find(par);
        return par;
    }

    bool check(int a, int b){ return find(a) == find(b); }
};
Tags dsu, c++, disjoint set

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English snsokolov 2015-07-31 21:10:55 82
en2 English snsokolov 2015-07-31 18:40:10 4 Tiny change: 'joint Set implement' -> 'joint Set C++ implement'
en1 English snsokolov 2015-07-31 13:03:32 531 Initial revision (published)