Find if given matrix is Toeplitz or not

Given a square matrix, find if it’s a Toeplitz matrix or not. A Toeplitz (or diagonal-constant) matrix is a matrix in which each descending diagonal from left to right is constant, i.e., all elements in a diagonal are same.

In general, any n×n matrix mat[][] is a Toeplitz matrix if every cell mat[i][j] is same as mat[i-1][j-1], mat[i+1][j+1], mat[i-2][j-2], mat[i+2][j+2], .. for every cell mat[i][j] and all the valid cells mat[i+k][j+k] or mat[i-k][j-k]

Examples :

Input: mat[N][N] = {{ 6, 7, 8},
                    { 4, 6, 7},
                    { 1, 4, 6}},
Output : True;
Values in all diagonals are same.

Input: mat[N][N] = {{ 6, 7, 8, 9 },
                    { 4, 6, 7, 8 },
                    { 1, 4, 6, 7 },
                    { 0, 1, 4, 6 },
                    { 2, 0, 1, 4 }};
Output : True;

Input: mat[N][N] = {{ 6, 3, 8},
                    { 4, 9, 7},
                    { 1, 4, 6}},
Output : False;

The idea is very simple. For each element of first row and first column(or last row and last column) in the matrix, we check if descending diagonal starting from that element have same values or not. If we found any diagonal having different values, we return false.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check whether given matrix
// is a Toeplitz matrix or not
#include <iostream>
using namespace std;
#define N 5
#define M 4
  
// Function to check if all elements present in
// descending diagonal starting from position
// (i, j) in the matrix are all same or not
bool checkDiagonal(int mat[N][M], int i, int j)
{
    int res = mat[i][j];
    while (++i < N && ++j < M)
    {
        // mismatch found
        if (mat[i][j] != res)
            return false;
    }
  
    // we only reach here when all elements
    // in given diagonal are same
    return true;
}
  
// Function to check whether given matrix is a
// Toeplitz matrix or not
bool isToepliz(int mat[N][M])
{
    // do for each element in first row
    for (int i = 0; i < M; i++)
    {
        // check descending diagonal starting from
        // position (0, j) in the matrix
        if (!checkDiagonal(mat, 0, i))
            return false;
    }
  
    // do for each element in first column
    for (int i = 1; i < N; i++)
    {
        // check descending diagonal starting from
        // position (i, 0) in the matrix
        if (!checkDiagonal(mat, i, 0))
            return false;
    }
  
    // we only reach here when each descending
    // diagonal from left to right is same
    return true;
}
  
