- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Matrix is Markov Matrix or not in Java
Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.
A square matrix is called a Markov matrix if all entries are nonnegative and the sum of each column vector is equal to 1. The Markov matrix represents steps in a Markov chain. Each input of the Markov matrix represents the probability of an outcome.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose we have a matrix | 0.10.50.4 | A= | 00.50.5 | | 0.90.10 |
All the rows add up to 1. Hence it is a Markov matrix
Instance-2
Suppose we have a matrix | 0.40.30.3 | A =| 0.50.30.2 | | 0.50.10.4 |
All the rows add up to 1. Hence it is a Markov matrix
Instance-3
Suppose we have a matrix | 1 0 0 0| A=| 0 1 0 0| | 0 0 1 0| | 0 0 0 1|
All the rows add up to 1. Hence it is a Markov matrix
Algorithm
Step 1 − Take the matrix.
Step 2 − Now call the check function that traverses the matrix and checks that each row sum equals to 1. If any row’s elements doesn't add up to 1, then the matrix is not a Markov matrix.
Step 3 − Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix.
By Using Dynamic Initialization of Matrix.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix
In this approach, matrix is initialized in the program, we use a for loop to calculate the sum of each row and check if it adds up to 1.
Example
import java.util.Scanner; public class Main { static boolean markovMatCheck(double mat[][]) { double sum = 0; // Traverses the matrix for (int i = 0; i < mat.length; i++) { sum = 0; // Adds the row elements for (int j = 0; j < mat.length; j++) { sum += mat[i][j]; } // Checks if row elements add upto 1, else returns false if (sum != 1.0) return false; } return true; } public static void main(String[] args) { // Matrix to be checked double mat[][] = { { 0.1, 0.5, 0.4 }, { 0, 0.5, 0.5 }, { 0.9, 0, 0.1 }, }; // Print results if (markovMatCheck(mat)) System.out.println("The matrix is a Markov matrix."); else System.out.println("The matrix is not a Markov matrix."); } }
Output
The matrix is a Markov matrix.
Approach-2: By Using Dynamic Initialization of Matrix
In this approach, we ask the user to enter a matrix of their desired size, and then use a for loop to calculate the sum of each row and check if it adds up to 1.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Enter number of matrix rows-"); Scanner sc = new Scanner(System.in); int size = sc.nextInt(); int mat[][] = new int[size][size]; // Enter Matrix Elements System.out.println("Enter the matrix elements"); for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { mat[i][j] = sc.nextInt(); } } // Print results if (markovMatCheck(mat)) System.out.println("The matrix is a Markov matrix."); else System.out.println("The matrix is not a Markov matrix."); } //user defined method static boolean markovMatCheck(int mat[][]) { int sum = 0; // Traverses the matrix for (int i = 0; i < mat.length; i++) { sum = 0; // Adds the row elements for (int j = 0; j < mat.length; j++) { sum += mat[i][j]; } // Checks if row elements add upto 1, else returns false if (sum != 1) return false; } return true; } }
Output
Enter number of matrix rows- 3 Enter the matrix elements 1 0 0 0 1 0 0 0 1 The matrix is a Markov matrix.
In this article, we explored different approaches to check if the matrix is a Markov matrix or not by using Java programming language.