Kim
Kim

Reputation: 15

How to add weighting value in graphshortestpath - Matlab

Currently I understand the use of graphshortestpath in MATLAB. But how can I add weighting value for certain path in the function.

EDITED I'm working on routing system using MATLAB and there are certain path I want to block the path. Those block path will have to go to the other shortest path route.

Is there any example for me to refer??

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)

Upvotes: 0

Views: 1325

Answers (1)

William Chiang
William Chiang

Reputation: 11

% 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.

Upvotes: 1

Related Questions