필터 지우기
필터 지우기

How to compute shortest path in Graph ?

조회 수: 2 (최근 30일)
charu shree
charu shree 2023년 4월 20일
댓글: Walter Roberson 2023년 4월 21일
Hello all, I am trying to compute shortest path in a following graph but not getting it correctly.
Suppose there are 15 small mobile towers which can act as source as well as target and 1 Big mobile tower which will only act as target and are modelled as below:
My query is how to compute the shortest path between sources (sr) and targets (ta) in this Graph. Any help in this regard will be highly appreciated.
diff_smt = 15; % different number of small mobile towers
bmt = 1; % only one big mobile tower which is target
idx_bmt = diff_smt +1; % index of big mobile tower (target)
sr = [1 1 1 2 2 2 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 12 12 13 13 13 14 14 15 15 15]; % various possible sources
ta=randi([2,16],1,length(sr)); % various possible targets
G = graph(sr,ta);
plot (G);

답변 (1개)

Matt J
Matt J 2023년 4월 20일
You can use the shortestpath command.
  댓글 수: 13
Matt J
Matt J 2023년 4월 21일
편집: Matt J 2023년 4월 21일
If it is okay and your question has been answered, please do Accept-click the answer.
Walter Roberson
Walter Roberson 2023년 4월 21일
sr = [1 2 2 2 3 3 3 4 5];
ta = [2 3 6 8 4 6 7 6 6];
G = graph(sr, ta);
plot(G)
But by construction each node in sr is connected to each node in ta, so the distance between them is always 1.
If you were asking for the path between each node and every other node then that would be do-able by looping with shortestpathtree
nodes = 1 : 8;
numnodes = length(nodes);
for idx = 1 : numnodes
focus = nodes(idx);
TR = shortestpathtree(G, focus, setdiff(nodes, focus));
figure
p = plot(G);
highlight(p, TR, 'EdgeColor', 'r');
title("node " + focus)
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by