shortest path between two nodes in unweighted graph
3 次查看(过去 30 天)
显示 更早的评论
Using shortestpath command in matlab2015 version unable to find two or more shortest path of same length in between two nodes(for unweighted graph or graph with same weight). It gives only one of these paths. Can anyone suggest a way to find all such shortest path of same length? Thanks in advance
回答(1 个)
Walter Roberson
2017-10-12
Extract the neighbors of the destination node as a list.
Now run shortestpathtree() between the source and all of those neighbours, giving the 'OutputForm', 'cell' option. Also request the second output, d.
Select only the entries from the cells whose d value is equal to the minimum of the d values.
If you want, explicitly add the destination node to each of the resulting paths.
3 个评论
Walter Roberson
2017-10-12
In that case, extract the neighbors of the destination node. Loop over them calling shortestpath() between the source and each of those destinations and recording the distances as you go -- or, rather, the distance plus the cost of the edge between that destination and the actual final node. Discard all the entries where the distance is not equal to the minimum distance.
... actually I just realized this strategy would fail if the branch was in the middle and that it merged back again with the cost of the branches being equal. I will need to think about that more.
Possibly you could get somewhere with bfsearch() the breadth-first search. However that was introduced in R2015b, and I am not clear as to whether you have R2015a or R2015b.
另请参阅
类别
在 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!