source: distools/dspaths.m @ 69

Last change on this file since 69 was 10, checked in by bduin, 14 years ago
File size: 1.6 KB
RevLine 
[10]1%DSPATHS  All Shortest Paths in a (Dissimilarity) Graph
2%
3%   D = DSPATHS (A,ALG)
4%
5% INPUT
6%   A   NxN Weight/dissimilarity matrix or dataset
7%   ALG Algorithm used (optional; default: 'F')
8%       'F' Floyd's algorithm; fast in Matlab
9%       'D' Dijkstra's algorithm; slow in Matlab
10%
11% OUTPUT
12%   D   NxN matrix of the shortest paths
13%
14% DESCRIPTION
15% Determines all shortest paths in a (dissimilarity) graph. A is a NxN matrix
16% of weights (or a dissimilarity matrix) representing a graph G(V,E). V is
17% a set of vertices, |V| = N, and E is a set of edges. If there is no edge
18% between i and j then A(i,j) = INF. D is the matrix of the values of shortest
19% paths between all pairs of vertices. Either Floyd's or Dijkstra's algorithm
20% is used.
21%
22% DEFAULT
23% ALG = 'F'
24%
25% SEE ALSO
26% DSPATH
27%
28% LITERATURE
29% Any book on graph algorithms or basic algorithms in computer science.
30
31% Elzbieta Pekalska, ela.pekalska@googlemail.com
32% Faculty of EWI, Delft University of Technology, The Netherlands and
33% School of Computer Science, University of Manchester
34
35function A = dspaths (A,alg)
36
37if nargin < 2,
38  alg = 'F';
39else
40  alg = 'D';
41end
42
43V = +A;
44[n,m] = size(V);
45if n~=m,
46  error('Weight matrix W should be square.')
47end
48
49switch upper(alg)
50  case 'F',
51    for k=1:n
52      V = min(V,repmat(V(k,:),n,1)+repmat(V(:,k),1,n));
53    end
54  case 'D',
55    VV = zeros(n,n);
56    for i=1:n
57      for j=1:n
58        VV(i,j) = dspath(V,i,j);
59      end
60    end
61    V = VV;
62otherwise
63  error('Wrong algorithm.')
64end
65
66if isdataset(A),
67  A = setdata(A,V);
68else
69  A = V;
70end
Note: See TracBrowser for help on using the repository browser.