Summod

Hi ,

I am trying to generate the possible combinations from the below groups

A1 B1 C1
A2 B2 C2
A3

The output shall be like

A1B1C1
A1B1C2
A1B2C1
A1B2C2

A2B1C1
A2B1C2
A2B2C1
A2B2C2

A3B1C1
A3B1C2
A3B2C1
A3B2C2

Any idea how this output can be genarated ,... using recursive functions

Regards
Sumod

### Re: Visual Basic Express Edition Possible Combinations algorithm

LouieG

do a loop inside a loop inside a loop. something like:

for i=1 to 3

for j = 1 to 2

for k = 1 to 2

value=arr1(i) & arr2(j) & arr3(k)

next k

next j

next i

no recursive functions needed

### Re: Visual Basic Express Edition Possible Combinations algorithm

Summod

Thanks for the suggestion. But needs little more .. Here the number of groups ( A,B,C ) is dynamically selected , could be any number of groups ( A,B,C,D,E,F ) .

Not sure how the loop logic can be implemented for this

### Re: Visual Basic Express Edition Possible Combinations algorithm

Dave299

I haven't tested this as thoroughly as I might but it looks ok. The code for working out the combinations does not depend upon the number of groups you have.

Code Snippet

Public Class Form1

Dim Label1 As New Label

Dim WithEvents Button1 As New Button

Dim Groups As New List(Of Group)

Dim GroupA As New Group

Dim GroupB As New Group

Dim GroupC As New Group

Dim GroupD As New Group

Dim Combinations As New List(Of String)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

AutoScroll = True

Button1.SetBounds(20, 20, 80, 25)

Button1.Text = "Click me!"

Label1.AutoSize = True

Label1.Font = New Font("Arial", 14, FontStyle.Bold)

Label1.Location = New Point(100, 20)

For count As Integer = 1 To 2

Next

For count As Integer = 1 To 3

Next

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label1.Text = ""

Dim Total As Integer = 1

For Each G As Group In Groups

Total *= G.Values.Count

Next

For count As Integer = 0 To Total - 1

Next

Group.Number = Total

For count As Integer = 0 To Groups.Count - 1

Next

Label1.Text = ""

For count As Integer = 0 To Total - 1

Label1.Text += Combinations(count) & vbCrLf

Next

End Sub

End Class

Public Class Group

Public Shared Number As Integer

Public Values As New List(Of String)

Public Sub AddValues(ByRef CurrentList As List(Of String))

Dim Count As Integer

Dim NumberToInsert As Integer = Number \ Values.Count

Dim NumberInserted As Integer

For counter As Integer = 0 To CurrentList.Count - 1

CurrentList(counter) += Values(Count)

NumberInserted += 1

If NumberInserted = NumberToInsert Then

NumberInserted = 0

Count += 1

If Count = Values.Count Then

Count = 0

End If

End If

Next

Number = Number \ Values.Count

End Sub

End Class