0

目前我了解graphshortestpath在 MATLAB 中的使用。但是如何为函数中的某些路径添加权重值。

已编辑我正在使用 MATLAB 开发路由系统,并且我想阻止某些路径。那些块路径将不得不去另一个最短路径路径。

有什么例子可以参考吗??

W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W);
UG = tril(DG + DG');
h = view(biograph(DG,[],'ShowWeights','on'));
[dist,path,pred] = graphshortestpath(DG,1,6);
set(h.Nodes(path),'Color',[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)
4

1 回答 1

1
% path weights,if you want to block
% one edge,you could set the value very large.
W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];

DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W)

DG =
   (4,1)       0.4500
   (6,2)       0.4100
   (2,3)       0.5100
   (5,3)       0.3200
   (6,3)       0.2900
   (3,4)       0.1500
   (5,4)       0.3600
   (1,5)       0.2100
   (2,5)       0.3200
   (1,6)       0.9900
   (4,6)       0.3800


[dist,path,pred] = graphshortestpath(DG,1,6)
%Find the shortest path in the graph from node 1 to node 6.
于 2012-05-31T06:53:59.120 回答