aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/disjoint_sets/disjoint_sets.h
blob: 57688867046af9800135a52bb7dae2382b24d148 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once

#include <util/system/yassert.h>
#include <util/generic/vector.h>

// Implementation of disjoint-set data structure with union by rank and path compression.
// See http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class TDisjointSets {
public:
    using TElement = size_t;

private:
    mutable TVector<TElement> Parents;
    TVector<size_t> Ranks;
    TVector<size_t> Sizes;
    size_t NumberOfSets;

public:
    TDisjointSets(size_t setCount)
        : Parents(setCount)
        , Ranks(setCount, 0)
        , Sizes(setCount, 1)
        , NumberOfSets(setCount)
    {
        for (size_t i = 0; i < setCount; ++i)
            Parents[i] = i;
    }

    TElement CanonicSetElement(TElement item) const {
        if (Parents[item] != item)
            Parents[item] = CanonicSetElement(Parents[item]);
        return Parents[item];
    }

    size_t SizeOfSet(TElement item) const {
        return Sizes[CanonicSetElement(item)];
    }

    size_t InitialSetCount() const {
        return Parents.size();
    }

    size_t SetCount() const {
        return NumberOfSets;
    }

    void UnionSets(TElement item1, TElement item2) {
        TElement canonic1 = CanonicSetElement(item1);
        TElement canonic2 = CanonicSetElement(item2);
        if (canonic1 == canonic2)
            return;

        --NumberOfSets;
        if (Ranks[canonic1] < Ranks[canonic2]) {
            Parents[canonic1] = canonic2;
            Sizes[canonic2] += Sizes[canonic1];
        } else {
            Parents[canonic2] = canonic1;
            Sizes[canonic1] += Sizes[canonic2];
            Ranks[canonic2] += Ranks[canonic1] == Ranks[canonic2] ? 1 : 0;
        }
    }

    void Expand(size_t setCount) {
        if (setCount < Parents.size()) {
            return;
        }

        size_t prevSize = Parents.size();
        Parents.resize(setCount);
        Ranks.resize(setCount);
        Sizes.resize(setCount);
        NumberOfSets += setCount - prevSize;

        for (size_t i = prevSize; i < setCount; ++i) {
            Parents[i] = i;
            Ranks[i] = 0;
            Sizes[i] = 1;
        }
    }
};