如何在graphshortestpath中添加权重值 - Matlab

时间:2012-05-31 03:45:34

标签: graph-theory matlab

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

已编辑我正在使用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)

1 个答案:

答案 0 :(得分: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.