xrrain
xrrain

Reputation: 21

How can I get all the existing shortest paths between two single nodes for graph using MATLAB

I know that there are many functions can compute the distance between two nodes, and there are some functions can return the shortest path between two nodes in MATLAB, however, I find all the functions seem only return the first shortest path between two nodes, but I want to find a way to get all the existing shortest paths between two single nodes for graph. For instance, if we have a graph: (1,2), (1,3), (2,4), (3,4), when I query the shortest path between 1 and 4, it should return (1,2,4) and (1,3,4). Thanks!

Upvotes: 2

Views: 562

Answers (1)

Alex Taylor
Alex Taylor

Reputation: 1412

I'd suggest using the graph class in MATLAB, which has a shortestpath method which defines exactly the operation you are describing.

https://www.mathworks.com/help/matlab/ref/graph.html

https://www.mathworks.com/help/matlab/ref/graph.shortestpath.html

If you are working with a directed graph, same thing applies, but you'd use the digraph class instead.

Or, if you desire the shortest paths between all nodes in the graph, the distances method will do that:

https://www.mathworks.com/help/matlab/ref/graph.distances.html

Upvotes: 0

Related Questions