📜  使用行主映射存储下三角矩阵的有效方法

📅  最后修改于: 2021-09-08 12:30:13             🧑  作者: Mango

给定一个下三角矩阵Mat[][] ,任务是使用行主映射存储矩阵。

下三角矩阵:下三角矩阵是一个方阵,其中矩阵的下三角部分由非零元素组成,上三角部分由0组成。二维矩阵Mat[][]的下三角矩阵在数学上定义为:

  • 如果i < j ,则设置Mat[i][j] = 0
  • 如果i >= j ,则设置Mat[i][j] > 0

存储大小为N的下三角矩阵的有效方法:

  • 非零元素的数量 = 1 + 2 + 3 + … + N = N * (N + 1) /2
  • 0的计数 = N 2 – (N * (N + 1) /2 = (N * (N – 1)/2 )

现在让我们看看如何在我们的程序中表示下三角矩阵。请注意,必须避免存储0以减少内存消耗。根据计算,为了存储非零元素,需要N*(N + 1)/2空间。以上面的例子为例, N = 5 。需要大小为5 * (5 + 1)/2 = 15 的数组来存储非零元素。

现在,二维矩阵的元素可以逐行存储在一维数组中,如下所示:

除了将元素存储在数组中之外,还需要提取与行号和列号对应的元素的过程。
使用 Row-Major Mapping 存储下三角矩阵,索引Mat[i][j]处的元素可以表示为:

下面是上述方法的实现:

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 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live