JavaScript Program for Markov Matrix


A matrix is a kind of 2-D array, which is in the form of some number of rows, and for each row, there is the same number of columns and by the row and column number, we can get the element at any particular index. For the Markov matrix, each row's sum must equal 1. We are going to implement a code to create a new Markov matrix and find if the currently given matrix is a Markov matrix or not.

Introduction to Problem

In the given problem, we have to write a code that will produce the Markov matrix by using the binary data that is by using only zero and ones as we know the Markov matrix is the matrix in which the sum of the row must be equal to one (this don’t mean that it only consists of the binary numbers) which mean in each row there will be a single 1 present and other elements are zero.

The program we will implement will be just a special case for the Markov matrix.

For the second code, we will be given a matrix and have to find whether the current matrix is Markov’s Matrix. Let us see both the codes −

Creating a Markov Matrix

In the current part, we are using the binary digits that are zero and ones to create a Markov matrix. Let us see the approach first then we will move to the code implementation −

Approach

In this code, we are going to create a matrix using the new keyword and the Array. For each index of array we will again make an array to fill in it.

For each row of matrix, using the random function, we will get the random number in range of number of columns and fill that column of current row with 1 and other as zero.

At the end we will return the matrix.

Example

// creating a Markov's Matrix using binary digits
// defining the rows and columns
var row = 4
var col = 5
function MarkovMat(row, col){

   // creating an array of size row
   var arr = new Array(row);

   // traversing over the created array
   for(var i = 0; i < row; i++){

      // creating an array of size column
      var brr = new Array(col);
      brr.fill(0) // making every element zero of current array

      // generating random number
      var k = Math.floor(Math.random()*5);

      // marking kth index as 1
      brr[k] = 1

      // adding columns to the current row
      arr[i] = brr;
   }

   // printing the values
   console.log(arr)
}

// calling the function
MarkovMat(row,col)

Time and space complexity

In the above code, we have moved over the complete matrix and for each movement or the traversal we have got the random number each time which takes the constant time. So, the time complexity of the above code O(N*M) where N is the number of rows and M is the number of columns.

The space complexity is just equal to the size of the matrix, and we haven’t used any extra space. So, the space complexity of the above code O(N*M).

Checking Whether Current Matrix is Markov or Not

In the current part, we are given a matrix and have to find whether the current matrix is Markov Matrix. Let’s see the approach first then we will move to the code implementation −

Approach

In this code, we will simply traverse over the matrix and for each row get its count. If the count of the current row is 1 then we move to next one otherwise we will return that current matrix is not Markov’s.

Example

// function to check whether the current matrix is
// markov or not
function isMarkov(mat){
   var rows = mat.length
   var col = mat[0].length;

   // checking the sum of each row
   for(var i = 0; i < rows;i++){
      var count = 0;
      for(var j =0; j<col; j++) {
         count += mat[i][j];
      }
      if(count != 1){
         console.log("The given matrix is not Markov's Matrix");
         return
      }
   }
   console.log("The given matrix is Markov's Matrix");
}

// defining the matrix1
matrix1 = [[0.5, 0, 0.5], [0.5, 0.25, 0.25], [1, 0.0, 0], [0.33, 0.34, 0.33]]
console.log("For the matrix1: ")
isMarkov(matrix1)

// defining the matrix2
matrix2 = [[0.5, 1, 0.5], [0.5, 0.25, 0.25], [1, 0.0, 0], [0.33, 0.34, 0.33]]
console.log("For the matrix2: ")
isMarkov(matrix2)

Time and space complexity

In the above code, we have traversed the matrix and stored the sum of the each column, make the time complexity of above code as O(N*M).

We haven’t used any extra space in the above code, make the space complexity as O(1).

Conclusion

In this tutorial, we have implemented the JavaScript Program for the Markov Matrix. For the Markov matrix, each row's sum must equal 1. We have implemented a code to generate a binary Markov’s matrix using the random number generating function in time complexity of O(N*M) and the same space. Also, we have implemented a code that will check whether the current matrix is Markov’s or not in O(N*M) time.

Updated on: 30-Mar-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements