shortest path between two nodes in unweighted graph

3 views (last 30 days)
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
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
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.
Raj Kishor
Raj Kishor on 12 Oct 2017
Let me tell you about my exact problem. I am calculating betweenness centrality of each node in a network. For that purpose I need to find out how many shortest paths that are passing through a node. I have implemented the following logic and it is working also but the problem is that if there exist two or more shortest path of exactly same length then my program is counting only one of them. I have just taken a network of 6 nodes and 6 edges. A1 and A2 are the source and destination nodes for an edge. num_edges=12; W =ones(1,num_edges);
num_node=6;
A1=[1 3 1 2 2 4 4 3 4 5 5 6];A2=[3 1 2 1 4 2 3 4 5 4 6 5];
DG = sparse(A1,A2,W);
Count=zeros(num_node,1);
for ii=1:num_node-1
for jj=ii+1:num_node
[dist,path] = graphshortestpath(DG,ii,jj);
for kk=1:length(path)
Count(path(kk))=Count(path(kk))+1;
end
end
end
Hope you understood my problem.

Sign in to comment.

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!