PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Generating unique combinations.-VBForums
Results 1 to 14 of 14

Thread: Generating unique combinations.

Threaded View

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Generating Unique Combinations

    Some times I update the class and post the updated date if I fined any bugs so it is recommended to check out this thread once a while.
    This class can be used to generate unique combinations of objects from a list of objects. The way this class is generating the combinations and the fact that the 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.

    Now, letís talk about the combinations. As I said, the method returns unique combinations of nested list which is different from all the combinations. For example: let's say we have a list of character objects {A,B,C} and we want to get combinations with 2 characters. The all combinations of the list 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" making total of six combinations. But now let look ate the unique combinations.

    AB
    AC
    BC

    There are only three combinations.
    I want to point it out that the maximum of the list items count that returned 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 objects that will be passed to the method should have unique objects in order to get unique lists of objects. For example if we have a set {A,A,B,C} then 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 parameter.

    Using The Code:

    'This will get a list of unique combination subsets of numbers.
    vb Code:
    1. 'An integer array to hold the set.
    2. Dim mySet() As Object = {1, 2, 3, 4, 5}
    3. 'Get the unique combinations of numbers.
    4. Dim mySubsets As New List(Of List(Of Object))
    5. mySubsets = Combination.GetSubsets(mySet, 3)
    6. 'Clear the display.
    7. Me.ListBox1.Items.Clear()
    8. 'Sort the 2D list of combinations.
    9. Combination.Sort(mySubsets)
    10. Dim str As String
    11. For Each i As List(Of Object) In mySubsets
    12.     str = ""
    13.     For Each j As Integer In i
    14.         'Assign the subset numbers to a string.
    15.         str &= j
    16.     Next j
    17.     'Populate the ListBox.
    18.     Me.ListBox1.Items.Add(str)
    19. Next i
    'This will get the count of combinations.
    vb Code:
    1. Me.TextBox2.Text = CStr(Combination.Count(5, 3))

    Set = {1, 2, 3, 4, 5}, Subsets = 3

    Outcome:

    123
    124
    125
    134
    135
    145
    234
    235
    245
    345

    Please feel free to comment or notify me about any problems associated with the class.

    Download the latest version here
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width