usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Code for Permutation (Possible Ways of Arrangement) in VB.Net



Permutation - What It Is.

In the unlikely scenario that the Teacher wanted to see just how any four pupils, in a class of six (6), could be seated on a four-person desk; what this Teacher would be doing in essence is called Permutation (nPr).



Code for Doing Permutation in VB.Net

The algorithm for Permutation - nPr, possible ways of arrangement - will simply be based on that of combination.

All that is needed after combination is a rotation / shuffle of the make-up / constituents of each possible combination result.

This shuffle simply involves interchanging the elements of the combination group of size, r, to take all possible positions starting from the extreme right to extreme left.

This is how our Permutation code in VB.Net will work.

Create a new VB.Net class file; Call it Permutation
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it Miscellaneous_Permutation.

Type out the adjoining VB.Net algorithm for Permutation (nPr).



Advice: You might want to keep the mother-class size (n) and the group-size (r) small to avoid the permutation code taking too long.
As a rule-of-thump, DO NOT ASK QUESTIONS YOU DON'T WANT TO KNOW THE ANSWER TO.








VB.Net Code for Permutation Class

Public Class Permutation
    Inherits Combination

    Private local_store As List(Of String())
    Protected perm_store As List(Of String())
    Private index As Integer

    ' till the ground for shuffle to grind on
    Public Function possibleWordPermutations(candidates As List(Of String), size As IntegerAs List(Of String())
        perm_store = New List(Of String())
        possibleWordCombinations(candidates, size)
        ' illegal 'r' value
        If comb_store.Count = 0 Or r = 1 Then
            perm_store = comb_store
        Else
            Dim last_two As List(Of String())
            For i As Integer = 0 To comb_store.Count - 1
                last_two = New List(Of String())({New String() {""""}, New String() {""""}})
                index = r - 1
                ' copy up last two elements of 'comb_store(i)'
                last_two(0)(0) = comb_store(i)(index)
                last_two(1)(1) = last_two(0)(0)
                index -= 1
                last_two(0)(1) = comb_store(i)(index)
                last_two(1)(0) = last_two(0)(1)
                index -= 1

                local_store = New List(Of String())
                local_store.Add(last_two(0))
                local_store.Add(last_two(1))
                If r > 2 Then
                    shuffleWord(local_store, i)
                End If
                perm_store.AddRange(local_store)
            Next i
        End If
        Return perm_store
    End Function

    Private Sub shuffleWord(arg_store As List(Of String()), i As Integer)
        local_store = New List(Of String())
        Dim members As List(Of String)
        For j As Integer = 0 To arg_store.Count - 1
            members = New List(Of String)
            members.AddRange(arg_store(j))
            ' add 'index' 'comb_store(i)' element to this list of members
            members.Add(comb_store(i)(index))

            Dim temp_char As String
            Dim shift_index As Integer = members.Count
            ' shuffle this pack of words
            Do While shift_index > 0
                ' skip if already in store
                If local_store.Contains(members.ToArray()) = False Then
                    local_store.Add(members.ToArray())
                End If
                ' interchange these two neighbours
                shift_index -= 1
                If shift_index > 0 Then
                    temp_char = members(shift_index)
                    members(shift_index) = members(shift_index - 1)
                    members(shift_index - 1) = temp_char
                End If
            Loop
        Next j
        ' Are there any elements left? repeat if yes
        If index > 0 Then
            index -= 1
            shuffleWord(local_store, i)
        End If
    End Sub

End Class

Miscellaneous_Permutation Module

Module Miscellaneous_Permutation

    Sub Main()

        Dim goods As List(Of String)
        goods = New List(Of String)(New String() {"Eno""Chidi""Olu""Ahmed""Osas""Gbeda"})
        Dim perm As New Permutation
        Dim result As List(Of String()) = perm.possibleWordPermutations(goods, 2)
        ' print choices and operation
        Console.Write("[ ")
        For Each choice As String In perm.words
            Console.Write(choice & "; ")
        Next
        Console.WriteLine("] permutation " & perm.r & ":" & Environment.NewLine)
        ' print out permutations nicely
        Dim i As Integer = 0
        For Each group As String() In result
            i += 1
            Console.Write(i & ": ")
            For Each member As String In group
                Console.Write(member & "; ")
            Next
            Console.WriteLine()
        Next
        Console.WriteLine(Environment.NewLine & "Number of ways is " & result.Count & ".")

    End Sub

End Module



<< PreviousNext >>