FST  openfst-1.7.2
OpenFst Library
union-find.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Union-find algorithm for dense sets of non-negative integers, implemented
5 // using disjoint tree forests with rank heuristics and path compression.
6 
7 #ifndef FST_UNION_FIND_H_
8 #define FST_UNION_FIND_H_
9 
10 #include <stack>
11 #include <vector>
12 
13 namespace fst {
14 
15 // Union-Find algorithm for dense sets of non-negative integers.
16 template <class T>
17 class UnionFind {
18  public:
19  // Creates a disjoint set forest for the range [0; max); 'fail' is a value
20  // indicating that an element hasn't been initialized using MakeSet(...).
21  // The upper bound of the range can be reset (increased) using MakeSet(...).
22  UnionFind(T max, T fail) : parent_(max, fail), rank_(max), fail_(fail) {}
23 
24  // Finds the representative of the set 'item' belongs to, performing path
25  // compression if necessary.
26  T FindSet(T item) {
27  if (item >= parent_.size() || item == fail_ || parent_[item] == fail_) {
28  return fail_;
29  }
30  auto *p = &parent_[item];
31  for (; *p != item; item = *p, p = &parent_[item]) exec_stack_.push(p);
32  for (; !exec_stack_.empty(); exec_stack_.pop()) *exec_stack_.top() = *p;
33  return *p;
34  }
35 
36  // Creates the (destructive) union of the sets x and y belong to.
37  void Union(T x, T y) { Link(FindSet(x), FindSet(y)); }
38 
39  // Initialization of an element: creates a singleton set containing 'item'.
40  // The range [0; max) is reset if item >= max.
41  T MakeSet(T item) {
42  if (item >= parent_.size()) {
43  // New value in parent_ should be initialized to fail_.
44  const auto nitem = item > 0 ? 2 * item : 2;
45  parent_.resize(nitem, fail_);
46  rank_.resize(nitem);
47  }
48  parent_[item] = item;
49  return item;
50  }
51 
52  // Initialization of all elements starting from 0 to max - 1 to distinct sets.
53  void MakeAllSet(T max) {
54  parent_.resize(max);
55  for (T item = 0; item < max; ++item) parent_[item] = item;
56  }
57 
58  private:
59  // Links trees rooted in 'x' and 'y'.
60  void Link(T x, T y) {
61  if (x == y) return;
62  if (rank_[x] > rank_[y]) {
63  parent_[y] = x;
64  } else {
65  parent_[x] = y;
66  if (rank_[x] == rank_[y]) {
67  ++rank_[y];
68  }
69  }
70  }
71 
72  UnionFind(const UnionFind &) = delete;
73 
74  UnionFind &operator=(const UnionFind &) = delete;
75 
76  std::vector<T> parent_; // Parent nodes.
77  std::vector<int> rank_; // Rank of an element = min. depth in tree.
78  T fail_; // Value indicating lookup failure.
79  std::stack<T *> exec_stack_; // Used for path compression.
80 };
81 
82 } // namespace fst
83 
84 #endif // FST_UNION_FIND_H_
void MakeAllSet(T max)
Definition: union-find.h:53
UnionFind(T max, T fail)
Definition: union-find.h:22
void Union(T x, T y)
Definition: union-find.h:37
T MakeSet(T item)
Definition: union-find.h:41
T FindSet(T item)
Definition: union-find.h:26