source: distools/nhgraph.m @ 10

Last change on this file since 10 was 10, checked in by bduin, 14 years ago
File size: 3.1 KB
RevLine 
[10]1%NHGRAPH Find a neighborhood graph and its shortest paths
2%
3%   [NG,L,DL,D] = NHGRAPH(A,OPT,PAR)
4%
5% INPUT
6%   A   NxN Weight (dissimilarity) matrix/dataset representing a weighted graph
7%   OPT Option method (optional; default: 'MST')
8%       'NN'     - based on nearest neighbor neighborhoods
9%       'MST'    - based on minimum spanning trees (MSTs)
10%       'EPS'    - based on eps-neighborhoods
11%       'MST-NN' - based on a single MST followed by nearest neighbor neighborhoods
12%       'MST-EPS'- based on a single MST followed by eps-neighborhoods
13%   PAR Parameter (optional; default: 3 for 'NN' and 'MST-NN'; 1 for 'MST';
14%       0.1*avr(W) for 'EPS' and 'MST-EPS')
15%       Integer K, 1<=K<N, for 'NN', 'MST-NN'
16%       Integer K, 1<=K<N, for 'MST'
17%       Real positive value for 'EPS', 'MST-EPS'
18%
19% OUTPUT
20%   NG  NxN Weight matrix (dataset) representing the neighborhood graph
21%   L   List of edges in the neighborhood graph
22%   DL  List of edge weights of the neighborhood graph
23%   D   NxN matrix of shortest paths
24%
25% DESCRIPTION
26% Finds a neighborhood graph for the graph described by the weight matrix A.
27% A is NxN matrix representing a graph G(V,E). V is a set of vertices, |V| = N,
28% and E is a set of edges. If there is no edge between i and j then A(i,j) = INF.
29%
30% DEFAULT
31%   M   = 'MST'
32%   PAR = 3 ('NN','MST-NN'), 1 ('MST') or 0.1*avr(W) ('EPS','MST-EPS')
33%
34% SEE ALSO
35% KMST, DSPATH, DSPATHS
36
37% Elzbieta Pekalska, ela.pekalska@googlemail.com
38% Faculty of EWI, Delft University of Technology, The Netherlands and
39% School of Computer Science, University of Manchester
40
41
42function [V,L,DL,D] = nhgraph(A,opt,par)
43
44if nargin < 2 | isempty(opt),
45  opt = 'MST';
46end
47
48opt = upper(opt);
49if nargin < 3 | isempty(par),
50  switch opt
51    case 'MST', par = 1;
52    case {'MST','NN','MST-NN'}, par = 3;
53    case {'EPS','MST-EPS'}, par = 0.1*mean(A(:));
54    otherwise
55                        error(['Wrong method', opt]);
56  end
57end
58
59
60V = +A;
61[n,m] = size(V);
62if n~= m,
63  error('Weight matrix A should be square.');
64end
65
66
67L  = [];
68DL = [];
69switch opt
70  case 'MST'
71    [L,DL,V] = kmst(V,par);
72
73  case {'NN', 'MST-NN'}
74    K = par;
75    if K ~= round(K) | K < 2 | K >= n,
76      error ('K should be the number of nearest neighbors.');
77    end
78    if strcmp(opt,'MST-NN'),
79      [L,DL,V] = kmst(V,1);
80    end
81    [VV,I] = sort(V);
82    for i=1:n
83      V(i,I((2+K):end,i)) = inf;
84    end
85    if nargout > 1,
86      for i=1:n
87        L  = [L; i*ones(K,1) I(2:1+K,i)];
88        DL = [DL; VV(i,I(2:1+K,i))'];
89      end
90    end
91
92  case {'EPS','MST-EPS'}
93    if strcmp(opt,'MST-EPS')
94      [L,DL,V] = kmst(V,1);
95    end
96    deps = par;
97    if deps <= 0,
98      error ('EPS should be positive.');
99    end
100    warning off;
101    V = V./(V <= deps);
102    V = min(V,inf);
103    if nargout > 1,
104      [I J] = find(V < inf);
105      L  = [L; I J];
106      DL = [DL; V(J*(n-1)+I)];
107    end
108    warning on;
109  otherwise
110    error('Unknown method.')
111end
112
113LL = [L; L(:,[2 1])];
114V  = inf*ones(n,m);
115V((LL(:,2)-1)*n+LL(:,1)) = +A((LL(:,2)-1)*n+LL(:,1));
116
117if isdataset(A),
118  V = setdata(A,V);
119end
120
121if nargout > 3,
122  D = dspaths(V);
123end
124return;
Note: See TracBrowser for help on using the repository browser.