Disjoint set union (DSU)

Revision en3, by snsokolov, 2015-07-31 21:10:55

For the 566D - Restructuring Company

Basic Disjoint Set C++ 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 a){ return a == parent[a] ? a : parent[a] = find(parent[a]); }

    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)