Python Program for Markov Matrix

In the previous article, we have discussed Python Program for Sum of Middle Row and Column in Matrix

Markov Matrix:

The matrix where the sum of each row equals one.

Given a matrix, the task is to check if the given matrix is a Markov matrix or not.

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Examples:

Example1:

Input:

Given Matrix : 
0.5 0.5 0
0 0 1
1 0 0

Output:

Yes, the given matrix is a Markov Matrix

Example2:

Input:

Given Matrix : 
1 2 0
0 0.5 0.5
1 0 0

Output:

No, the given matrix is not a Markov Matrix

Program for Markov Matrix in Python

Below are the ways to check if the given matrix is a Markov matrix or not in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the matrix as static input and store it in a variable.
  • Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
  • Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
  • Pass the given matrix as an argument to the chek_Markovmatrix function and check if the function returns true or false using the if conditional statement.
  • If it is true then print  “Yes, the given matrix is a Markov Matrix”.
  • Else print “No, the given matrix is not a Markov Matrix”.
  • Create a function to say chek_Markovmatrix which takes the given matrix as an argument and returns True or false.
  • Loop till the given number of rows using the For loop.
  • Take a variable to say row_summ and initialize its value to 0.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Add mtrx[p][q] to the above row_summ (where p is the iterator value of the parent for loop and q is the iterator value of the inner forloop).
  • Store it in the same variable row_summ
  • Check if the value of row_summ is not equal to 1 using the if conditional statement.
  • If it is true then return false.
  • Return True.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say chek_Markovmatrix which takes the given matrix as an argument
# and returns True or false.


def chek_Markovmatrix(mtrx):

   # Loop till the given number of rows using the For loop.
    for p in range(mtrxrows):
        # Take a variable to say row_summ and initialize its value to 0.

        row_summ = 0
        # Inside the For loop, Iterate till the given number of rows using another Nested
        # For loop (Inner For loop).
        for q in range(mtrxrows):
           # Add mtrx[p][q] to the above row_summ (where p is the iterator value of the
           # parent for loop and q is the iterator value of the inner forloop).
           # Store it in the same variable row_summ
            row_summ = row_summ + mtrx[p][q]
        # Check if the value of row_summ is not equal to 1 using the if conditional
        # statement.
        if (row_summ != 1):
          # If it is true then return false.
            return False
        # Return True.
    return True


# Give the matrix as static input and store it in a variable.
mtrx = [[0.5, 0.5, 0], [0, 0, 1], [1, 0, 0]]
# Calculate the number of rows of the given matrix by
# calculating the length of the nested list using the len() function
# and store it in a variable mtrxrows.
mtrxrows = len(mtrx)
# Calculate the number of columns of the given matrix by
# calculating the length of the first list in the nested list
# using the len() function and store it in a variable mtrxcols.
mtrxcols = len(mtrx[0])
# Pass the given matrix as an argument to the chek_Markovmatrix function and check if
# the function returns true or false using the if conditional statement.
if (chek_Markovmatrix(mtrx)):
  # If it is true then print  "Yes, the given matrix is a Markov Matrix".
    print("Yes, the given matrix is a Markov Matrix")
else:
  # Else print "No, the given matrix is not a Markov Matrix".
    print("No, the given matrix is not a Markov Matrix")

Output:

Yes, the given matrix is a Markov Matrix

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, give all the row elements of the given Matrix as a list using the list(), map(), float(), split() functions and store it in a variable.
  • Add the above row elements list to gvnmatrix using the append() function.
  • Pass the given matrix as an argument to the chek_Markovmatrix function and check if the function returns true or false using the if conditional statement.
  • If it is true then print  “Yes, the given matrix is a Markov Matrix”.
  • Else print “No, the given matrix is not a Markov Matrix”.
  • Create a function to say chek_Markovmatrix which takes the given matrix as an argument and returns True or false.
  • Loop till the given number of rows using the For loop.
  • Take a variable to say row_summ and initialize its value to 0.
  • Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
  • Add mtrx[p][q] to the above row_summ (where p is the iterator value of the parent for loop and q is the iterator value of the inner for loop).
  • Store it in the same variable row_summ
  • Check if the value of row_summ is not equal to 1 using the if conditional statement.
  • If it is true then return false.
  • Return True.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say chek_Markovmatrix which takes the given matrix as an argument
# and returns True or false.


def chek_Markovmatrix(mtrx):

   # Loop till the given number of rows using the For loop.
    for p in range(mtrxrows):
        # Take a variable to say row_summ and initialize its value to 0.

        row_summ = 0
        # Inside the For loop, Iterate till the given number of rows using another Nested
        # For loop (Inner For loop).
        for q in range(mtrxrows):
           # Add mtrx[p][q] to the above row_summ (where p is the iterator value of the
           # parent for loop and q is the iterator value of the inner forloop).
           # Store it in the same variable row_summ
            row_summ = row_summ + mtrx[p][q]
        # Check if the value of row_summ is not equal to 1 using the if conditional
        # statement.
        if (row_summ != 1):
          # If it is true then return false.
            return False
        # Return True.
    return True


# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using
    # the list(),map(),float(),split() functions and store it in a variable.
    l = list(map(float, input(
        'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
    # Add the above row elements list to gvnmatrix using the append() function.

    mtrx.append(l)
    
# Pass the given matrix as an argument to the chek_Markovmatrix function and check if
# the function returns true or false using the if conditional statement.
if (chek_Markovmatrix(mtrx)):
  # If it is true then print  "Yes, the given matrix is a Markov Matrix".
    print("Yes, the given matrix is a Markov Matrix")
else:
  # Else print "No, the given matrix is not a Markov Matrix".
    print("No, the given matrix is not a Markov Matrix")

Output:

Enter some random number of rows of the matrix = 3
Enter some random number of columns of the matrix = 3
Enter {3} elements of row {1} separated by spaces = 1 2 0
Enter {3} elements of row {2} separated by spaces = 0 0.5 0.5
Enter {3} elements of row {3} separated by spaces = 1 0 0
No, the given matrix is not a Markov Matrix

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.