I am using the MATLAB function graphallshortestpaths to compute shortest paths between vertices of an undirected network. The undirected network is given as a weighted edge list file, which you can find here.
This is the MATLAB code that I use to compute the shortest paths:
A=load('genome_edge_list');
%Extract the edges
E=[A(:,1);A(:,2)];
%Extract the vertices
V=unique(E);
%N is the number of vertices
N=length(V);
%Take the inverse of the weights
A(:,3)=1./A(:,3);
%Create a sparse weighted adjacency matrix
B=sparse(A(:,1),A(:,2),A(:,3),N,N);
%Make B symmetric
B=sparse(full(B)+full(B)');
%Compute shortest paths
D=graphallshortestpaths(B,'directed',false);
Now, the matrix D that MATLAB gives as output is not symmetric. However, since the input to graphallshortestpaths is a symmetric matrix in sparse format, the output ought to be a symmetric matrix. So what am I doing wrong?
The only related question that I could find on mathworks is this question, however in that question the OP clearly is not giving a symmetric matrix as input, which explains why the matrix returned by MATLAB is not symmetric.
EDIT:
To see how far off D and D' are, I computed the following:
E=D';
C=D==E;
find(C==0)
this returns the following linear indices:
33133
543038
1363077
1398421
1398786
1399373
but the values of D and E at those indices are the same, e.g. D(33133)= 0.1024=E(33133). Now, if I take the difference of the two matrices, then I find that the difference at those indices is -1.0000e-05. It therefore seems to be a rounding error, as @beaker points out. However, as I write in my comment below, I don't understand how this can occur, as graphsallshortestpaths computes the distance between node i and j only once, so the values of D(i,j) and D(i,j) should be the result of the same computation.
V=unique(E);
sinceH
is undefined, but I can't see how that could cause the problem even ifH
was already defined elsewhere. – beakerC=D==D.'
andfind(C==0)
gives me a list of six indices for which D and D.' should be different. However, the values for those indices are the same, so I am baffled as to whyissymmetric(D)
returns 0. – n.o.