forked from dacapo1142/clustering-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksetsplus.m
99 lines (89 loc) · 3.09 KB
/
ksetsplus.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function [ collection, objective ] = ksetsplus(g, n, k, varargin)
pnames={'measure','verbose', 'which_cluster'};
dflts={'distance',false,[]};
[measure, verbose, which_cluster]=internal.stats.parseArgs(pnames, dflts, varargin{:});
addpath('containers');
if which_cluster
collection=DisjointSets(n,k,which_cluster);
else
collection=DisjointSets(n,k);
end
iter_order=1:n;
changed=true;
count=0;
if verbose
tic;
fprintf('------------------------------------------------------------------------------------\n');
fprintf('The function is going to display the changing of the objective value in each run.\n');
switch measure
case 'distance'
objective=0;
for cid=1:k
cluster=collection.cluster(cid);
csize=collection.csize(cid);
gss=g(cluster,cluster);
objective=objective+sum(gss(:))/csize;
end
end
fprintf('The original objective value is: %f\n', objective);
fprintf('------------------------------------------------------------------------------------\n');
end
while changed
count=count+1;
changed=false;
for vid=iter_order
min_adjust_distance=inf;
old_cid=collection.which_cluster(vid);
if collection.csize(old_cid) ==1
continue;
end
for cid=1:k
cluster=collection.cluster(cid);
cluster_size=collection.csize(cid);
switch measure
case 'cohesion'
delta_distance=g(vid,vid)...
-2/cluster_size*sum(g(vid, cluster))...
+1/cluster_size^2*sum(sum(g(cluster,cluster)));
case 'distance'
delta_distance=...
(2*sum(g(vid,cluster)/cluster_size)...
-sum(sum(g(cluster,cluster)))/cluster_size^2);
end
if cid==old_cid
adjust_distance=delta_distance*cluster_size/(cluster_size-1);
else
adjust_distance=delta_distance*cluster_size/(cluster_size+1);
end
if adjust_distance<min_adjust_distance
min_adjust_distance=adjust_distance;
best_cid=cid;
end
end
if best_cid ~= old_cid
collection.move(vid, best_cid);
changed=true;
end
end
if nargout>1
switch measure
case 'distance'
objective=0;
for cid=1:k
cluster=collection.cluster(cid);
csize=collection.csize(cid);
gss=g(cluster,cluster);
objective=objective+sum(gss(:))/csize;
end
end
end
if verbose
fprintf('%f\n',objective);
end
end
if verbose
fprintf('------------------------------------------------------------------------------------\n');
time_consumption=toc;
fprintf('The number of iterations: %d\n', count);
fprintf('Time consumption: %f seconds\n', time_consumption);
end