shortest path between two nodes in unweighted graph
3 views (last 30 days)
Show older comments
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
Answers (1)
Walter Roberson
on 12 Oct 2017
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 Comments
Walter Roberson
on 12 Oct 2017
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.
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!