How to find all the possible rows which are permutations of a given row in a matrix with VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub printPermutationRows(ByVal matrix As Integer()(), ByVal givenrow As Integer)
        Dim rows As Integer = matrix.Length
        Dim cols As Integer = matrix(0).Length
        Dim St As HashSet(Of Integer) = New HashSet(Of Integer)()

        For j As Integer = 0 To cols - 1
			st.Add(matrix(givenrow)(j))
        Next

        For i As Integer = 0 To rows - 1
            If i = givenrow Then
                Continue For
            End If

            Dim j As Integer = 0
            While j < cols
				If Not st.Contains(matrix(i)(j)) Then
					Exit While
                End If
                j += 1
            End While

            If j <> cols Then
                Continue For
            End If
            
			Console.Write(i & ", ")
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim givenrow As Integer = 1
        
		Dim matrix As Integer()() = New Integer()() {
				New Integer() {7, 9, 4, 3, 1}, 
				New Integer() {4, 7, 9, 1, 3}, 
				New Integer() {4, 7, 8, 1, 2},
				New Integer() {1, 6, 9, 7, 4}, 
				New Integer() {9, 1, 3, 7, 4}}

        printPermutationRows(matrix, givenrow)
    End Sub
End Class



' run:
'
' 0, 4,
'

 



answered Feb 21, 2024 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub printPermutationRows(ByVal matrix As Integer(,), ByVal givenrow As Integer)
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)
        Dim St As HashSet(Of Integer) = New HashSet(Of Integer)()

        For j As Integer = 0 To cols - 1
			st.Add(matrix(givenrow, j))
        Next

        For i As Integer = 0 To rows - 1
            If i = givenrow Then
                Continue For
            End If

            Dim j As Integer = 0
            While j < cols
				If Not st.Contains(matrix(i, j)) Then
					Exit While
                End If
                j += 1
            End While

            If j <> cols Then
                Continue For
            End If
            
			Console.Write(i & ", ")
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim givenrow As Integer = 1
        
		Dim matrix = New Integer(,) {
				{7, 9, 4, 3, 1}, 
				{4, 7, 9, 1, 3}, 
				{4, 7, 8, 1, 2},
				{1, 6, 9, 7, 4}, 
				{9, 1, 3, 7, 4}}

        printPermutationRows(matrix, givenrow)
    End Sub
End Class



' run:
'
' 0, 4,
'

 



answered Feb 21, 2024 by avibootz
...