Class: DSA::UnionFind
- Inherits:
-
Object
- Object
- DSA::UnionFind
- Defined in:
- lib/dsa-ruby/union_find.rb
Overview
DSA::UnionFind - Disjoint-set data structure with path compression and union by rank.
Tracks a set of elements partitioned into disjoint subsets. Supports efficient union and find operations.
Instance Method Summary collapse
-
#connected?(x, y) ⇒ Boolean
Checks if two elements are in the same set.
-
#count ⇒ Integer
Returns the number of disjoint sets.
-
#find(x) ⇒ Integer
Finds the root of the set containing element x, with path compression.
-
#initialize(n) ⇒ DSA::UnionFind
constructor
Initialize a new UnionFind structure with n disjoint sets.
-
#size ⇒ Integer
Returns the total number of elements.
-
#union(x, y) ⇒ Boolean
Merges the sets containing elements x and y.
Constructor Details
#initialize(n) ⇒ DSA::UnionFind
Initialize a new UnionFind structure with n disjoint sets.
19 20 21 22 23 |
# File 'lib/dsa-ruby/union_find.rb', line 19 def initialize(n) @parent = (0...n).to_a @rank = Array.new(n, 0) @count = n end |
Instance Method Details
#connected?(x, y) ⇒ Boolean
Checks if two elements are in the same set.
81 82 83 |
# File 'lib/dsa-ruby/union_find.rb', line 81 def connected?(x, y) find(x) == find(y) end |
#count ⇒ Integer
Returns the number of disjoint sets.
92 93 94 |
# File 'lib/dsa-ruby/union_find.rb', line 92 def count @count end |
#find(x) ⇒ Integer
Finds the root of the set containing element x, with path compression.
33 34 35 36 37 38 |
# File 'lib/dsa-ruby/union_find.rb', line 33 def find(x) raise IndexError, "index out of bounds" if x < 0 || x >= @parent.size @parent[x] = find(@parent[x]) if @parent[x] != x @parent[x] end |
#size ⇒ Integer
Returns the total number of elements.
102 103 104 |
# File 'lib/dsa-ruby/union_find.rb', line 102 def size @parent.size end |
#union(x, y) ⇒ Boolean
Merges the sets containing elements x and y.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dsa-ruby/union_find.rb', line 50 def union(x, y) raise IndexError, "index out of bounds" if x < 0 || x >= @parent.size raise IndexError, "index out of bounds" if y < 0 || y >= @parent.size root_x = find(x) root_y = find(y) return false if root_x == root_y if @rank[root_x] < @rank[root_y] @parent[root_x] = root_y elsif @rank[root_x] > @rank[root_y] @parent[root_y] = root_x else @parent[root_y] = root_x @rank[root_x] += 1 end @count -= 1 true end |