[1] | 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 |
|
---|
| 26 | function g = distgraph(L,e);
|
---|
| 27 |
|
---|
| 28 | n = size(L,1);
|
---|
| 29 | if nargin < 2
|
---|
| 30 | e = ones(n,1);
|
---|
| 31 | end
|
---|
| 32 | R = 1e10/max(e);
|
---|
| 33 | e = round(e*R); % reduce accuracy to avoid bit ripling
|
---|
| 34 | k = max(L(:)); % size of distance matrix (highest node index)
|
---|
| 35 | % construct initial distance matrix
|
---|
| 36 | g = repmat(inf,k,k);
|
---|
| 37 | g(1:k+1:k*k) = zeros(1,k);
|
---|
| 38 | % substitute given distances
|
---|
| 39 | for j=1:n
|
---|
| 40 | g(L(j,1),L(j,2)) = e(j);
|
---|
| 41 | end
|
---|
| 42 | h = min(g,g');
|
---|
| 43 | % take care that edges are given two-way
|
---|
| 44 | L = [L; fliplr(L)];
|
---|
| 45 | x = [e(:);e(:)];
|
---|
| 46 | % loop as long as changes
|
---|
| 47 | while 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');
|
---|
| 53 | o
|
---|
| 54 | g = h/R; % reset scale |
---|