Results 1 to 8 of 8

Thread: Recursive combinations [please help]

  1. #1

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Recursive combinations [please help]

    How do I make the code below recursive? I'm trying to create all combination of the array. This gives all combinations where the length is exactly 5. I want it to give all combinatoin where the length is 1 to the ubound of array s. So for a length of 3 there would be 3 nested loops, for length of 8 there would be 8 nested loops and so on.

    Ultimately what I want to accomplish is to have an array filled with numbers and find the sum of every possible combination and pick the combination that has a sum that is closet to a target number without going under, but I thought I'd start here first.

    VB Code:
    1. Sub CreateCombos()
    2.  
    3.     Dim s(8)
    4.  
    5.     s(1) = "A"
    6.     s(2) = "B"
    7.     s(3) = "C"
    8.     s(4) = "D"
    9.     s(5) = "E"
    10.     s(6) = "F"
    11.     s(7) = "G"
    12.     s(8) = "H"
    13.  
    14.     lLen = 5
    15.  
    16.     For i1 = 1 To (UBound(s) - lLen) + 1
    17.     For i2 = i1 + 1 To (UBound(s) - lLen) + 2
    18.     For i3 = i2 + 1 To (UBound(s) - lLen) + 3
    19.     For i4 = i3 + 1 To (UBound(s) - lLen) + 4
    20.     For i5 = i4 + 1 To (UBound(s) - lLen) + 5
    21.                    
    22.         Debug.Print s(i1) & s(i2) & s(i3) & s(i4) & s(i5)
    23.    
    24.     Next i5
    25.     Next i4
    26.     Next i3
    27.     Next i2
    28.     Next i1
    29.  
    30. End Sub

    Any help?
    Last edited by WorkHorse; Jul 11th, 2006 at 12:02 AM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Recursive combinations

    By definition, for a procedure to be recursive, it must call itself.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub CreateCombos()
    4.   Dim s(1 To 4) As String
    5.   Dim MyCombo As String
    6.   Dim MaxLen As Long
    7.  
    8.   s(1) = "A"
    9.   s(2) = "B"
    10.   s(3) = "C"
    11.   s(4) = "D"
    12.  
    13.   Call ComboRecursion(s, MyCombo, 1, 3)
    14. End Sub
    15.  
    16. Private Sub ComboRecursion(s() As String, MyCombo As String, ByVal ThisLen As Long, MaxLen As Long)
    17.   Dim i As Long
    18.  
    19.   For i = LBound(s) To UBound(s)
    20.     MyCombo = MyCombo & s(i)
    21.     If ThisLen < MaxLen Then
    22.       Call ComboRecursion(s, MyCombo, ThisLen + 1, 3)
    23.     Else
    24.       Debug.Print MyCombo
    25.     End If
    26.     MyCombo = Left$(MyCombo, ThisLen - 1)
    27.   Next i
    28. End Sub
    29.  
    30. Private Sub Command1_Click()
    31.   Call CreateCombos
    32. End Sub

    This code obviously doesn't do what you want, but it should give you an idea about how to approach this.

  3. #3

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Recursive combinations

    I still don't get it. I understand recursion, but I just can't seem to apply it to my own code. Any help?

  4. #4

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Recursive combinations [please help]

    OK, any way to do this without recursion? I don't care. I just need a way to handle this where lLen could be anything so there could be an infinite number of for...next loops. I'm not getting anything to work. Please help.

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Recursive combinations [please help]

    G'Day WH, how are ya.

    Wouldn't the math be (in the example at least)
    8x7x6x5x4x3x2x1 (being a range from 1-8)

    So that something simple like A,B,C (an Array of 3 elements) yould yeild:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    For this you have 3x2x1 = 6, hence 6 results (above) for A,B,C.


    Is that theory correct (if so I have seen it posted here before)?

  6. #6

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Recursive combinations [please help]

    Quote Originally Posted by Bruce Fox
    G'Day WH, how are ya.

    Wouldn't the math be (in the example at least)
    8x7x6x5x4x3x2x1 (being a range from 1-8)

    So that something simple like A,B,C (an Array of 3 elements) yould yeild:

    ABC
    ACB
    BAC
    BCA
    CAB
    CBA

    Is that theory correct (if so I have seen it posted here before)?
    No, no, no. The idea is to get combinations, not permutations (if I'm using those words right). ABC is the same as ACB as far as I'm concerned. I have an array of numbers. I need to find the best combination of those numbers so that the some of the combination gets closest to a target value. The sum of A+B+C would be the same as the sum of A+C+B. So I need exclusive combinations. And I need every combination or every length. The array might have 8 numbers, but the best combination might consist of only two of those numbers, or only one, or all 8.

    I think I'm getting close with the code below, but I'm not sure how to work in the array.

    VB Code:
    1. Sub DooDah()
    2.  
    3.     For x = 1 To 3
    4.         Combo "", 0, x, 1, 4
    5.     Next x
    6.  
    7. End Sub
    8.  
    9. Sub Combo(sPrefix, lDepth, lLen, lMin, lMax)
    10.  
    11.     For i = lMin To lMax
    12.       If lDepth = (lLen - 1) Then
    13.         Debug.Print sPrefix & i
    14.       Else
    15.         Combo sPrefix & i, lDepth + 1, lLen, i + 1, lMax + 1
    16.       End If
    17.     Next i
    18.  
    19. End Sub

  7. #7

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Re: Recursive combinations [please help]

    Bwwwahhhhahhhahhhhaa!!!

    I've at least managed to do what I asked for in the original post. Now I just need to work in the array.

    VB Code:
    1. Sub DooDah()
    2.  
    3.     For x = 1 To 5
    4.         Combo "", 0, x, 1, 5
    5.     Next x
    6.  
    7. End Sub
    8.  
    9. Sub Combo(sPrefix, lDepth, lLen, lMin, lMax)
    10.  
    11.     For i = lMin To lMax
    12.       If lDepth = (lLen - 1) Then
    13.         Debug.Print sPrefix & Chr$(i + 64)
    14.       Else
    15.         Combo sPrefix & Chr$(i + 64), lDepth + 1, lLen, i + 1, lMax
    16.       End If
    17.     Next i
    18.  
    19. End Sub

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Recursive combinations [please help]

    Could you now pass each Array element from an ireration within the DooDah() Sub (similar to the For/Next (x - being the Array Index))?

Posting Permissions

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



Click Here to Expand Forum to Full Width