VB.NET - permutations - Asked By George n t on 02-Oct-08 02:10 AM

how to generate the permutation  of any set of numbers

11,22,23,45,34

without repetition in vb.net

program for generating permutation of given number - Perry replied to George n t on 02-Oct-08 03:46 AM

Hi,

I used following code to generate the permuation of given number.
Suppose number given : 123

Output will be:
123
132
213
231
312
321

Code
----
' Generate permutations of the values in the

' values collection.
' Return the result through a collection of
' collections that each hold a permutation.
Private Function GeneratePermutations(ByVal values As _
Collection) As Collection
Dim results As New Collection

' See if there is only one value.
If values.Count = 1 Then
' Return a collection containing one
' permutation equal to the single value.
results.Add(New Collection)
results.Item(1).Add(values.Item(1))
Return results
End If

' Build permutations starting with
' each possible first item.
results = New Collection
Dim num_values As Integer = values.Count
For i As Integer = 1 To num_values
' Save this value.
Dim first_value As Object = values.Item(i)

' Remove the item.
values.Remove(i)

' Generate the permutations of the
' remaining values.
Dim new_permutations As Collection = _
GeneratePermutations(values)

' Make permutations by adding first_value
' to the beginning of each of the new
' permutations.
For j As Integer = 1 To new_permutations.Count
' Add the first item.
Dim new_result As New Collection
new_result.Add(first_value)

' Add the rest of the items in the jth
' new permutation.
For k As Integer = 1 To _
new_permutations(j).Count
new_result.Add(new_permutations(j).Item(k))
Next k

' Add this new permutation to the results.
results.Add(new_result)
Next j

' Restore the removed value.
If i > values.Count Then
values.Add(first_value)
Else
values.Add(first_value, , i)
End If
Next i

' Return the results.
Return results
End Function

Regards,
Megha

generate parmutation recursively - Perry replied to George n t on 02-Oct-08 03:49 AM

Hi,

You can generate permutation by using following program which uses recursive method.

Private m_NumValues As Integer
Private m_Used() As Integer
Private m_CurrentSolution() As Integer

Private Sub EnumerateValues(ByVal index As Integer)
Dim result As String
Dim i As Integer

' See if there are any values left to try.
If index > m_NumValues Then
' All values are m_Used.
' Get a string for the current solution.
For i = 1 To m_NumValues
result = result & Format$(m_CurrentSolution(i)) _
& " "
Next i

' Add the current solution to the list.
txtResults.Text = txtResults.Text & _
result & vbCrLf
Exit Sub
End If

' Examine each value.
For i = 1 To m_NumValues
' See if this value has been m_Used yet.
If Not m_Used(i) Then
' It is unm_Used. Try using it.
m_Used(i) = True
m_CurrentSolution(index) = i

EnumerateValues index + 1

m_Used(i) = False
End If
Next i
End Sub

Regards,
Megha

REPLY - Binny ch replied to George n t on 02-Oct-08 12:59 PM

list = originalString.split('')
index
= (0,0)
list
= [""]
for iteration n in 1 to y:
  index
= (index[1], len(list))
 
for string s in list.subset(index[0] to end):
   
for character c in originalString:
      list
.add(s + c)

you'd then need to remove all strings less than x in length, they'll be the first (x-1) * len(originalString) entries in the list.

Cheers

how to display the permutaions from the collection 'results' into listbox
George n t replied to Perry on 07-Oct-08 11:25 AM
end of post
solution - Perry replied to George n t on 07-Oct-08 11:36 AM
end of post
solution - Perry replied to George n t on 07-Oct-08 11:41 AM
Hi,

Iterate the collection values and insert in to ListBox.

Use below code:

    Dim Item As String;
    For Each Item In Results
        ListBox1.Items.Add(Item)
    Next

Regards,
Megha
permutation - George n t replied to Perry on 08-Oct-08 01:43 AM

Private Function GeneratePermutations(ByVal values As _
    Collection) As Collection
    Dim results As New Collection

    ' See if there is only one value.
    If values.Count = 1 Then
        ' Return a collection containing one
        ' permutation equal to the single value.
        results.Add(New Collection)
        results.Item(1).Add(values.Item(1))
        Return results
    End If

    ' Build permutations starting with
    ' each possible first item.
    results = New Collection
    Dim num_values As Integer = values.Count
    For i As Integer = 1 To num_values
        ' Save this value.
        Dim first_value As Object = values.Item(i)

        ' Remove the item.
        values.Remove(i)

        ' Generate the permutations of the
        ' remaining values.
        Dim new_permutations As Collection = _
            GeneratePermutations(values)

        ' Make permutations by adding first_value
        ' to the beginning of each of the new
        ' permutations.
        For j As Integer = 1 To new_permutations.Count
            ' Add the first item.
            Dim new_result As New Collection
            new_result.Add(first_value)

            ' Add the rest of the items in the jth
            ' new permutation.
            For k As Integer = 1 To _
                new_permutations(j).Count
                new_result.Add(new_permutations(j).Item(k))
            Next k

            ' Add this new permutation to the results.
            results.Add(new_result)
        Next j

        ' Restore the removed value.
        If i > values.Count Then
            values.Add(first_value)
        Else
            values.Add(first_value, , i)
        End If
    Next i

    ' Return the results.
    Return results
End Function


how to call the above  function & dispaly the permutaion of numbers   32,21,14

reply - Perry replied to George n t on 08-Oct-08 02:46 AM

You main code should looks like below:

Sub MyMain()

    Dim Item As String;

    Dim Resulsts As Collection

    Results = GeneratePermutations(123);
    For Each Item In Results
        ListBox1.Items.Add(Item)
    Next

End Sub

Regards,

Megha

how to call - George n t replied to Perry on 08-Oct-08 03:26 AM

how to call the above  function & dispaly the permutaion of numbers   32,21,14

Jim Hersey replied to George n t on 30-Jul-13 08:37 PM

 

After finding the call method suggested did not work. The following might help some of you struggling with this.  The function looks for a collection of characters.  Convert your string into a collection of characters. Pass that collection in and then after the call put the collection back together.  I used a list instead of a collection for what I wanted to do.


        'Permutations
       'Create a Collection Of Characters
       Dim CharCollection As New Collection
       Dim c As Char() = TextBox1.Text.ToCharArray()
       Dim Results As Collection
       For Each Ch As Char In c
         CharCollection.Add(Ch)
       Next
 
       'Call the Function
       Results = GeneratePermutations(CharCollection)
 
       'Put characters back into a list of words
       Dim SearchList As New List(Of String)
       For Each Word In Results
         Dim strtemp As String = String.Empty
         For Each cx In Word
           strtemp &= cx
         Next
         SearchList.Add(strtemp)
       Next