PHP Program to Check if all rows of a matrix are circular rotations of each other


A rectangular array called a matrix is made up of rows and columns. And circular rotations entail rotating the array's elements so that after one rotation, the last member is in the first position and the other elements are shifted to the right. We are given an N*N matrix in this problem, and our objective is to determine whether all of the rows are circular rotations of one another. If they are, print "YES," otherwise print "NO."

In order to better understand the issue, let's look at some cases with explanations below.

Input 1

mat = [ [ 7, 8, 9],
      [ 9, 7, 8],
      [ 8, 9, 7] ]

Output 1

YES

Explanation

As consider the first row as a given array [ 7, 8, 9] now check for the remaining rows [9, 7, 8] and [8, 9, 7]:

Rotations for the second row [9, 7, 8] are: [8, 9, 7] and [7, 8, 9]

Second rotation is matched to the first row.

Rotations for the third row [8, 9, 7] are: [7, 8, 9] and [9, 7, 8]

First rotation is matched to the first row.

Therefore the answer is “YES” as all rows are permutations of each other.

Input 2

mat = [ [ 8, 9, 4],
      [ 9, 8, 4] ]

Output 2

NO

Explanation

As consider the first row as a given array [8, 9, 4] now check for the remaining rows [9, 8, 4]:

Rotations for the second row [9, 8, 4] are: [4, 9, 8] and [8, 4, 9]

None of them are equal to the first row.

Therefore, the answer is “NO” as all rows are not permutations of each other.

Approaches

We have seen the examples for the given matrix of size N*N above, let us move to the approaches:

Approach1: Naive Approach

The concept behind this method is to store the first row as a string and then combine it with itself so that it may be used for substring search operations. Then, iterate through the matrix to examine the other rows. After converting each row to a string, check to see whether it is a substring of the string from the first row. whether it is, return true; otherwise, return false.

To further understand the above method, let see the code below.

Example

PHP program to check if all rows of a matrix are circular rotations of each other

<?php
//Create a Function to check every row are circular rotations to each other or not.
function circularRotations( &$mat, $n){
   // Making a string that contains 
   // the first row's integers.
   $strFirstRow = "";    
   // Traverse the matrix to store the first row as a string
   for ($i = 0 ; $i < $n ; $i++) {
      // add the first row numbers to the variable strFirstRow
      $strFirstRow = $strFirstRow . "-" .
         strval($mat[0][$i]);
   } 
   // Combining the string strFirstRow with strFirstRow to enable the use of this string for substring search operations
   $strFirstRow = $strFirstRow . $strFirstRow;
   // Traverse the matrix from 1 row to check all remaning rows
   for ($i = 1; $i < $n; $i++){
      // Creating variable strCurrentRow and intialize it
      $strCurrentRow = "";       
      // Traverse the curret row
      for ($j = 0 ; $j < $n ; $j++) {
         // Store current rows number to the variable strCurrentRow
         $strCurrentRow = $strCurrentRow . "-" . strval($mat[$i][$j]);
      }
      // Verify whether the combining string strFirstRow contains the strCurrentRow string or not.
      if (strpos($strFirstRow, $strCurrentRow)){
         // Returns true if every row of given matrix are circular rotations of one another.
         return true;
      }
   }
   // If none of the specified matrix's rows are circular rotations of one another the function returns false.
   return false;
}
$n = 4; // Given size of the matrix
// Given matrix
$mat = array(array(6, 7, 8, 9),
            array(9, 6, 7, 8),
            array(8, 9, 6, 7),
            array(7, 8, 9, 6));       
// call function to check all rows of matrix are circular rotations of each other or not
if (circularRotations($mat, $n)){
   echo "YES";
   echo ", all rows of the matrix are circular rotations of each 
other";
} else{
   echo "NO";
   echo ", all rows of the matrix are not circular rotations of 
   each other.";
}
?>

Output

YES, all rows of the matrix are circular rotations of each other.

Time and Space Complexity

The time complexity of the above code is O(N^3), as we traverse the matrix and use the strval and strpos functions.

The space complexity of the above code is O(N), as we need to store rows of the matrix as strings.

Where N is the size of the row and column of the matrix.

Conclusion

In this tutorial, we have implemented a PHP Program to Check if all rows of a matrix are circular rotations of each other. We have implemented an approach in which convert rows into strings and use strval and strpos functions. The time complexity is O(N^3) and the space complexity of O(N). Where N is the size of the row and column of the matrix.

Updated on: 11-Jul-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements