-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfloydwarshall.m
78 lines (72 loc) · 1.72 KB
/
floydwarshall.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
function [D,P] = floydwarshall(A)
% FLOYDWARSHALL Compute all shortest paths using the Floyd-Warshall algorithm
%
% D=floydwarshall(A) returns the shortest distance matrix between all pairs
% of nodes in the graph A. If A has a negative weight cycle, then this
% algorithm will throw an error.
%
% [D,P]=floydwarshall(A) also returns the matrix of predecessors.
%
% See also DIJKSTRA
%
% Example:
% load_gaimc_graph('all_shortest_paths_example')
% D = floydwarshall(A)
% David F. Gleich
% Copyright, Stanford University, 2009
% History
% 2008-05-23: Initial coding
if isstruct(A),
rp=A.rp; ci=A.ci; ai=A.ai;
[ri ci ai]=csr_to_sparse(rp,ci,ai);
n = length(rp)-1;
else
[ri ci ai]=find(A);
n = size(A,1);
end
nz = length(ai);
computeP = nargout>1;
D = Inf*ones(n,n);
if computeP
P = zeros(n,n);
% initialize the distance and predecessor matrix
for ei=1:nz
i=ri(ei);
j=ci(ei);
v=ai(ei);
if v<D(i,j)
D(i,j)=v;
P(i,j)=i;
end
end
for i=1:n, D(i,i) = 0; end % set diagonal to 0
for k=1:n
for i=1:n
for j=1:n
if D(i,k)+D(k,j)<D(i,j)
D(i,j)=D(i,k)+D(k,j);
P(i,j)=P(k,j);
end
end
end
end
else
% initialize the distance matrix
for ei=1:nz
i=ri(ei);
j=ci(ei);
v=ai(ei);
D(i,j)=min(D(i,j),v);
end
for i=1:n, D(i,i) = 0; end % set diagonal to 0
for k=1:n
for i=1:n
for j=1:n
D(i,j)=min(D(i,j),D(i,k)+D(k,j));
end
end
end
end
if any(diag(D))<0
warning('floydwarshall:negativeCycle','negative weight cycle detected');
end