Unique Combination Class

in this post I will talk about and present you with Combination class that can be used to generate unique combination of objects from a list of objects. The way this class is generating the combination and the fact that the GetSubsets method's parameter is an array of objects gives us an advantage of having the objects whit different data types. The only limitation of having the objects with different data types is that it can't be sorted. The objects can be value type as well referenced type. In order to sort a referenced type object array, the object should implement sortable interface.

Background:

Now, let's talk about the implementation. As I said in the introduction, the GetSubsets method returns unique combination of nested list. What do I mean by unique combination? For example: let's say we have a list of character objects {A,B,C} and we want to get combination of 2 characters. All the possible combination we can get would be:

AB
BA
AC
CA
BC
CB

As you can see "AB" is the same as "BA", "AC" is the same as "CA", and "BC" is the same as "CB" (they differ only by location) making them total of six combination. But now let look ate the unique combination of these letters.

AB
AC
BC

There are only three combination of letters. I want to point it out that the maximum number of items can be returned from the method is limited to the max integer "2147483647" due to the fact that indexed data types support integer type for the index parameter. Also, I want to mention that the list of objects that will be passed to the method (the set), each object in the set should be unique in order to get unique lists of subset objects. For example if we have a set {A,A,B,C}, than the method will return:

AA
AB
AC
AB
AC
BC

That is because each element is treated as a single unique object so if you don't want it to happen then just remove the repeated objects from the set before passing it in to the method.

Code Example (VB.Net)

The Combination class in action:

Public Class Form1

    Private Sub ButtonCalcula_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCalcula.Click
        'This will get a list of unique combination subsets of integers.
        'An integer array to hold the set.
        Dim mySet() As Object = {1, 2, 3, 4, 5, 6, 7}
        'Get the unique combination of numbers.
        Dim mySubsets As New List(Of List(Of Object))
        Try
            mySubsets = Combination.GetSubsets(mySet, CInt(Me.SetsTextBox.Text))
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Combination Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        'Clear the display.
        Me.ListBox1.Items.Clear()
        'Sort the 2D list of subsets.
        Combination.Sort(mySubsets)
        Dim str As String
        For Each i As List(Of Object) In mySubsets
            str = ""
            For Each j As Integer In i
                'Assign the subset numbers to a string.
                str &= j
            Next j
            'Populate the ListBox1.
            Me.ListBox1.Items.Add(str)
        Next i

        'This will get the number of subsets.
        Me.SetsLabel.Text = CStr(Combination.Count( _
        CULng(mySet.LongLength), CULng(Me.SetsTextBox.Text)))
    End Sub

    'Set:{1, 2, 3, 4, 5}; Subsets (k): 3
    'Outcome: 

    '123
    '124
    '125
    '134
    '135
    '145
    '234
    '235
    '245
    '345

    'Number of Subsets: 10

End Class

System Requirements

  • Visual Studio
  • Dot Net Framework

Downloads



blog comments powered by Disqus