Shortest path constrained to use specific nodes
4 次查看(过去 30 天)
显示 更早的评论
Let's say I have a simple graph:
G = graph([1,2,3,4,5],[2,3,4,5,1]);
I want to find the shortest path from 1 to 3. The shortestpath function would give me 1-2-3. But in my case, I have a list of nodes that I can travel on:
nodes = [1,3,4,5];
How can I find the shortest path between any two nodes in the graph that only takes the nodes in that list (which will be 1-5-4-3 in this case)?
0 个评论
回答(3 个)
Sulaymon Eshkabilov
2021-4-3
Here is an easy solution:
shortestpath(G, 1,3) % Shortest path between 1 and 3
Good luck.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!