Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether the given matrix is a Toeplitz matrix using C#?
A Toeplitz matrix is a special matrix where every diagonal running from top-left to bottom-right contains identical elements. In other words, for any element at position matrix[i][j], it should be equal to matrix[i-1][j-1].
This property makes Toeplitz matrices useful in signal processing, time series analysis, and various mathematical applications.
Algorithm
The key insight is that we only need to check if each element equals the element at its upper-left diagonal position. If matrix[i,j] != matrix[i-1,j-1] for any valid position, the matrix is not Toeplitz.
Using Simple Comparison Method
Example 1: Toeplitz Matrix
using System;
public class Matrix {
public bool IsToeplitzMatrix(int[,] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[i, j] != matrix[i - 1, j - 1]) {
return false;
}
}
}
return true;
}
public void DisplayMatrix(int[,] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++) {
Console.Write("[");
for (int j = 0; j < cols; j++) {
Console.Write(matrix[i, j]);
if (j < cols - 1) Console.Write(",");
}
Console.WriteLine("]");
}
}
}
class Program {
public static void Main() {
Matrix m = new Matrix();
int[,] matrix1 = {{1, 2, 3, 4},
{5, 1, 2, 3},
{9, 5, 1, 2}};
Console.WriteLine("Matrix 1:");
m.DisplayMatrix(matrix1);
Console.WriteLine("Is Toeplitz: " + m.IsToeplitzMatrix(matrix1));
Console.WriteLine();
int[,] matrix2 = {{1, 2},
{2, 2}};
Console.WriteLine("Matrix 2:");
m.DisplayMatrix(matrix2);
Console.WriteLine("Is Toeplitz: " + m.IsToeplitzMatrix(matrix2));
}
}
The output of the above code is −
Matrix 1: [1,2,3,4] [5,1,2,3] [9,5,1,2] Is Toeplitz: True Matrix 2: [1,2] [2,2] Is Toeplitz: False
Using Dictionary-Based Diagonal Tracking
Example 2: Alternative Approach
using System;
using System.Collections.Generic;
public class ToeplitzChecker {
public bool IsToeplitzMatrix(int[,] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
Dictionary<int, int> diagonals = new Dictionary<int, int>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int diagonalIndex = i - j;
if (diagonals.ContainsKey(diagonalIndex)) {
if (diagonals[diagonalIndex] != matrix[i, j]) {
return false;
}
} else {
diagonals[diagonalIndex] = matrix[i, j];
}
}
}
return true;
}
}
class Program {
public static void Main() {
ToeplitzChecker checker = new ToeplitzChecker();
int[,] matrix = {{1, 2, 3},
{4, 1, 2},
{7, 4, 1}};
Console.WriteLine("Checking matrix using diagonal tracking:");
Console.WriteLine("Result: " + checker.IsToeplitzMatrix(matrix));
int[,] nonToeplitz = {{1, 2},
{3, 1}};
Console.WriteLine("Non-Toeplitz matrix result: " + checker.IsToeplitzMatrix(nonToeplitz));
}
}
The output of the above code is −
Checking matrix using diagonal tracking: Result: True Non-Toeplitz matrix result: False
Key Points
-
Time Complexity: O(m × n) where m and n are matrix dimensions.
-
Space Complexity: O(1) for the simple approach, O(m + n) for dictionary approach.
-
The diagonal index can be calculated as
i - jfor each element. -
Elements on the same diagonal have the same
i - jvalue.
Conclusion
A Toeplitz matrix check can be efficiently implemented by comparing each element with its upper-left diagonal neighbor. The simple comparison method is more memory-efficient, while the dictionary approach provides clearer logic for understanding diagonal relationships.
