Unique Combinations in a set

rmatthew

Centurion
Joined
Dec 30, 2002
Messages
115
Location
Texas
I am in need of an algorithm to return an array of ALL unique subsets of n params. For instance in a set where n = 3 the return values would be:

I sure wish I would have paid more attention in math class all those years ago :)

Original set 1,2,3

[none]
123
1
2
3
12
13
23

Here is what I know (think I know):

1.The algorithm will be recursive.
2.The algorithm will be exponential in complexity/passes for each increase in initial population.

Any and all coments are greatly appreciated.
 
Here is what I have for a fixed set but it needs to be recursive...

Visual Basic:
   Dim arraylist() As String = Split("1,2,3,4,5,6", ",")
        Dim setsize As Integer = 4
        Dim level As Integer = 1
        Dim a As Integer = 0
        Dim b As Integer
        Dim c As Integer
        Dim d As Integer
        Dim f As Integer

        For b = a + 1 To UBound(arraylist) + 1 - setsize + level
            level = level + 1
            For c = b + 1 To UBound(arraylist) + 1 - setsize + level
                level = level + 1
                For d = c + 1 To UBound(arraylist) + 1 - setsize + level
                    level = level + 1
                    Debug.WriteLine(b & c & d)
                    level = level - 1
                Next
                level = level - 1
            Next
            level = level - 1
        Next
 
If you haven't figured it out by this evening, I'll try to help further but for now my initial thoughts are:
Code:
Function combos(ary() as string, digits as integer)
   If digits = 1 Then
       Return ary
   Else
         'create tmpAry
          'loop through ary finding all combinations for the given set of digits adding each to tmpAry
         'this will be something close n=ary.length/digits for the number of loops.
         Return tmpAry + combox(ary, digits -1)
    End if
End Function

sorry this is kinda choppy, it's a question i'd need to actually think about but hopefully this will get you started.
 
Back
Top