-
Notifications
You must be signed in to change notification settings - Fork 9
/
adjacency.m
executable file
·116 lines (92 loc) · 2.69 KB
/
adjacency.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function A = adjacency(DATA, TYPE, PARAM, DISTANCEFUNCTION);
% Compute the adjacency graph of the data set DATA
%
% A = adjacency(DATA, TYPE, PARAM, DISTANCEFUNCTION);
%
% DATA - NxK matrix. Data points are rows.
% TYPE - string 'nn' or string 'epsballs'.
% PARAM - integer if TYPE='nn', real number if TYPE='epsballs'.
% DISTANCEFUNCTION - function mapping a (DxM) and a (D x N) matrix
% to an M x N distance matrix (D:dimensionality)
% Returns: A, sparse symmetric NxN matrix of distances between the
% adjacent points.
%
% Example:
%
% A = adjacency(X,'nn',6)
% A contains the adjacency matrix for the data
% set X. For each point, the distances to 6 adjacent points are
% stored. N
%
% Note: the adjacency relation is symmetrized, i.e. if
% point a is adjacent to point b, then point b is also considered to be
% adjacent to point a.
%
%
% Author:
%
% Mikhail Belkin
%
% Modified by: Vikas Sindhwani
% June 2004
% disp('Computing Adjacency Graph');
if (nargin < 3) | (strcmp(TYPE,'nn') & strcmp(TYPE,'epsballs')) | ~isreal(PARAM)
disp(sprintf('ERROR: Too few arguments given or incorrect arguments.\n'));
disp(sprintf('USAGE:\n A = laplacian(DATA, TYPE, PARAM)'));
disp(sprintf('DATA - the data matrix. Data points are rows.'));
disp(sprintf('Nearest neigbors: TYPE =''nn'' PARAM = number of nearest neigbors'));
disp(sprintf('Epsilon balls: TYPE =''epsballs'' PARAM = redius of the ball\n'));
return;
end
n = size(DATA,1);
%disp (sprintf ('DATA: %d points in %d dimensional space.',n,size (DATA,2)));
switch TYPE
case {'nn'}
% disp(sprintf('Creating the adjacency matrix. Nearest neighbors, N=%d.', PARAM));
case{'eps', 'epsballs'}
%disp(sprintf('Creating the adjacency matrix. Epsilon balls, eps=%f.', PARAM));
end;
A = sparse(n,n);
step = 100;
if (strcmp(TYPE,'nn'))
for i1=1:step:n
i2 = i1+step-1;
if (i2> n)
i2=n;
end;
XX= DATA(i1:i2,:);
dt = feval(DISTANCEFUNCTION, XX',DATA');
[Z,I] = sort ( dt,2);
for i=i1:i2
if ( mod(i, 500) ==0)
%disp(sprintf('%d points processed.', i));
end;
for j=2:PARAM+1
A(i,I(i-i1+1,j))= Z(i-i1+1,j);
A(I(i-i1+1,j),i)= Z(i-i1+1,j);
end;
end
end;
% epsilon balls
else
for i1=1:step:n
i2 = i1+step-1;
if (i2> n)
i2=n;
end;
XX= DATA(i1:i2,:);
dt = feval(DISTANCEFUNCTION, XX',DATA');
[Z,I] = sort ( dt,2 );
for i=i1:i2
% if ( mod(i, 500) ==0) disp(sprintf('%d points processed.', i)); end;
j=2;
while ( (Z(i-i1+1,j) < PARAM))
j = j+1;
jj = I(i-i1+1,j);
A(i,jj)= Z(i-i1+1,j);
A(jj,i)= Z(i-i1+1,j);
end;
end
end;
end;