JavaScript Program to Check if Matrix is Upper Triangular


An Upper triangular matrix is a squared matrix that has the same number of rows and columns and all the elements that are present below the main diagonal passing from the first cell (present at the top-left) towards the last cell (present at the bottom-right) are zero. Upper triangular means the elements present in the lower triangle will be zero. We will implement a proper code with explanation and discussion over time and space complexity.

Example

Input1:
mat = [ [ 1, 2, 3, 4],
   [ 0, 5, 6, 7],
   [ 0, 0, 8, 9],
   [ 0, 0, 0, 1]
]
Output1: Yes,

Explanation: We can see that the main diagonal contains the elements 1, 5, 8, and 1 and all the cells present below the main diagonal have the value zero.

Input2:
mat = [ [ 1, 2, 3, 4],
   [ 0, 5, 6, 7],
   [ 0, 0, 8, 9],
   [ 0, 1, 0, 1]
]
Output1: No

Explanation: We can see that the main diagonal contains the elements 1, 5, 8, and 1 and all the cells present below the main diagonal don’t have the value zero as the last row’s second column contains the non-zero value.

Approach

We have seen an example above, now let us see the steps to implement the code:

First, we will create a function in which we will pass the given matrix. We will traverse over the matrix only in the part which is the lower side of the main diagonal that is for each cell (i,j) where j is less than i. If we found any cell with a non-zero value, we will return false, otherwise in the end we will return true.

Example

// function to traverse over the matrix
function check(mat){

   // getting total number of rows of matrix
   var rows = mat.length
   
   // traversing over the section present above the main diagonal
   for(var i = 0; i < rows; i++){
      for(var j = 0; j < i; j++){
         if(mat[i][j] != 0){
            return false;
         }
      }
   }
   return true;
}

// defining the matrix
var mat = [ [ 1, 2, 3, 4],
   [ 0, 5, 6, 7],
   [ 0, 0, 8, 9],
   [ 0, 0, 0, 1]
]
   
// given matrix
console.log("The given matrix is: ");
console.log(mat)
if(check(mat)){
   console.log("The given matrix is an upper triangular matrix");
}
else{
   console.log("The given matrix is not an upper triangular matrix");
}

// updating matrix
mat = [ [ 1, 2, 3, 4],
   [ 0, 5, 6, 7],
   [ 0, 0, 8, 9],
   [ 0, 1, 0, 1]
      ]
   
// given matrix
console.log("The given matrix is: ");
console.log(mat)
if(check(mat)){
   console.log("The given matrix is an upper triangular matrix");
}
else{
   console.log("The given matrix is not an upper triangular matrix");
}

Output

The given matrix is: 
[ [ 1, 2, 3, 4 ], [ 0, 5, 6, 7 ], [ 0, 0, 8, 9 ], [ 0, 0, 0, 1 ] ]
The given matrix is an upper triangular matrix
The given matrix is: 
[ [ 1, 2, 3, 4 ], [ 0, 5, 6, 7 ], [ 0, 0, 8, 9 ], [ 0, 1, 0, 1 ] ]
The given matrix is not an upper triangular matrix

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the number of rows of the given matrix. This is because we have traversed over the matrix only once.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a JavaScript program to check if the given matrix is an upper triangular matrix or not. Upper triangular means the elements present in the lower triangle will be zero. We have traversed over the cells of the matrix where the value of columns is less as compared to the row number with the time complexity of O(N*N) and the space complexity of O(1).

Updated on: 13-Apr-2023

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements