title: Find all paths of at most length 2 from a set of relationships
body: <p>I have a connection data set with each row marks <code>A</code> connects <code>B</code> in the form <code>A B</code>. The direct connection between <code>A</code> and <code>B</code> appears only once, either in the form <code>A B</code> or <code>B A</code>. I want to find all the connections at most one hop away, i.e. <code>A</code> and <code>C</code> are at most one hop away, if <code>A</code> and <code>C</code> are directly connected, or <code>A</code> connects <code>C</code> through some <code>B</code>.</p>
  
  <p>For example, I have the following direct connection data</p>
  
  <pre><code>1 2
  2 4
  3 7
  4 5
  </code></pre>
  
  <p>Then the resulting data I want is</p>
  
  <pre><code>1 {2,4}
  2 {1,4,5}
  3 {7}
  4 {1,2,5}
  5 {2,4}
  7 {3}
  </code></pre>
  
  <p>Could anybody help me to find a way as efficient as possible? Thank  you.</p>