Partition of a set

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
How can we generate the partitions of a given set using VBA. For example, the set {1, 2, 3} has these five partitions (one partition per item):
{ {1}, {2}, {3} }, sometimes written 1|2|3.
{ {1, 2}, {3} }, or 12|3.
{ {1, 3}, {2} }, or 13|2.
{ {1}, {2, 3} }, or 1|23.
{ {1, 2, 3} }, or 123.

Wikipedia Reference: Set-Partition

There is an article with a non-vba code which is generating the sets using Stirling numbers of second kind, Set-Generate but I could not comprehend the same. I have the Stirling number UDF.

VBA Code:
Public Function StirlingType2( _
       ByVal n As Long, _
       ByVal k As Long) As Long

    Dim u, v, i, j

    u = k ^ n
    v = 0
    i = 0
    j = 0

    If k = n Then
        StirlingType2 = 1&
    ElseIf k > n Then
        StirlingType2 = 0&
    ElseIf k = 0& Then
        StirlingType2 = 0&
    Else
        For i = 1 To (k - 1)
            j = Application.WorksheetFunction.Combin(k, i) * (k - i) ^ n
            If i Mod 2 = 0 Then
                v = v + j
            Else
                v = v - j
            End If
        Next
        StirlingType2 = (u + v) / Application.WorksheetFunction.Fact(k)
    End If

End Function
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
What are you going to use this for?

VBA Code:
Sub SetPartition()
' MrExcel.com, Eric W, Partition of a set   
    Call recur("", "abc")
   
End Sub

Sub recur(set1, set2)
Dim ls As Long, i As Long, j As Long, str1 As String, str2 As String, w As String

    Debug.Print Mid(set1 & "|" & set2, 2)
   
    ls = Len(set2) - 1
    If ls = 0 Then Exit Sub

    For i = 1 To 2 ^ ls - 1
        str1 = Left(set2, 1)
        str2 = ""
        w = WorksheetFunction.Dec2Bin(i, ls)
        For j = 1 To ls
            If Mid(w, j, 1) = "0" Then
                str1 = str1 & Mid(set2, j + 1, 1)
            Else
                str2 = str2 & Mid(set2, j + 1, 1)
            End If
        Next j
        Call recur(set1 & "|" & str1, str2)
    Next i
       
End Sub
 
Upvote 0
Change this line

Debug.Print Mid(set1 & "|" & set2, 2)

to

Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Offset(1) = Mid(set1 & "|" & set2, 2)

changing the sheet name and column as desired.
 
Upvote 0
@Eric W ... how can I start from Row=1 instead of current output starting from Row=2 of column C.
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top