Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Generate table of combinations

Can anyone point me to an algorithm or VBA code to produce a table of
combination of items. For example if the items are 1,2,3,4,5 then the code
would generate:

1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 5
1, 2, 3, 4, 5
1, 2, 4
1, 2, 4, 5
1, 2, 5
1, 3
1, 3, 4
1, 3, 4, 5
1, 3, 5
1, 4
1, 4, 5
1, 5
2
2, 3
2, 3, 4
2, 3, 4, 5
2, 3, 5
2, 4
2, 4, 5
2, 5
3
3, 4
3, 4, 5
3, 5
4
4, 5
5

Similar for dog, cat, house
--
Gary's Student
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Generate table of combinations

http://tinyurl.com/7jqeo

Code previously posted by Myrna Larson. Not a complete solution, but you
could run it multiple times with different number of values in the subsets.

--
Regards,
Tom Ogilvy

"Gary's Student" wrote in message
...
Can anyone point me to an algorithm or VBA code to produce a table of
combination of items. For example if the items are 1,2,3,4,5 then the

code
would generate:

1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 5
1, 2, 3, 4, 5
1, 2, 4
1, 2, 4, 5
1, 2, 5
1, 3
1, 3, 4
1, 3, 4, 5
1, 3, 5
1, 4
1, 4, 5
1, 5
2
2, 3
2, 3, 4
2, 3, 4, 5
2, 3, 5
2, 4
2, 4, 5
2, 5
3
3, 4
3, 4, 5
3, 5
4
4, 5
5

Similar for dog, cat, house
--
Gary's Student



  #3   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Generate table of combinations

Hi,

Have a look at this:
http://www.excelforum.com/archive/in.../t-258299.html

Regards,
KL


"Gary's Student" wrote in message
...
Can anyone point me to an algorithm or VBA code to produce a table of
combination of items. For example if the items are 1,2,3,4,5 then the
code
would generate:

1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 5
1, 2, 3, 4, 5
1, 2, 4
1, 2, 4, 5
1, 2, 5
1, 3
1, 3, 4
1, 3, 4, 5
1, 3, 5
1, 4
1, 4, 5
1, 5
2
2, 3
2, 3, 4
2, 3, 4, 5
2, 3, 5
2, 4
2, 4, 5
2, 5
3
3, 4
3, 4, 5
3, 5
4
4, 5
5

Similar for dog, cat, house
--
Gary's Student



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Generate table of combinations

Thank you both very much.
--
Gary's Student


"Tom Ogilvy" wrote:

http://tinyurl.com/7jqeo

Code previously posted by Myrna Larson. Not a complete solution, but you
could run it multiple times with different number of values in the subsets.

--
Regards,
Tom Ogilvy

"Gary's Student" wrote in message
...
Can anyone point me to an algorithm or VBA code to produce a table of
combination of items. For example if the items are 1,2,3,4,5 then the

code
would generate:

1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 5
1, 2, 3, 4, 5
1, 2, 4
1, 2, 4, 5
1, 2, 5
1, 3
1, 3, 4
1, 3, 4, 5
1, 3, 5
1, 4
1, 4, 5
1, 5
2
2, 3
2, 3, 4
2, 3, 4, 5
2, 3, 5
2, 4
2, 4, 5
2, 5
3
3, 4
3, 4, 5
3, 5
4
4, 5
5

Similar for dog, cat, house
--
Gary's Student




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Generate table of combinations


Gary's Student wrote:
Can anyone point me to an algorithm or VBA code to produce a table of
combination of items. For example if the items are 1,2,3,4,5 then the code
would generate:

1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 5
1, 2, 3, 4, 5
1, 2, 4
1, 2, 4, 5
1, 2, 5
1, 3
1, 3, 4
1, 3, 4, 5
1, 3, 5
1, 4
1, 4, 5
1, 5
2
2, 3
2, 3, 4
2, 3, 4, 5
2, 3, 5
2, 4
2, 4, 5
2, 5
3
3, 4
3, 4, 5
3, 5
4
4, 5
5

Similar for dog, cat, house
--
Gary's Student


You could use a Gray code - which is what your example seems to be
trying to do:

Option Explicit

Function ListSubsets(Items As Variant) As String
Dim CodeVector() As Integer
Dim i As Integer
Dim lower As Integer, upper As Integer
Dim SubList As String
Dim NewSub As String
Dim done As Boolean
Dim OddStep As Boolean

OddStep = True
lower = LBound(Items)
upper = UBound(Items)

ReDim CodeVector(lower To upper) 'it starts all 0
Do Until done
'Add a new subset according to current contents
'of CodeVector

