title: Finding all edges (and nodes) within X steps from a chosen line in an adjacency matrix in MATLAB
body: <p>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).</p>
  
  <p>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. </p>
  
  <pre><code>  adj_mat = sparse(from_nodes, to_nodes, 1, s, s); 
  </code></pre>
  
  <p>Is there a way I can to this using the adjacency matrix? Can I do it more efficiently using the to/from list?</p>
  
  <p>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. </p>
  
  <p>The code I use now looks something like this:</p>
  
  <pre><code>  % 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
  </code></pre>
  
  <p>Thank you!</p>