📜  将给定矩阵转换为上 Hessenberg 矩阵的最小步骤数

📅  最后修改于: 2022-05-13 01:57:22.979000             🧑  作者: Mango

将给定矩阵转换为上 Hessenberg 矩阵的最小步骤数

给定一个NxN阶矩阵,找到将给定矩阵转换为 Upper Hessenberg 矩阵的最小步数。在每一步中,唯一允许的操作是将任何元素值减少或增加 1。
例子:

方法:

  • 对于要成为上 Hessenberg 矩阵的矩阵,其次对角线以下的所有元素都必须为零,即对于所有 i > j+1A ij = 0。 .
  • 转换上 Hessenberg 矩阵中给定矩阵所需的最小步骤数等于所有 i > j + 1 的所有 A ij绝对值之和。
  • 单元的模量值被考虑在内,因为单元的增加和减少都算作一个步骤。

下面是上述方法的实现:



C++
// C++ implementation of above approach
#include 
#define N 4
using namespace std;
 
// Function to count steps in
// conversion of matrix into upper
// Hessenberg matrix
int stepsRequired(int arr[][N])
{
    int result = 0;
    for (int i = 0; i < N; i++) {
 
        for (int j = 0; j < N; j++) {
 
            // if element is below sub-diagonal
            // add abs(element) into result
            if (i > j + 1)
                result += abs(arr[i][j]);
        }
    }
    return result;
}
 
// Driver code
int main()
{
    int arr[N][N] = { 1, 2, 3, 4,
                      3, 1, 0, 3,
                      3, 2, 1, 3,
                     -3, 4, 2, 1 };
 
    // Function call
    cout << stepsRequired(arr);
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
     
    static int N = 4;
     
    // Function to count steps in
    // conversion of matrix into upper
    // Hessenberg matrix
    static int stepsRequired(int arr[][])
    {
        int result = 0;
        for (int i = 0; i < N; i++)
        {
     
            for (int j = 0; j < N; j++)
            {
     
                // if element is below sub-diagonal
                // add abs(element) into result
                if (i > j + 1)
                    result += Math.abs(arr[i][j]);
            }
        }
        return result;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        int arr [][] = new int [][] {{1, 2, 3, 4},
                        {3, 1, 0, 3},
                        {3, 2, 1, 3},
                        {-3, 4, 2, 1 }};
     
        // Function call
        System.out.println(stepsRequired(arr));
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of above approach
N = 4;
 
# Function to count steps in
# conversion of matrix into upper
# Hessenberg matrix
def stepsRequired(arr):
    result = 0;
    for i in range(N):
 
        for j in range(N):
 
            # if element is below sub-diagonal
            # add abs(element) into result
            if (i > j + 1):
                result += abs(arr[i][j]);
 
    return result;
 
# Driver code
arr =   [[1, 2, 3, 4],
         [3, 1, 0, 3],
         [3, 2, 1, 3],
         [-3, 4, 2, 1]];
 
# Function call
print(stepsRequired(arr));
 
# This code is contributed by Rajput-Ji


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
    static int N = 4;
     
    // Function to count steps in
    // conversion of matrix into upper
    // Hessenberg matrix
    static int stepsRequired(int [, ] arr)
    {
        int result = 0;
        for (int i = 0; i < N; i++)
        {
     
            for (int j = 0; j < N; j++)
            {
     
                // if element is below sub-diagonal
                // add abs(element) into result
                if (i > j + 1)
                    result += Math.Abs(arr[i, j]);
            }
        }
        return result;
    }
     
    // Driver code
    public static void Main ()
    {
         
        int [ , ] arr = new int [, ] { {1, 2, 3, 4},
                        {3, 1, 0, 3},
                        {3, 2, 1, 3},
                        {-3, 4, 2, 1}};
     
        // Function call
        Console.WriteLine(stepsRequired(arr));
     
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
10

时间复杂度: O(N*N)