Results 1 to 24 of 24

Thread: recursive functions

Threaded View

  1. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: recursive functions

    I think I managed to adapt HeapPermute algorithm and make a generic class out of it.

    Some theory can be found here:
    http://www.cut-the-knot.org/do_you_know/AllPerm.shtml


    vb Code:
    1. ''' <summary>
    2.     ''' A generic class that encapsulates a HeapPermute algorithm (I think)
    3.     ''' </summary>
    4.     ''' <typeparam name="T"></typeparam>
    5.     ''' <remarks>
    6.     ''' With this class you can obtain an IEnumerable which contains all the permutations of the given sequence of values.
    7.     ''' </remarks>
    8.     Class Permuter(Of T)
    9.         ' VS 2010 Auto implemented
    10.         Public Property Sequence As IEnumerable(Of T)
    11.  
    12.         Public Function GetAllPermutations() As IEnumerable(Of IEnumerable(Of T))
    13.             Return GetAllPermutations(_Sequence)
    14.         End Function
    15.  
    16.         Private Function GetAllPermutations(ByVal values As IEnumerable(Of T)) As IEnumerable(Of IEnumerable(Of T))
    17.             Dim ret As New List(Of List(Of T))
    18.  
    19.             Select Case values.Count
    20.                 Case 0
    21.                     Return ret
    22.                 Case 1
    23.                     ret.Add(New List(Of T) From {values(0)})
    24.                     Return ret
    25.                 Case Else
    26.                     Dim GetExcluded = Function(sourcevalues As IEnumerable(Of T), excluded_element As T) As IEnumerable(Of T)
    27.                                           Return From vals In sourcevalues Where Not vals.Equals(excluded_element)
    28.                                       End Function
    29.  
    30.                     For Each value As T In values
    31.                         Dim tempret As IEnumerable(Of IEnumerable(Of T)) = GetAllPermutations(GetExcluded(values, value))
    32.                         For Each tempelement In tempret
    33.                             Dim templist As New List(Of T)
    34.                             templist.Add(value)
    35.                             templist.AddRange(tempelement)
    36.                             ret.Add(templist)
    37.                         Next
    38.                     Next
    39.                     Return ret
    40.             End Select
    41.  
    42.         End Function
    43.     End Class


    Test program:

    vb Code:
    1. Sub Main()
    2.         Dim p As New Permuter(Of Integer)
    3.         Dim sequence() As Integer = {1, 2, 3, 4}
    4.  
    5.         p.Sequence = sequence
    6.  
    7.         Dim ret As IEnumerable(Of IEnumerable(Of Integer)) = p.GetAllPermutations
    8.  
    9.         For Each permutedsequence In ret
    10.             For Each value As Integer In permutedsequence
    11.                 Console.Write(value.ToString & "; ")
    12.             Next
    13.             Console.WriteLine()
    14.         Next
    15.         Console.ReadLine()
    16.     End Sub

    Test output:
    Code:
    1; 2; 3; 4;
    1; 2; 4; 3;
    1; 3; 2; 4;
    1; 3; 4; 2;
    1; 4; 2; 3;
    1; 4; 3; 2;
    2; 1; 3; 4;
    2; 1; 4; 3;
    2; 3; 1; 4;
    2; 3; 4; 1;
    2; 4; 1; 3;
    2; 4; 3; 1;
    3; 1; 2; 4;
    3; 1; 4; 2;
    3; 2; 1; 4;
    3; 2; 4; 1;
    3; 4; 1; 2;
    3; 4; 2; 1;
    4; 1; 2; 3;
    4; 1; 3; 2;
    4; 2; 1; 3;
    4; 2; 3; 1;
    4; 3; 1; 2;
    4; 3; 2; 1;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width