On matlab, I have an adjacency matrix and using a function, I would like to find out how to plot a histogram showing the degrees of separation between 2 given nodes(up to 10).
As of now I only have a function that finds a node’s neighbours. Basically it’ll be similar to the notion of 6 degrees of separation, except with 10.
Thanks!
function n=neighbour(A,v)
global n;
for i=1:length(v)
a=find(A(:,v(i))+A(v(i),:)');
n=setdiff(a(:)',v(i));
end
end
In general, this is solved using the Floyd–Warshall algorithm, which computes the shortest paths between all pairs of nodes in a graph.
Since you’re using Matlab and because the distance between any two connected nodes is always the same (“1 step”), you could use a trick that involves matrix multiplication: if you have an adjacency matrix
A, then raisingAto theNth power gives you a new matrix that tells you how many paths of lengthNexist between each pair of nodes. So, in a loop, raise A to the 1st power, the 2nd power, etc, and note at which power each element becomes nonzero. The maximum path length is equal to the number of nodes, so you can stop there.