给定较低的三角矩阵Mat [] [] ,任务是使用行占主导的映射存储矩阵。
下三角矩阵:下三角矩阵是一个正方形矩阵,其中矩阵的下三角部分由非零元素组成,而上三角部分由0 s组成。二维矩阵Mat [] []的下三角矩阵在数学上定义为:
- 如果i
,则设置Mat [i] [j] = 0 。 - 如果i> = j ,则设置Mat [i] [j]> 0 。
Illustration: Below is a 5×5 lower triangular matrix. In general, such matrices can be stored in a 2D array, but when it comes to matrices of large size, it is not a good choice because of its high memory consumption due to the storage of unwanted 0s.
Such a matrix can be implemented in an optimized manner.
存储大小为N的下三角矩阵的有效方法:
- 非零元素的计数= 1 + 2 + 3 +…+ N = N *(N +1)/ 2 。
- 计数为0 s = N 2 –(N *(N + 1)/ 2 =(N *(N – 1)/ 2) 。
现在让我们看看如何在程序中表示下三角矩阵。请注意,必须避免存储0以减少内存消耗。根据计算,为了存储非零元素,需要N *(N +1)/ 2个空间。以上面的示例为例, N = 5 。存储非零元素需要大小为5 *(5 + 1)/ 2 = 15的数组。
现在,可以将2D矩阵的元素逐行存储在1D数组中,如下所示:
除了将元素存储在数组中之外,还需要提取与行号和列号相对应的元素的过程。
使用行主映射存储下三角矩阵,索引为Mat [i] [j]的元素可以表示为:
Index of Mat[i][j] matrix in the array A[] = [i*(i – 1)/2 + j – 1]
下面是上述方法的实现:
C
// C program for the above approach
#include
#include
// Dimensions of a matrix
const int N = 5;
// Structure of the efficient matrix
struct Matrix {
int* A;
int size;
};
// Function to set the
// values in the Matrix
void Set(struct Matrix* mat,
int i, int j, int x)
{
if (i >= j)
mat->A[i * (i - 1) / 2 + j - 1] = x;
}
// Function to store the
// values in the Matrix
int Get(struct Matrix mat, int i, int j)
{
if (i >= j) {
return mat.A[i * (i - 1) / 2 + j - 1];
}
else {
return 0;
}
}
// Function to display the
// elements of the matrix
void Display(struct Matrix mat)
{
int i, j;
// Traverse the matrix
for (i = 1; i <= mat.size; i++) {
for (j = 1; j <= mat.size; j++) {
if (i >= j) {
printf("%d ",
mat.A[i * (i - 1) / 2 + j - 1]);
}
else {
printf("0 ");
}
}
printf("\n");
}
}
// Function to generate an efficient matrix
struct Matrix createMat(int Mat[N][N])
{
// Declare efficient Matrix
struct Matrix mat;
// Initialize the Matrix
mat.size = N;
mat.A = (int*)malloc(
mat.size * (mat.size + 1) / 2
* sizeof(int));
int i, j;
// Set the values in matrix
for (i = 1; i <= mat.size; i++) {
for (j = 1; j <= mat.size; j++) {
Set(&mat, i, j, Mat[i - 1][j - 1]);
}
}
// Return the matrix
return mat;
}
// Driver Code
int main()
{
int Mat[5][5] = { { 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 1, 2, 3, 0, 0 },
{ 1, 2, 3, 4, 0 },
{ 1, 2, 3, 4, 5 } };
// Stores the efficient matrix
struct Matrix mat = createMat(Mat);
// Print the Matrix
Display(mat);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Dimensions of a matrix
static int N = 5;
// Structure of the efficient matrix
static class Matrix {
int[] A;
int size;
};
// Function to set the
// values in the Matrix
static void Set(Matrix mat,
int i, int j, int x)
{
if (i >= j)
mat.A[i * (i - 1) / 2 + j - 1] = x;
}
// Function to store the
// values in the Matrix
static int Get(Matrix mat, int i, int j)
{
if (i >= j) {
return mat.A[i * (i - 1) / 2 + j - 1];
}
else {
return 0;
}
}
// Function to display the
// elements of the matrix
static void Display(Matrix mat)
{
int i, j;
// Traverse the matrix
for (i = 1; i <= mat.size; i++) {
for (j = 1; j <= mat.size; j++) {
if (i >= j) {
System.out.printf("%d ",
mat.A[i * (i - 1) / 2 + j - 1]);
}
else {
System.out.printf("0 ");
}
}
System.out.printf("\n");
}
}
// Function to generate an efficient matrix
static Matrix createMat(int Mat[][])
{
// Declare efficient Matrix
Matrix mat = new Matrix();
// Initialize the Matrix
mat.size = N;
mat.A = new int[(mat.size*(mat.size + 1)) / 2];
int i, j;
// Set the values in matrix
for (i = 1; i <= mat.size; i++)
{
for (j = 1; j <= mat.size; j++)
{
Set(mat, i, j, Mat[i - 1][j - 1]);
}
}
// Return the matrix
return mat;
}
// Driver Code
public static void main(String[] args)
{
int Mat[][] = { { 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 1, 2, 3, 0, 0 },
{ 1, 2, 3, 4, 0 },
{ 1, 2, 3, 4, 5 } };
// Stores the efficient matrix
Matrix mat = createMat(Mat);
// Print the Matrix
Display(mat);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Dimensions of a matrix
static int N = 5;
// Structure of the efficient matrix
class Matrix {
public int[] A;
public int size;
};
// Function to set the
// values in the Matrix
static void Set(Matrix mat,
int i, int j, int x)
{
if (i >= j)
mat.A[i * (i - 1) / 2 + j - 1] = x;
}
// Function to store the
// values in the Matrix
static int Get(Matrix mat, int i, int j)
{
if (i >= j) {
return mat.A[i * (i - 1) / 2 + j - 1];
}
else {
return 0;
}
}
// Function to display the
// elements of the matrix
static void Display(Matrix mat)
{
int i, j;
// Traverse the matrix
for (i = 1; i <= mat.size; i++) {
for (j = 1; j <= mat.size; j++) {
if (i >= j) {
Console.Write("{0} ",
mat.A[i * (i - 1) / 2 + j - 1]);
}
else {
Console.Write("0 ");
}
}
Console.Write("\n");
}
}
// Function to generate an efficient matrix
static Matrix createMat(int [,]Mat)
{
// Declare efficient Matrix
Matrix mat = new Matrix();
// Initialize the Matrix
mat.size = N;
mat.A = new int[(mat.size*(mat.size + 1)) / 2];
int i, j;
// Set the values in matrix
for (i = 1; i <= mat.size; i++)
{
for (j = 1; j <= mat.size; j++)
{
Set(mat, i, j, Mat[i - 1,j - 1]);
}
}
// Return the matrix
return mat;
}
// Driver Code
public static void Main(String[] args)
{
int [,]Mat = { { 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 1, 2, 3, 0, 0 },
{ 1, 2, 3, 4, 0 },
{ 1, 2, 3, 4, 5 } };
// Stores the efficient matrix
Matrix mat = createMat(Mat);
// Print the Matrix
Display(mat);
}
}
// This code is contributed by 29AjayKumar
1 0 0 0 0
1 2 0 0 0
1 2 3 0 0
1 2 3 4 0
1 2 3 4 5
时间复杂度: O(N 2 )
辅助空间: O(N 2 )