title: Divide the number into different groups according to the adjacency relationship
body: <p>I have a dataframe that stores adjacency relations. I want to divide numbers into different groups according to this dataframe. The dataframe are as follows:</p>
  <pre class="lang-r prettyprint-override"><code>df = data.frame(from=c(1,1,2,2,2,3,3,3,4,4,4,5,5), to=c(1,3,2,3,4,1,2,3,2,4,5,4,5))
  df
     from to
  1     1  1
  2     1  3
  3     2  2
  4     2  3
  5     2  4
  6     3  1
  7     3  2
  8     3  3
  9     4  2
  10    4  4
  11    4  5
  12    5  4
  13    5  5
  </code></pre>
  <p>In above dataframe, number <code>1</code> has links with number <code>1</code> and <code>3</code>, number <code>2</code> has links with number <code>2</code>, <code>3</code>, <code>4</code>, so number <code>1</code> can not be in same group with number <code>3</code> and number <code>2</code> can not be in same group with number <code>3</code> and number <code>4</code>. In the end, groups can be <code>c(1, 2, 5)</code> and <code>c(3, 4)</code>.</p>
  <p>I wonder how to program it?</p>