title: How to group a list of connected pairs
body: <p>I have a list of connections from X to Y as follows:-</p>
  
  <pre><code>public class Connection
  {
      private int X { get; set; }
      private int Y { get; set; }
  
      public Connection(int X, int Y)
      {
          this.X = X;
          this.Y = Y;
      }
  }
  
  
  List&lt;Connection&gt; connectionList = new List&lt;Connection&gt;();
  
  connectionList.Add(new Connection(1, 2));
  connectionList.Add(new Connection(1, 3));
  connectionList.Add(new Connection(1, 4));
  connectionList.Add(new Connection(2, 1));
  connectionList.Add(new Connection(2, 3));
  connectionList.Add(new Connection(2, 4));
  connectionList.Add(new Connection(3, 1));
  connectionList.Add(new Connection(3, 2));
  connectionList.Add(new Connection(4, 1));
  connectionList.Add(new Connection(4, 2));
  connectionList.Add(new Connection(5, 6));
  </code></pre>
  
  <p>I want to now know from connectionList which <code>groups</code> I have. For example here are two connected graphs which represent the data above.</p>
  
  <p><a href="https://i.stack.imgur.com/i5pXI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i5pXI.jpg" alt="connected graph"></a></p>
  
  <p>The desired output from the program would be two groups, A(1, 2, 3 4) and B(5, 6).</p>
  
  <p>What is the best approach for this?</p>