Class: Hashira::Duplication::UnionFind

Inherits:
Object
  • Object
show all
Defined in:
lib/hashira/duplication/union_find.rb

Instance Method Summary collapse

Constructor Details

#initializeUnionFind

Returns a new instance of UnionFind.



6
7
8
# File 'lib/hashira/duplication/union_find.rb', line 6

def initialize
  @parent = {}
end

Instance Method Details

#clustersObject



12
# File 'lib/hashira/duplication/union_find.rb', line 12

def clusters = @parent.keys.group_by { root(it) }.values

#root(node) ⇒ Object



14
15
16
17
18
# File 'lib/hashira/duplication/union_find.rb', line 14

def root(node)
  @parent[node] = node unless @parent.key?(node)
  found = @parent[node]
  found == node ? node : (@parent[node] = root(found))
end

#union(left, right) ⇒ Object



10
# File 'lib/hashira/duplication/union_find.rb', line 10

def union(left, right) = @parent[root(left)] = root(right)