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.

  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

  2. #2
    New Member
    Join Date
    Oct 2006
    Posts
    6

    Question Re: Generating unique combinations.

    this looks promising. I have been thinking about doing the same in SQL where input will be set of values from another table. Now that I see you code, I am not sure if this can be done in SQL. I haven't tested your code yet but will this work for numbers?
    So input {1,2,3,4} and we want all unique 3 combinations from list of 4
    o/p = {(1,2,3),(1,2,4), (1,3,4), (2,4,3)}

    Do you think it is possible to get this kind of output? Thanks for your help.

  3. #3
    New Member
    Join Date
    Oct 2006
    Posts
    6

    Re: Generating unique combinations.

    Thanks for all these examples. I'll try them later today and let you know how it goes.

    edit - I tested the code and it rocks!! thanks much.
    Last edited by strider2005; Oct 23rd, 2006 at 03:59 PM. Reason: added text

  4. #4
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: Generating unique combinations.

    thanks for the help. I'll try it out!
    If my post has been helpful, please rate it!

  5. #5
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: Generating unique combinations.

    hmm.. there seems to be something wrong when I changed it slightly. When I do it with only numbers 1234 and subset 3.. the list pops up as "(collection)".

    I kept the Combination Class exactly the same but there seems to be a problem when i copied your first example (1234) subset3 and changed it slightly to my own code. I'll post it on when I get back to my computer.
    If my post has been helpful, please rate it!

  6. #6
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: Generating unique combinations.

    Numbers = numericupdown
    Characters = textbox
    Here's the code:
    Code:
    Dim com As New Combination
            Dim mylist As New List(Of List(Of Object))
            Dim myobjectlist As New List(Of Object)
            Dim myInt() As Object = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
                 For Each i As Object In myInt
                myobjectlist.Add(i)
            Next
            mylist = com.GetCombinations(myobjectlist, Numbers.Value)
            Dim str As String
            ListBox1.Items.Clear()
            For Each i As List(Of Object) In mylist
                str = ""
                For Each j As Integer In i
                    str &= j
                Next
                Me.ListBox1.Items.Add(i)
            Next
            For Each i As List(Of Object) In mylist
                i.Sort()
                str = ""
                For Each j As Object In i
                    str &= CStr(j)
                Next
                Me.ListBox1.Items.Add(i)
            Next
        End Sub
    If my post has been helpful, please rate it!

  7. #7

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

    Re: Generating unique combinations.

    In your code you are assigning a list(Of object) to the ListBox items which is ďiĒ. You need to use the ďjĒ you assing the subset numbers to a string and than assing the string to the ListBox item's property.

    I updated the class so it will make it easy to sort and all the methods are declared as shared so you donít have to declare an instance of the class. I posted the code examples and it is based on your code.
    I hope it helps!

  8. #8
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: Generating unique combinations.

    there seems to be an error with get subset and sort...
    and also.. is there a way to list out all combos with up to 2 repeats of each letter/number? including repeats with different arrangements.
    Last edited by JXDOS; Nov 15th, 2007 at 05:37 AM.
    If my post has been helpful, please rate it!

  9. #9

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

    Re: Generating unique combinations.

    JXDOS the example I posted has no error and I tested it before I posted. What is the error and what is your code. If you are changing the code with out understanding what you are doing than unfurtunatly, there can be many problems. Try the example i posted whith out changing anything and see if it gives you any error. If it does than please provide the error!

  10. #10
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Re: Generating unique combinations.

    right.. sorry.. it does work fine.. thank you so much for all the help. just one last quesiton.. what about with repeats? like..a certain character could repeat up to x times?

    e.g.

    characters = 1,0
    repeats = 2
    length = 3

    output = 110, 001
    Last edited by JXDOS; Nov 18th, 2007 at 12:31 AM.
    If my post has been helpful, please rate it!

  11. #11

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

    Re: Generating unique combinations.

    Quote Originally Posted by JXDOS
    right.. sorry.. it does work fine.. thank you so much for all the help. just one last quesiton.. what about with repeats? like..a certain character could repeat up to x times?

    e.g.

    characters = 1,0
    repeats = 2
    length = 3

    output = 110, 001
    The subset size should be lees or equal to the set size otherewize it will throug an exception. You may want to try the multiples or the set objects, it might work {1,1,0,0}

  12. #12
    Hyperactive Member JXDOS's Avatar
    Join Date
    Aug 2006
    Location
    Mars...
    Posts
    423

    Thumbs up Re: Generating unique combinations.

    Thank you very much for all the help!
    If my post has been helpful, please rate it!

  13. #13
    New Member
    Join Date
    Jan 2013
    Posts
    1

    Re: Generating unique combinations.

    fantastic...
    but whats the alteration i need in this coding is values must be separated
    for instance out put should be
    1 2 3
    2 3 4
    5 6 3 (with blank spaces )
    or
    using any separators...
    plssssss i m doing this project for months...
    help me

  14. #14
    New Member
    Join Date
    Aug 2016
    Posts
    1

    Re: Generating unique combinations.

    Hey!

    I would like to generate all possible combinations with this code. Does anyone know how to configure the function to do so?

    Thanks in advance

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