Code for Doing Combination in VB.Net
Writing up an algorithm to carry out the different Combination - Selection without Repetition, nCr - of a set of things in VB.Net requires some level of imaginative thinking.
Get a writing pad and pencil:- Write out all n members in the set - for Combination - at the top of the pad.
- Beginning with the first member, match it separately with the other members until the required selected-group size (r) is reached.
-
When every possible Combination for this first member is
exhausted, remove the current first member from the mother set.
The immediate next member becomes the new first member in the culminating set. - Take the first member in what is left of the mother set and repeat the same process from step II.
This is exactly what we will do with code to list up all
possible selections without repetition in VB.Net.
Create a new Visual Basic project;
call it Miscellaneous_VS
.
You can rename the module name -- just right-click on the name from the
Solution Explorer panel -- to Miscellaneous_Combination
if you want.
Create a new Visual Basic class file;
call it Combination
.
Type out the adjoining VB.Net algorithm for the combination of different options
(nCr).
Why Bother About Combination
Well, isn't it obvious?
Say you are to pick only four (4) pupils from a class of six
- such a small class; our little Combination algorithm solves
this little problem for you by showing all your possible options
/ selection outcomes.
VB.Net Code for Combination Class
Public words As New List(Of String)
Public r As Integer ' min length Of word
Protected comb_store As List(Of String())
Private i As Integer
' point of entry
Public Function possibleWordCombinations(candidates As List(Of String), size As Integer) As List(Of String())
words = candidates
r = size
comb_store = New List(Of String())
i = 0
' check for conformity
If r <= 0 Or r > words.Count Then
comb_store = New List(Of String())
ElseIf r = 1 Then
For i = 0 To words.Count - 1
comb_store.Add(New String() {words(i)})
Next i
Else
progressiveCombination()
End If
Return comb_store
End Function
' do combinations for all 'words' element
Private Sub progressiveCombination()
' single member list
repetitivePairing(New List(Of String)({words(i)}), i + 1)
If i + r <= words.Count Then
' move on to next degree
i += 1
progressiveCombination()
End If
End Sub
' do all possible combinations for 1st element of this array
Private Sub repetitivePairing(prefix As List(Of String), position As Integer)
Dim auxiliary_store(words.Count - position) As List(Of String)
For j As Integer = 0 To words.Count - position - 1
' check if desired -- r -- size will be realised
If r - prefix.Count <= words.Count - position Then
auxiliary_store(j) = New List(Of String)
auxiliary_store(j).AddRange(prefix)
auxiliary_store(j).Add(words(position))
If auxiliary_store(j).Count < r Then
' see to adding next word on
repetitivePairing(auxiliary_store(j), position + 1)
Else
comb_store.Add(auxiliary_store(j).ToArray())
End If
End If
position += 1
Next j
End Sub
End Class
Miscellaneous_Combination Module
Sub Main()
Dim goods As List(Of String)
goods = New List(Of String)(New String() {"Eno", "Chidi", "Olu", "Ahmed", "Osas", "Gbeda"})
Dim combo As New Combination
Dim result As List(Of String()) = combo.possibleWordCombinations(goods, 3)
' print choices and operation
Console.Write("[ ")
For Each choice As String In combo.words
Console.Write(choice & "; ")
Next
Console.WriteLine("] combination " & combo.r & ":" & Environment.NewLine)
' print out combinations 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