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







<< PreviousNext >>

Code for Selection with Repetitions in VB.Net



Possible Selections With Repetition.

Imagine being given the opportunity to pick six (6) exercise books from a book-shop having 10 different brands of exercise books - Ben 10, Chelsea F.C., Superman, Tottenham F.C., Indomitables, Manchester City F.C., Spider Man, Power Rangers, Liverpool F.C. and Bat Man exercise books.
If you happen to be a big Power Rangers fan, nothing stops you from selecting all 6 exercise books to be Power Rangers exercise books.
But you can as well decide to pick only 3 Power Rangers exercise books and make up for the other 3 with any other brands of exercise book.



Code for Repetitive Selection in VB.Net

The algorithm for Selection with Repetition will be a lot similar to that of combination.

  1. Beginning with the first member in the mother set, match it separately with every member - including itself - until the required Selection group-size (r) is reached.
  2. When every possible Selection with this member is exhausted, move to the next member in the mother set and repeat Step I.

This is how our Repetitive Selection code in VB.Net will work.

Create a new VB.Net class file; Call it Selection.
Optionally, Create a new VB.Net module file; Call it Miscellaneous_Selection.

Type out the adjoining VB.Net algorithm for Selection with Repetition.








VB.Net Code for Selection Class

Public Class Selection
    Public words As String()
    Public r As Integer ' min length Of word
    Protected complete_group As List(Of String())
    Private i As Integer

    Public Function groupSelection(candidates As String(), size As IntegerAs List(Of String())
        words = candidates
        r = size
        complete_group = New List(Of String())
        i = 0
        recursiveFillUp(New List(Of String))

        Return complete_group
    End Function

    ' pick elements recursively
    Protected Sub recursiveFillUp(temp As List(Of String))
        Dim picked_elements(words.Length) As List(Of String)
        Dim j As Integer = i
        Do While j < words.Length
            picked_elements(j) = New List(Of String)
            picked_elements(j).AddRange(temp)
            picked_elements(j).Add(words(j))
            ' recoil factor
            If i >= words.Length Then
                i = j
            End If
            ' satisfied yet?
            If picked_elements(j).Count = r Then
                complete_group.Add(picked_elements(j).ToArray())
            ElseIf picked_elements(j).Count < r Then
                recursiveFillUp(picked_elements(j))
            End If
            j += 1
        Loop
        j -= 1
        If picked_elements(j).Equals(Nothing) = False And picked_elements(j).Count = r Then
            i += 1 ' keep recoil factor straightened out
        End If
    End Sub

End Class

Miscellaneous_Selection Module

Module Miscellaneous_Selection

    Sub Main()

        Dim goods As String() = {"0""1""2""3""4""5""6""7""8""9"}
        Dim pick As New Selection
        Dim result As List(Of String()) = pick.groupSelection(goods, 2)

        ' print choices and operation
        Console.Write("[ ")
        For Each choice As String In pick.words
            Console.Write(choice & "; ")
        Next
        Console.WriteLine("] selection " & pick.r & ":" & Environment.NewLine)

        ' print out selections 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 >>