使用其他顶点更新测地距离矩阵

时间:2014-11-11 14:29:23

标签: matlab graph distance shortest-path

我有一个为部分无向图计算的测地线ditance矩阵,并希望用一些添加的顶点更新这个矩阵。是否有任何方法可以包含顶点而不需要对每个点进行整个计算?

详细说明,我确实估算了添加顶点的相关加权矩阵,并重新执行最短路径算法,例如: Dijkstra&Floyd-Warshall算法。如果出现问题或遗失,请纠正我。

感谢任何线索和建议......

2 个答案:

答案 0 :(得分:0)

您不必再次计算所有路径,您只需使用新顶点更新距离矩阵。

n成为新的顶点。首先,您需要计算通过每个中间顶点1..(n-1)的每个顶点nk之间的距离(在matlab-ish伪代码中):

//% distance matrix is initially equal to adjacency matrix
distance(n,:) = adjacency(n,:); 
for kk = 1:n-1
   for ii = 1:n-1
      if (distance(ii,kk) + distance(kk,n)) < distance(ii,n)
         distance(ii,n) = distance(ii,kk) + distance(kk,n);
         //% distance(n,ii) = distance(ii,kk) + distance(kk,n); 
         //%   ^^ the above for undirected graphs
         //% also update parent matrix, if used
      end
   end
end

现在,使用顶点n作为新的k中间顶点来更新每个顶点1..n-1和每个其他顶点之间的所有距离。逻辑是相同的,只是调整我们使用的索引:

for ii = 1:n-2
   for jj = 1:n-1  //% or ii+1:n-1 for undirected graphs
      if (distance(ii,n) + distance(n,jj)) < distance(ii,jj)
         distance(ii,jj) = distance(ii,n) + distance(n,jj)
         //% same notes as above
      end
   end
end

正如您所看到的,我们每次都会从原始的Floyd-Warshall算法中删除一个循环,因此重新执行整个算法的时间复杂度为O(|V^2|)而不是O(|V^3|)

答案 1 :(得分:0)

这是我的代码@beaker指出。

//%% function to update geodesic distance matrix with new adjancy matrix  
function Dgwaug = updateGeoDis(W,Dgw)  
//% W: weighted adjancy matrix  
//% Dgw : current geodesics-distance matrix  

nmX = length(Dgw); //% number of vertices in the current graph  
nmXaug = length(W); //% number of vertices in the augmented graph  
nmXnew = nmXaug - nmX; //% number of new vertices to be included

//% initialize the distance matrix for augmented graph
Dgwaug = W;  
Dgwaug(1:nmX,1:nmX) = Dgw;  

//% compute the distances between each vertex 1..nmX and each added vertex 1..nXnew (j) going through each intermediate vertex (i)
for j=1:nmXnew
   for i=1:nmX  
      Dgwaug(1:nmX,nmX+j) = min(Dgwaug(1:nmX,nmX+j),Dgwaug(1:nmX,i) + repmat(Dgwaug(i,nmX+j),[nmX 1]));
      Dgwaug(nmX+j,:) = transpose(Dgwaug(:,nmX+j));
   end  
end  

//% compute the distances between each vertex 1..nmX and each vertex 1..nmX going through the each intermediate added vertex k
for k=1:nmXnew  
   Dgwaug(1:nmX,1:nmX) = min(Dgwaug(1:nmX,1:nmX),repmat(Dgwaug(1:nmX,nmX+k),[1 nmX])+ repmat(Dgwaug(nmX+k,1:nmX),[nmX 1]));      
end

这是我使用整个数据的Floyd-Warshal代码。

//%% function to update geodesic distance matrix with new adjancy matrix  
function Dgw = compGeoDis(W)  
//% W: weighted adjancy matrix  

nmX = length(W); //% number of vertices in the graph  

//% initialize the distance matrix for augmented graph  
Dgwaug = W;  

//% compute the distances between each vertex 1..nmX and each vertex 1..nmX going through the each intermediate vertex k
for k=1:nmX 
   Dgw = min(Dgw,repmat(Dgw(:,k),[1 nmX])+ repmat(Dgw(k,:),[nmX 1]));      
end