source: distools/distgraph.m @ 10

Last change on this file since 10 was 10, checked in by bduin, 14 years ago
File size: 1.4 KB
Line 
1%DISTGRAPH Compute distances in a graph
2%
3%       D = DISTGRAPH(L,E)
4%
5% INPUT
6%   L   Nx2 array with indices of connected nodes
7%   E   Vector with N corresponding distances
8%       Default: all distances equal to 1
9%
10% OUTPUT
11%   D   Full square distance matrix
12%
13% DESCRIPTION
14% The distances between all nodes in the graph are computed by
15% adding all distances of the connecting nodes. Unconnected
16% nodes have distance INF.
17%
18% SEE ALSO
19% KMST, GRAPHPATH, PLOTGRAPH
20
21% Copyright: R.P.W. Duin, r.p.w.duin@prtools.org
22% Faculty EWI, Delft University of Technology
23% P.O. Box 5031, 2600 GA Delft, The Netherlands
24
25
26function g = distgraph(L,e);
27
28n = size(L,1);
29if nargin < 2
30        e = ones(n,1);
31end
32R = 1e10/max(e);
33e = round(e*R);   % reduce accuracy to avoid bit ripling
34k = max(L(:));    % size of distance matrix (highest node index)
35                  % construct initial distance matrix
36g = repmat(inf,k,k);
37g(1:k+1:k*k) = zeros(1,k);
38                  % substitute given distances
39for j=1:n
40        g(L(j,1),L(j,2)) = e(j);
41end
42h = min(g,g');
43                  % take care that edges are given two-way
44L = [L; fliplr(L)];
45x = [e(:);e(:)];
46                  % loop as long as changes
47while any(h(:) ~= g(:))
48        g = h;
49        for j=1:length(x)
50                h(L(j,1),:) = min(h(L(j,1),:),h(L(j,2),:)+x(j));
51        end
52        h = min(h,h');
53o
54g = h/R;           % reset scale
Note: See TracBrowser for help on using the repository browser.