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.



4
5
6
# File 'lib/hashira/duplication/union_find.rb', line 4

def initialize
  @parent = {}
end

Instance Method Details

#clustersObject



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

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

#root(node) ⇒ Object



12
13
14
15
16
# File 'lib/hashira/duplication/union_find.rb', line 12

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



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

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