-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisjointSet.m
53 lines (49 loc) · 1.37 KB
/
disjointSet.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
classdef disjointSet < handle
%disjointSet 并查集
% find
% union
properties
set
% best
end
methods
function obj = disjointSet(best,num)
% 构造
% from best 数组
obj.set = zeros(1,num);
for i=1:length(best)
% if best(i)
obj.set(best(i))=best(i);
end
% obj.set = best;
% obj.best = best;
end
function index = findDJ(obj,x)
%findDJ_Naive
% 递归查询
% if x>length(obj.set)
% x
% end
% disp(x)
if obj.set(x)==x || obj.set(x)==0
index = obj.set(x);
return
end
obj.set(x) = findDJ(obj,obj.set(x));
index = obj.set(x);
return
end
function merge(obj, in_1, in_2, neib)
% merge_count=merge_o+1;
if obj.set(in_1)*obj.set(in_2)==0
return
end
if length(neib{in_1})<length(neib{in_2})
[in_1, in_2] = deal(in_2,in_1); % in_1.neib >= in_2.neib
end
obj.set(findDJ(obj,in_2))=findDJ(obj,in_1);
obj.set(in_1) = findDJ(obj,obj.set(in_1));
obj.set(in_2) = findDJ(obj,obj.set(in_2));
end
end
end