Check Horizontal and Vertical Symmetry in the Binary Matrix using Python


A binary matrix is a rectangular grid in which each element is either 0 or 1, indicating true or false states. It is widely employed to represent relationships, connectivity, and patterns across various disciplines

Assume we have taken a 2D binary input matrix containing N rows and M columns. T. We will now check whether the input matrix is horizontal or vertically symmetric or both using the below method.

If the first row matches the last row, the second row matches the second last row, and so on, the matrix is said to be horizontally symmetric.

If the first column matches the last column, the second column matches the second last column, and so on, the matrix is said to be vertically symmetric.

Example

1 0 1
0 1 0
1 0 1

Horizontal symmetry: The first row, "1 0 1," is an exact mirror reflection of the third row, "1 0 1," when the matrix is flipped horizontally. Each element in the first row corresponds to the element in the same position in the third row.

Vertical symmetry: The first column, "1 0 1," is a mirrored version of the third column, "1 0 1," when the matrix is flipped vertically. The elements in the first column align with the elements in the same positions in the third column.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a function checkHorizontalVertical() to check whether the input matrix is horizontal or vertically symmetric by passing the input matrix, no of rows and columns as arguments.

  • Initialize horizontal_symmetric and vertical_symmetric as True to assume that the matrix is symmetric in both directions.

  • Compare the first row with the last row, the second row with the second-to-last row, and so on, to check for horizontal symmetry.

  • Traverse till half of the rows of the matrix.

  • Inside the loop, traverse through the columns and compare each cell of the current row with the corresponding cell in the row being compared.

  • If any cell is different, set horizontal_symmetric to False and break the loop.

  • Increment the row number by 1 and decrement the row number from the last row by 1 for the next iteration.

  • Next, check for vertical symmetry by comparing the first column with the last column, the second column with the second-to-last column, and so on.

  • Traverse till half of the columns of the matrix.

  • Inside the loop, traverse through the rows and compare each cell of the current column with the corresponding cell in the column being compared.

  • If any cell is different, set vertical_symmetric to False and break the loop.

  • Increment the column number by 1 and decrement the column number from the last column by 1 for the next iteration.

  • Check the conditions for symmetry:

    • If neither horizontal_symmetric nor vertical_symmetric is True, print "Neither horizontal nor vertical symmetric."

    • If horizontal_symmetric is True but vertical_symmetric is False, print "The input matrix is horizontally symmetric."

    • If vertical_symmetric is True but horizontal_symmetric is False, print "The input matrix is vertically symmetric."

    • If both horizontal_symmetric and vertical_symmetric are True, print "The input matrix is both horizontally and vertically symmetric."

  • Call the checkHorizontalVertical function with the inputMatrix, number of rows, and number of cols as arguments.

Example

The example given below compares rows with their counterparts from the end to check for horizontal symmetry and columns with their counterparts from the end to check for vertical symmetry. Based on the comparison results, it prints whether the input matrix is horizontally symmetric, vertically symmetric, both horizontally and vertically symmetric, or neither horizontal nor vertically symmetric.

# Creating a function to check whether the input matrix is
# horizontal or vertically symmetric by passing the input matrix,
# no of rows and columns as arguments
def checkHorizontalVertical(inputMatrix, rows, cols):

    # Initializing both the horizontal and vertical
    # symmetric as true at first
    horizontal_symmetric = True
    vertical_symmetric = True

    # We compare the first row with the last row, the second row with second
    # last row and so on.
    p = 0
    x = rows - 1
    # Traversing till half of the rows of the matrix
    while p < rows // 2:

        # Traversing in the columns
        for q in range(cols):
            # Checking if each cell is the same or not
            if inputMatrix[p][q] != inputMatrix[x][q]:
                # If it is not the same then that horizontal symmetric is false
                horizontal_symmetric = False
                break
                # Incrementing the row number by 1
        p += 1
        x -= 1

    # Checking for Vertical Symmetry. The first column is compared to the last column,
    # the second column to the second-to-last column, and so forth.
    p = 0
    x = cols - 1
    # Traversing till half of the columns
    while p < cols // 2:

        # Checking each row of the column
        for q in range(rows):

            # Checking if each cell is the same or not
            if inputMatrix[p][q] != inputMatrix[x][q]:
                # If it is not the same then that vertical symmetric is false
                vertical = False
                break
                # Incrementing the column number by 1
        p += 1
        x -= 1

        # checking whether not horizontal and not vertically symmetric
    if not horizontal_symmetric and not vertical_symmetric:
        # printing Neither horizontal nor vertical symmetric if the condition is true
        print("Neither horizontal nor vertical symmetric")
        # checking whether the matrix is horizontally symmetric but not vertical
    elif horizontal_symmetric and not vertical_symmetric:
        # printing horizontal symmetric matrix
        print("The input matrix is horizontal symmetric")
        # checking whether the matrix is vertically symmetric but not horizontal
    elif vertical_symmetric and not horizontal_symmetric:
        # printing vertically symmetric
        print("The input matrix is vertical symmetric")
    else:
        # else printing both horizontal and vertical symmetric
        print("The input matrix is both horizontal and vertically symmetric")


# input matrix
inputMatrix = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]


# calling the above checkHorizontalVertical() by passing
# input matrix, no of rows, columns as arguments
checkHorizontalVertical(inputMatrix, 3, 3)

Output

The input matrix is both horizontal and vertically symmetric

Conclusion

We covered how to traverse the matrix using nested for loops and how to traverse through rows of each column using for loops in this post. Finally, we learned how to determine whether a particular matrix is horizontally or vertically symmetric.

Updated on: 17-Aug-2023

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements