We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent adbee2c commit fa804f7Copy full SHA for fa804f7
union-find/quick-find.rb
@@ -0,0 +1,35 @@
1
+class QuickFind
2
+ attr_reader :size, :values
3
+
4
+ def initialize(size)
5
+ @size = size
6
+ @values = Array.new(size)
7
+ @values.each_with_index do |v, i|
8
+ values[i] = i
9
+ end
10
11
12
+ def connected?(p, q)
13
+ values[p] == values[q]
14
15
16
+ def union(p, q)
17
+ pid = values[p]
18
+ qid = values[q]
19
+ (0..@size).each do |i|
20
+ if(values[i] == pid)
21
+ values[i] = qid
22
23
24
25
+end
26
27
+qf = QuickFind.new(10)
28
+p qf.values
29
+qf.union(4, 3)
30
31
+qf.union(9, 8)
32
33
+qf.union(4, 8)
34
35
union-find/quick-union.rb
0 commit comments