algorithm combinations

  • Thread starter Thread starter andrews
  • Start date Start date
A

andrews

I am searching for a good algorithm for generating combinations.
F.e.
I give the arguments n and m where m and n are integers and m < n.
Takes as example n = 5 and m = 3.
The program gives the results
123
124
125
134
135
145
234
235
245
345
The ordering of the three elements is not important and there are
n!/(n-m)!m! possibilities
I can to that with
for
for
for
for
....
next
next
next
next
.....
but this is to long and to complicated when n is big
Thanks for any response
 
I am searching for a good algorithm for generating combinations.
F.e.
I give the arguments n and m where m and n are integers and m < n.
Takes as example n = 5 and m = 3.
The program gives the results
123
124
125
134
135
145
234
235
245
345
The ordering of the three elements is not important and there are
n!/(n-m)!m! possibilities
I can to that with
for
for
for
for
...
next
next
next
next
....
but this is to long and to complicated when n is big

Have you thought about some sort of recursion?
 
That should be the solution, but it is not Visual Basic and I know
nothing about C (or other) lanquages, but Pascal.
 
That should be the solution, but it is not Visual Basic and I know
nothing about C (or other) lanquages, but Pascal.

Then do like I did and google for a VB solution.
 
andrews explained on 7/16/2010 :
I am searching for a good algorithm for generating combinations.
F.e.
I give the arguments n and m where m and n are integers and m < n.
Takes as example n = 5 and m = 3.
The program gives the results
123
124
125
134
135
145
234
235
245
345
The ordering of the three elements is not important and there are n!/(n-m)!m!
possibilities
I can to that with
for
for
for
for
...
next
next
next
next
....
but this is to long and to complicated when n is big
Thanks for any response

Maybe something like this?

Imports System.Text

Module Module1

Sub Main()
Dim comboGen As New CombonationGenerator(5, 3)
For Each combo As String In comboGen.GenerateCombos()
Console.WriteLine(combo)
Next
End Sub

Class CombonationGenerator
Private Const AllDigits As String = "0123456789"
Private _maxDigit As Integer
Private _length As Integer


Public Sub New(ByVal maxDigit As Integer, ByVal length As
Integer)
If maxDigit < 0 OrElse maxDigit > 9 Then Throw New
ArgumentException("maxDigit must be between 0 and 9")

_maxDigit = maxDigit
_length = length
End Sub

Public Function GenerateCombos() As List(Of String)
Dim digits() As Char = AllDigits.Substring(0, _maxDigit +
1).ToCharArray()
Dim initialValue As New String(digits(0), _length)
Dim maxValue As New String(digits(_maxDigit), _length)
Dim combos As New List(Of String)()
combos.Add(initialValue)

Dim buffer As New StringBuilder(initialValue)

While buffer.ToString() <> maxValue
For i As Integer = _length - 1 To 0 Step -1
Dim index As Integer = Array.FindIndex(digits,
Function(c As Char) c = buffer(i))
If index <> digits.GetUpperBound(0) Then
buffer(i) = digits(index + 1)
Exit For
Else
buffer(i) = digits(0)
End If
Next
combos.Add(buffer.ToString())
End While

Return combos
End Function

End Class
End Module
 
Back
Top