Check Horizontal and Vertical Symmetry in Binary Matrix using JavaScript



In this article, we will learn to create a program to check both horizontal and vertical symmetry in a binary matrix in JavaScript.

A binary matrix is a 2-D array that consists of one and zero only as the elements in each cell. Horizontal symmetry of a binary matrix means if the first row is the same as the last row, the second row is the same as the second last row, and so on.

What are symmetry matrices? 

Symmetry in matrices refers to how the values in the matrix mirror themselves along an axis. In the case of a binary matrix (a matrix filled with only 0s and 1s), symmetry can be checked both horizontally and vertically.

  • Horizontal Symmetry: A binary matrix is said to have horizontal symmetry if the first row is identical to the last row, the second row is identical to the second-last row, and so on.
[1, 0, 1]
[0, 1, 0]
[1, 0, 1]
  • Vertical Symmetry: A matrix has vertical symmetry if the first column is identical to the last column, the second column to the second-last, and so on. 
[1, 0, 1] 
[0, 1, 0]
[1, 0, 1]

Problem Statment

Write a program in JavaScript to Check horizontal and vertical symmetry in binary matrix

Input

1 0 1
0 0 0 
1 0 1 

Output 

Both, horizontal and vertical symmetry is present. 

Explanation ?The first row and last row are the same which means there is horizontal symmetry. Similarly, first and last columns are the same leading to the vertical symmetry.

Input

1 0 1
0 0 0 
1 1 0 

Output

None of the symmetry is present.

Explanation ? The first row is not equal to the last row and the first column is not equal to the last column.

Using Naive Approach

We have seen the examples to get an idea about the given problem, now let us see the steps to implement the code ?

  • First, we will define a function to check the horizontal symmetry of the given matrix. This function will take a single parameter that is the given matrix and will return if the current matrix is horizontally symmetric or not.

  • We will traverse over the matrix and for each row we will compare to the row present on the other side of the imaginary line passing through the middle of the matrix and at the same distance the as current one.

  • We will define a function to check the vertical symmetry of the given matrix. This function will take a single parameter that is the given matrix.

  • We will traverse over the matrix and for each column we will compare to the column present on the other side of the imaginary line passing through the middle of the matrix and at the same distance as the current one.

  • We will call both functions and based on the return value we will print the result.

Example

Below is an example to check horizontal and vertical symmetry in a binary matrix using the Naive approach ?

// function to check horizontal symmetry 
function horizontalSymm(mat){
   var rows = mat.length;
   var cols = mat[0].length; 
   for(var i = 0; i< rows/2; i++){
      for(var j = 0;j<cols; j++){
         if(mat[i][j] != mat[rows-i-1][j]){
            return false;
         }
      }
   }
   return true;
}

// function to check vertical symmetry 
function verticalSymm(mat){
   var rows = mat.length;
   var cols = mat[0].length; 
   for(var i = 0; i< cols/2; i++){
      for(var j = 0;j<rows; j++){
         if(mat[j][i] != mat[j][cols-i-1]){
            return false;
         }
      }
   }
   return true;
}

// function to check the symmetry of the given matrix 
function check(mat){
   var horSymm = horizontalSymm(mat);
   var varSymm = verticalSymm(mat);
   if(horSymm && varSymm){
      console.log("Both, horizontal and vertical symmetries are present in the given matrix");
   }
   else if(horSymm){
      console.log("The given binary matrix is only horizontally symmetric");
   }
   else if(varSymm){
      console.log("The given binary matrix is only vertically symmetric");
   }
   else{
      console.log("The given binary matrix is neither horizontally symmetric nor vertically symmetric");
   }
}

// defining the given matrix 
var mat = [[1, 0, 1], [0, 0, 0], [1, 0, 1]];
console.log("The given matrix is: ")
console.log(mat);
check(mat);

// defining the given matrix 
var mat = [[1, 0, 1], [0, 0, 0], [1, 1, 0]];
console.log("The given matrix is: ")
console.log(mat);
check(mat);

Output

The given matrix is: 
[ [ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 0, 1 ] ]
Both, horizontal and vertical symmetries are present in the given matrix
The given matrix is: 
[ [ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 1, 0 ] ]
The given binary matrix is neither horizontally symmetric nor vertically symmetric

Time Complexity: O(N*M) where N is the number of rows of the given matrix and M is the number of columns of the given matrix.

Space Complexity: O(1), as we are not using any extra space.

Using Array.every() 

Here's an alternative approach using a more concise and functional programming style with Array.every() to check for horizontal and vertical symmetry. This approach avoids nested loops and makes the code cleaner.

Array.every() method 

The Array.every() method in JavaScript is used to test whether all elements in an array pass a given condition implemented by a provided callback function. It returns a boolean value:

  • true: If every element in the array satisfies the condition.
  • false: If any element fails to meet the condition.
return matrix.every((row, index) => {});

Example

Below is an example of checking horizontal and vertical symmetry in a binary matrix using Array.every() method

// Function to check horizontal symmetry
function isHorizontallySymmetric(matrix) {
    return matrix.every((row, index) => {
        return row.toString() === matrix[matrix.length - index - 1].toString();
    });
}

// Function to check vertical symmetry
function isVerticallySymmetric(matrix) {
    const transpose = matrix[0].map((_, colIndex) => matrix.map(row => row[colIndex]));
    return transpose.every((col, index) => {
        return col.toString() === transpose[transpose.length - index - 1].toString();
    });
}

// Function to check the symmetry of the given matrix
function checkSymmetry(matrix) {
    const horizontal = isHorizontallySymmetric(matrix);
    const vertical = isVerticallySymmetric(matrix);

    if (horizontal && vertical) {
        console.log("Both horizontal and vertical symmetries are present in the given matrix.");
    } else if (horizontal) {
        console.log("The given binary matrix is only horizontally symmetric.");
    } else if (vertical) {
        console.log("The given binary matrix is only vertically symmetric.");
    } else {
        console.log("The given binary matrix is neither horizontally symmetric nor vertically symmetric.");
    }
}

// Test cases
const matrix1 = [
    [1, 0, 1],
    [0, 0, 0],
    [1, 0, 1]
];
console.log("The given matrix is:");
console.log(matrix1);
checkSymmetry(matrix1);

const matrix2 = [
    [1, 0, 1],
    [0, 0, 0],
    [1, 1, 0]
];
console.log("The given matrix is:");
console.log(matrix2);
checkSymmetry(matrix2);

Output

The given matrix is:
[ [ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 0, 1 ] ]
Both horizontal and vertical symmetries are present in the given matrix.
The given matrix is:
[ [ 1, 0, 1 ], [ 0, 0, 0 ], [ 1, 1, 0 ] ]
The given binary matrix is neither horizontally symmetric nor vertically symmetric.

Time Complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix. 
Space Complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix. 

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-20T11:33:30+05:30

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements