PC Review


Reply
Thread Tools Rate Thread

algorithm combinations

 
 
andrews
Guest
Posts: n/a
 
      16th Jul 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


 
Reply With Quote
 
 
 
 
Dennis
Guest
Posts: n/a
 
      16th Jul 2010
On Fri, 16 Jul 2010 16:40:03 +0200, andrews <(E-Mail Removed)> wrote:

>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?

--

Dennis
 
Reply With Quote
 
andrews
Guest
Posts: n/a
 
      16th Jul 2010
Yes, but that is just my question
Thanks
 
Reply With Quote
 
Dennis
Guest
Posts: n/a
 
      16th Jul 2010
On Fri, 16 Jul 2010 19:48:37 +0200, andrews <(E-Mail Removed)> wrote:

>Yes, but that is just my question


http://www.codeguru.com/cpp/cpp/algo...icle.php/c5117

--

Dennis
 
Reply With Quote
 
andrews
Guest
Posts: n/a
 
      16th Jul 2010
That should be the solution, but it is not Visual Basic and I know
nothing about C (or other) lanquages, but Pascal.
 
Reply With Quote
 
Dennis
Guest
Posts: n/a
 
      16th Jul 2010
On Fri, 16 Jul 2010 20:26:28 +0200, andrews <(E-Mail Removed)> wrote:

>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.

--

Dennis
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      17th Jul 2010
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

--
Tom Shelton


 
Reply With Quote
 
andrews
Guest
Posts: n/a
 
      17th Jul 2010
Thanks very much, I will try it.
 
Reply With Quote
 
andrews
Guest
Posts: n/a
 
      17th Jul 2010
Thanks very much, I will try it.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
algorithm alfred kamara Microsoft VB .NET 1 19th Jan 2007 03:57 PM
Help with an algorithm =?Utf-8?B?RGFu?= Microsoft Excel Programming 4 10th Sep 2006 01:51 AM
Combinations Algorithm Ryan Microsoft C# .NET 3 7th Dec 2003 04:57 AM
algorithm for combinations & permutations Bill McKeever Microsoft Excel Misc 4 31st Aug 2003 12:52 PM
algorithm? kathy Microsoft C# .NET 14 28th Aug 2003 07:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:39 AM.