NewSub = ""
For i = lower To upper
If CodeVector(i) = 1 Then
If NewSub = "" Then
NewSub = Items(i)
Else
NewSub = NewSub & ", " & Items(i)
End If
End If
Next i
If NewSub = "" Then NewSub = "{}" 'empty set
SubList = SubList & vbCrLf & NewSub
'now update code vector
If OddStep Then
'just flip first bit
CodeVector(lower) = 1 - CodeVector(lower)
Else
'first locate first 1
i = lower
Do While CodeVector(i) < 1
i = i + 1
Loop
'done if i = upper:
If i = upper Then
done = True
Else
'if not done then flip the *next* bit:
i = i + 1
CodeVector(i) = 1 - CodeVector(i)
End If
End If
OddStep = Not OddStep 'toggles between even and odd steps
Loop
ListSubsets = SubList
End Function

Sub TestThis()
Dim i As Integer
Dim A(3 To 7) As Integer
Dim B As Variant

For i = 3 To 7
A(i) = i
Next i
B = Array("dog", "cat", "mouse", "zebra")

MsgBox ListSubsets(A)
MsgBox ListSubsets(B)

End Sub

If you run TestThis, then for example the second message box returns

{}
dog
dog, cat
cat
cat, mouse
dog, cat, mouse
dog, mouse
mouse
mouse, zebra
dog, mouse, zebra
dog, cat, mouse, zebra
cat, mouse, zebra
cat, zebra
dog, cat, zebra
dog, zebra
zebra

Hope this helps,

John Coleman

p.s. The algorithm used to generate the Gray code comes from the
excellent book "Combinatorial Algorithms: Generation, Enumeration and
Search" by Kreher and Stinson.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Generate table of combinations

Thank you John.

I'll look for your reccomended book
--
Gary's Student


"John Coleman" wrote:


Gary's Student wrote:
Can anyone point me to an algorithm or VBA code to produce a table of
combination of items. For example if the items are 1,2,3,4,5 then the code
would generate:

1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 5
1, 2, 3, 4, 5
1, 2, 4
1, 2, 4, 5
1, 2, 5
1, 3
1, 3, 4
1, 3, 4, 5
1, 3, 5
1, 4
1, 4, 5
1, 5
2
2, 3
2, 3, 4
2, 3, 4, 5
2, 3, 5
2, 4
2, 4, 5
2, 5
3
3, 4
3, 4, 5
3, 5
4
4, 5
5

Similar for dog, cat, house
--
Gary's Student


You could use a Gray code - which is what your example seems to be
trying to do:

Option Explicit

Function ListSubsets(Items As Variant) As String
Dim CodeVector() As Integer
Dim i As Integer
Dim lower As Integer, upper As Integer
Dim SubList As String
Dim NewSub As String
Dim done As Boolean
Dim OddStep As Boolean

OddStep = True
lower = LBound(Items)
upper = UBound(Items)

ReDim CodeVector(lower To upper) 'it starts all 0
Do Until done
'Add a new subset according to current contents
'of CodeVector

NewSub = ""
For i = lower To upper
If CodeVector(i) = 1 Then
If NewSub = "" Then
NewSub = Items(i)
Else
NewSub = NewSub & ", " & Items(i)
End If
End If
Next i
If NewSub = "" Then NewSub = "{}" 'empty set
SubList = SubList & vbCrLf & NewSub
'now update code vector
If OddStep Then
'just flip first bit
CodeVector(lower) = 1 - CodeVector(lower)
Else
'first locate first 1
i = lower
Do While CodeVector(i) < 1
i = i + 1
Loop
'done if i = upper:
If i = upper Then
done = True
Else
'if not done then flip the *next* bit:
i = i + 1
CodeVector(i) = 1 - CodeVector(i)
End If
End If
OddStep = Not OddStep 'toggles between even and odd steps
Loop
ListSubsets = SubList
End Function

Sub TestThis()
Dim i As Integer
Dim A(3 To 7) As Integer
Dim B As Variant

For i = 3 To 7
A(i) = i
Next i
B = Array("dog", "cat", "mouse", "zebra")

MsgBox ListSubsets(A)
MsgBox ListSubsets(B)

End Sub

If you run TestThis, then for example the second message box returns

{}
dog
dog, cat
cat
cat, mouse
dog, cat, mouse
dog, mouse
mouse
mouse, zebra
dog, mouse, zebra
dog, cat, mouse, zebra
cat, mouse, zebra
cat, zebra
dog, cat, zebra
dog, zebra
zebra

Hope this helps,

John Coleman

p.s. The algorithm used to generate the Gray code comes from the
excellent book "Combinatorial Algorithms: Generation, Enumeration and
Search" by Kreher and Stinson.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a macro in excel so that it can generate a list ofunique records using all permutations and combinations of the data in eachrow ad column Rizwan[_4_] Excel Discussion (Misc queries) 1 August 6th 09 01:44 PM
How to generate a list from a table Prashant Rao Excel Discussion (Misc queries) 1 November 14th 07 10:17 PM
Sorting of a pivot table by outer and inner row field combinations andygoon Excel Discussion (Misc queries) 1 February 5th 07 11:02 PM
Generate Random Number Table Tim Bieri Excel Programming 4 February 10th 05 06:35 PM
Generate all possible combinations from three data sets? Flamikey[_6_] Excel Programming 2 October 8th 04 11:20 PM


All times are GMT +1. The time now is 11:21 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"