// Driver code
int main()
{
    int mat[N][M] =
    {
        { 6, 7, 8, 9 },
        { 4, 6, 7, 8 },
        { 1, 4, 6, 7 },
        { 0, 1, 4, 6 },
        { 2, 0, 1, 4 }
    };
  
    if (isToepliz(mat))
        cout << "Matrix is a Toepliz ";
    else
        cout << "Matrix is not a Toepliz ";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check whether given matrix
// is a Toeplitz matrix or not
import java.io.*;
  
class GFG 
{
    public static int N = 5;
    public static int M = 4;
      
    // Function to check if all elements present in
    // descending diagonal starting from position
    // (i, j) in the matrix are all same or not
    static boolean checkDiagonal(int mat[][], int i, int j)
    {
        int res = mat[i][j];
        while (++i < N && ++j < M)
        {
            // mismatch found
            if (mat[i][j] != res)
                return false;
        }
   
        // we only reach here when all elements
        // in given diagonal are same
        return true;
    }
      
    // Function to check whether given matrix is a
    // Toeplitz matrix or not
    static boolean isToepliz(int mat[][])
    {
        // do for each element in first row
        for (int i = 0; i < M; i++)
        {
            // check descending diagonal starting from
            // position (0, j) in the matrix
            if (!checkDiagonal(mat, 0, i))
                return false;
        }
   
        // do for each element in first column
        for (int i = 1; i < N; i++)
        {
            // check descending diagonal starting from
            // position (i, 0) in the matrix
            if (!checkDiagonal(mat, i, 0))
                return false;
        }
   
        // we only reach here when each descending
        // diagonal from left to right is same
        return true;
    }
      
    // driver program
    public static void main (String[] args) 
    {
        int mat[][] = { { 6, 7, 8, 9 },
                        { 4, 6, 7, 8 },
                        { 1, 4, 6, 7 },
                        { 0, 1, 4, 6 },
                        { 2, 0, 1, 4 }
                      };
   
        if (isToepliz(mat))
            System.out.println("Matrix is a Toepliz ");
        else
            System.out.println("Matrix is not a Toepliz ");
    }
}
  
// This code is contributed by Pramod Kumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check whether given 
# matrix is a Toeplitz matrix or not
N = 5
M = 4
  
# Function to check if all elements present in
# descending diagonal starting from position
# (i, j) in the matrix are all same or not
def checkDiagonal(mat, i, j):
    res = mat[i][j]
    i += 1
    j += 1
      
    while (i < N and j < M):
          
        # mismatch found
        if (mat[i][j] != res):
            return False
              
        i += 1
        j += 1
  
    # we only reach here when all elements
    # in given diagonal are same
    return True
  
# Function to check whether given  
# matrix is a Toeplitz matrix or not
def isToeplitz(mat):
      
    # do for each element in first row
    for j in range(M):
          
        # check descending diagonal starting from
        # position (0, j) in the matrix
        if not(checkDiagonal(mat, 0, j)):
            return False
      
    # do for each element in first column
    for i in range(1, N): 
          
        # check descending diagonal starting 
        # from position (i, 0) in the matrix
        if not(checkDiagonal(mat, i, 0)):
            return False
          
    return True
  
# Driver Code 
if __name__ == "__main__":
          
    mat = [[6, 7, 8, 9],
          [4, 6, 7, 8],
          [1, 4, 6, 7],
          [0, 1, 4, 6],
          [2, 0, 1, 4]]
  
    if(isToeplitz(mat)):
        print("Matrix is a Toeplitz")
    else:
        print("Matrix is not a Toeplitz")
  
# This code is contributed by Jasmine K Grewal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check whether given matrix
// is a Toeplitz matrix or not
using System;
  
class GFG {
  
    public static int N = 5;
    public static int M = 4;
      
    // Function to check if all elements present in
    // descending diagonal starting from position
    // (i, j) in the matrix are all same or not
    static bool checkDiagonal(int[,] mat, int i, int j)
    {
  
        int res = mat[i,j];
        while (++i < N && ++j < M)
        {
  
            // mismatch found
            if (mat[i,j] != res)
                return false;
        }
  
        // we only reach here when all elements
        // in given diagonal are same
        return true;
    }
      
    // Function to check whether given matrix is a
    // Toeplitz matrix or not
    static bool isToepliz(int [,] mat)
    {
  
        // do for each element in first row
        for (int i = 0; i < M; i++)
        {
  
            // check descending diagonal starting from
            // position (0, j) in the matrix
            if (!checkDiagonal(mat, 0, i))
                return false;
        }
  
        // do for each element in first column
        for (int i = 1; i < N; i++)
        {
  
            // check descending diagonal starting from
            // position (i, 0) in the matrix
            if (!checkDiagonal(mat, i, 0))
                return false;
        }
  
        // we only reach here when each descending
        // diagonal from left to right is same
        return true;
    }
      
    // driver program
    public static void Main () 
    {
        int [,] mat = { { 6, 7, 8, 9 },
                        { 4, 6, 7, 8 },
                        { 1, 4, 6, 7 },
                        { 0, 1, 4, 6 },
                        { 2, 0, 1, 4 }
                    };
  
        if (isToepliz(mat))
            Console.WriteLine("Matrix is a Toepliz ");
        else
            Console.WriteLine("Matrix is not a Toepliz ");
    }
}
  
// This code is contributed by KRV.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check whether 
// given matrix is a Toeplitz 
// matrix or not
  
// Function to check if all 
// elements present in descending 
// diagonal starting from position 
// (i, j) in the matrix are all 
// same or not
function checkDiagonal($mat, $i, $j)
{
    $N = 5; $M = 4;
    $res = $mat[$i][$j];
    while (++$i < $N && ++$j < $M)
    {
        // mismatch found
        if ($mat[$i][$j] != $res)
            return false;
    }
  
    // we only reach here when 
    // all elements in given 
    // diagonal are same
    return true;
}
  
// Function to check whether 
// given matrix is a
// Toeplitz matrix or not
function isToepliz($mat)
{
    $N = 5; $M = 4;
    // do for each element in first row
    for ($i = 0; $i < $M; $i++)
    {
        // check descending diagonal 
        // starting from position 
        // (0, j) in the matrix
        if (!checkDiagonal($mat, 0, $i))
            return false;
    }
  
    // do for each element 
    // in first column
    for ($i = 1; $i < $N; $i++)
    {
        // check descending diagonal
        // starting from position 
        // (i, 0) in the matrix
        if (!checkDiagonal($mat, $i, 0))
            return false;
    }
  
    // we only reach here when 
    // each descending diagonal 
    // from left to right is same
    return true;
}
  
// Driver code
$mat = array(array( 6, 7, 8, 9 ),
             array( 4, 6, 7, 8 ),
             array( 1, 4, 6, 7 ),
             array( 0, 1, 4, 6 ),
             array( 2, 0, 1, 4 ));
  
if (isToepliz($mat))
    echo "Matrix is a Toepliz ";
else
    echo "Matrix is not a Toepliz ";
  
// This code is contributed 
// by nitin mittal. 
?>

chevron_right



Output :

Matrix is a Toepliz

The time complexity of this solution would be O(n2) as we are traversing each element in the matrix once only.

Reference: https://en.wikipedia.org/wiki/Toeplitz_matrix

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : KRV, nitin mittal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.