Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. 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 Matrix
1 0 1 0 1 0 1 0 1
Horizontal symmetry: The first row "1 0 1" mirrors the third row "1 0 1" when 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" mirrors the third column "1 0 1" when flipped vertically. The elements in the first column align with the elements in the same positions in the third column.
Algorithm
Following are the steps to check matrix symmetry ?
Create a function checkHorizontalVertical() to check whether the input matrix is horizontal or vertically symmetric by passing the input matrix, number 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 secondtolast 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.
Next, check for vertical symmetry by comparing the first column with the last column, the second column with the secondtolast 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.
Check the conditions for symmetry and print appropriate messages based on the results.
Implementation
The example 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 ?
def checkHorizontalVertical(inputMatrix, rows, cols):
# Initializing both the horizontal and vertical symmetric as true at first
horizontal_symmetric = True
vertical_symmetric = True
# Check for horizontal symmetry
# Compare the first row with the last row, the second row with second last row and so on
for i in range(rows // 2):
for j in range(cols):
if inputMatrix[i][j] != inputMatrix[rows - 1 - i][j]:
horizontal_symmetric = False
break
if not horizontal_symmetric:
break
# Check for vertical symmetry
# Compare the first column with the last column, second column with second last column and so on
for j in range(cols // 2):
for i in range(rows):
if inputMatrix[i][j] != inputMatrix[i][cols - 1 - j]:
vertical_symmetric = False
break
if not vertical_symmetric:
break
# Check the conditions and print results
if not horizontal_symmetric and not vertical_symmetric:
print("Neither horizontal nor vertical symmetric")
elif horizontal_symmetric and not vertical_symmetric:
print("The input matrix is horizontally symmetric")
elif vertical_symmetric and not horizontal_symmetric:
print("The input matrix is vertically symmetric")
else:
print("The input matrix is both horizontally and vertically symmetric")
# Input matrix
inputMatrix = [[1, 0, 1],
[0, 1, 0],
[1, 0, 1]]
# Call the function
checkHorizontalVertical(inputMatrix, 3, 3)
The output of the above code is ?
The input matrix is both horizontally and vertically symmetric
Testing Different Cases
Let's test with matrices that have different symmetry properties ?
def checkHorizontalVertical(inputMatrix, rows, cols):
horizontal_symmetric = True
vertical_symmetric = True
# Check for horizontal symmetry
for i in range(rows // 2):
for j in range(cols):
if inputMatrix[i][j] != inputMatrix[rows - 1 - i][j]:
horizontal_symmetric = False
break
if not horizontal_symmetric:
break
# Check for vertical symmetry
for j in range(cols // 2):
for i in range(rows):
if inputMatrix[i][j] != inputMatrix[i][cols - 1 - j]:
vertical_symmetric = False
break
if not vertical_symmetric:
break
if not horizontal_symmetric and not vertical_symmetric:
print("Neither horizontal nor vertical symmetric")
elif horizontal_symmetric and not vertical_symmetric:
print("The input matrix is horizontally symmetric")
elif vertical_symmetric and not horizontal_symmetric:
print("The input matrix is vertically symmetric")
else:
print("The input matrix is both horizontally and vertically symmetric")
# Test Case 1: Only horizontally symmetric
matrix1 = [[1, 0, 1],
[0, 1, 0],
[1, 0, 1]]
print("Matrix 1:")
checkHorizontalVertical(matrix1, 3, 3)
# Test Case 2: Only vertically symmetric
matrix2 = [[1, 0, 1],
[0, 1, 0],
[0, 1, 0]]
print("\nMatrix 2:")
checkHorizontalVertical(matrix2, 3, 3)
# Test Case 3: Neither symmetric
matrix3 = [[1, 0, 1],
[0, 1, 0],
[0, 0, 1]]
print("\nMatrix 3:")
checkHorizontalVertical(matrix3, 3, 3)
Matrix 1: The input matrix is both horizontally and vertically symmetric Matrix 2: The input matrix is vertically symmetric Matrix 3: Neither horizontal nor vertical symmetric
Conclusion
We learned how to check matrix symmetry by comparing rows and columns from opposite ends. The algorithm efficiently determines if a binary matrix is horizontally symmetric, vertically symmetric, both, or neither using nested loops and boolean flags.
