I have a nx2 matrix of to-from nodes for a large network structure. I have used this to create a sparse adjacency matrix which I can plot using BIOGRAPH. My systems varies in size, the largest ones having more than 3000 nodes (obviously not suitable for plotting).
If I choose a line, I want to be able to create a list of all lines and nodes that are within X "steps" from the original line (two nodes), for a given X (typically 3). It's clearly not too difficult using brute-force. However, I need to do this as quick as possible.
adj_mat = sparse(from_nodes, to_nodes, 1, s, s);
Is there a way I can to this using the adjacency matrix? Can I do it more efficiently using the to/from list?
What I do now is finding the indices for the nodes connected to the chosen line, then search through the entire list of to-from nodes and finding all lines where either the to/from element is equal to one of the nodes of the chosen line. Then I use the new list of nodes and search through the entire to/from list, searching for these nodes again.
The code I use now looks something like this:
% tempBranch = the branches connected to the list of the current branches
k = 1;
for i = 1:nnz(nodeList) % number of after step X-1 (for X=0 this is
% equal to the nodes connected to the chosen line
for j = 1:n % n = number of lines
if branchList(j,1) == nodeList(i) || branchList(j,2) == nodeList(i)
tempBranch(k) = j;
k = k + 1;
end
end
end
Thank you!
A good starting point is to find all the nodes that are less than k
edge away from the two given nodes i
and j
. This is very easy using the adjacency matrix that you have built.
A
.v
of all 0
, excepted on the components i
and j
, where you put 1
.A^k*v
. All the nodes for which the entry is non-zero are within k
edges from the two starting points (remark that the value of the entry is the number of k-paths!). You can automate the extraction of these indices with the find
function.This should be quite efficient!
From the nodes, I think that it should be easy to find the edges that you are looking for.
Hope it helps!