Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix - GeeksforGeeks

31 Mar, 2022
Given a matrix of order NxN, the task is to find the minimum number of steps to convert given matrix into Diagonally Dominant Matrix. In each step, the only operation allowed is to decrease or increase any element by 1.Examples:  
Input: mat[][] = {{3, 2, 4}, {1, 4, 4}, {2, 3, 4}} Output: 5 Sum of the absolute values of elements of row 1 except the diagonal element is 3 more than abs(arr[0][0]). 1 more than abs(arr[1][1]) in the second row and 1 more than abs(arr[2][2]) in the third row. Hence, 3 + 1 + 1 = 5Input: mat[][] = {{1, 2, 4, 0}, {1, 3, 4, 2}, {3, 3, 4, 2}, {-1, 0, 1, 4}} Output: 13  
 
Approach:  
A square matrix is said to be diagonally dominant matrix if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row.
The minimum number of steps required to convert a given matrix into the diagonally dominant matrix can be calculated depending upon two case: If the sum of the absolute value of all elements of a row except diagonal element is greater than the absolute value of diagonal element then the difference between these two values will be added to the result.Else no need to add anything in the result as in that case row satisfies the condition for a diagonally dominant matrix.
If the sum of the absolute value of all elements of a row except diagonal element is greater than the absolute value of diagonal element then the difference between these two values will be added to the result.Else no need to add anything in the result as in that case row satisfies the condition for a diagonally dominant matrix.
If the sum of the absolute value of all elements of a row except diagonal element is greater than the absolute value of diagonal element then the difference between these two values will be added to the result.
Else no need to add anything in the result as in that case row satisfies the condition for a diagonally dominant matrix.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define N 3 // Function to return the minimum steps// required to convert the given matrix// to a Diagonally Dominant Matrixint findStepsForDDM(int arr[][N]){    int result = 0;     // For each row    for (int i = 0; i < N; i++) {         // To store the sum of the current row        int sum = 0;        for (int j = 0; j < N; j++)            sum += abs(arr[i][j]);         // Remove the element of the current row        // which lies on the main diagonal        sum -= abs(arr[i][i]);         // Checking if the diagonal element is less        // than the sum of non-diagonal element        // then add their difference to the result        if (abs(arr[i][i]) < abs(sum))            result += abs(abs(arr[i][i]) - abs(sum));    }     return result;} // Driven codeint main(){    int arr[N][N] = { { 3, -2, 1 },                      { 1, -3, 2 },                      { -1, 2, 4 } };     cout << findStepsForDDM(arr);     return 0;}
// Java implementation of the approachclass GFG{         final static int N = 3 ;         // Function to return the minimum steps    // required to convert the given matrix    // to a Diagonally Dominant Matrix    static int findStepsForDDM(int arr[][])    {        int result = 0;             // For each row        for (int i = 0; i < N; i++)        {                 // To store the sum of the current row            int sum = 0;            for (int j = 0; j < N; j++)                sum += Math.abs(arr[i][j]);                 // Remove the element of the current row            // which lies on the main diagonal            sum -= Math.abs(arr[i][i]);                 // Checking if the diagonal element is less            // than the sum of non-diagonal element            // then add their difference to the result            if (Math.abs(arr[i][i]) < Math.abs(sum))                result += Math.abs(Math.abs(arr[i][i]) - Math.abs(sum));        }             return result;    }         // Driven code    public static void main (String[] args)    {                 int arr[][] = { { 3, -2, 1 },                        { 1, -3, 2 },                        { -1, 2, 4 } };             System.out.println(findStepsForDDM(arr));    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approach N = 3 # Function to return the minimum steps# required to convert the given matrix# to a Diagonally Dominant Matrixdef findStepsForDDM(arr):     result = 0     # For each row    for i in range(N):         # To store the sum of the current row        sum = 0        for j in range(N):            sum += abs(arr[i][j])         # Remove the element of the current row        # which lies on the main diagonal        sum -= abs(arr[i][i])         # Checking if the diagonal element is less        # than the sum of non-diagonal element        # then add their difference to the result        if (abs(arr[i][i]) < abs(sum)):            result += abs(abs(arr[i][i]) - abs(sum))     return result # Driver code arr= [ [ 3, -2, 1 ],    [ 1, -3, 2 ],    [ -1, 2, 4 ] ] print(findStepsForDDM(arr)) # This code is contributed by mohit kumar 29
// C# implementation of the approachusing System; class GFG{             static int N = 3 ;         // Function to return the minimum steps    // required to convert the given matrix    // to a Diagonally Dominant Matrix    static int findStepsForDDM(int [,]arr)    {        int result = 0;             // For each row        for (int i = 0; i < N; i++)        {                 // To store the sum of the current row            int sum = 0;            for (int j = 0; j < N; j++)                sum += Math.Abs(arr[i,j]);                 // Remove the element of the current row            // which lies on the main diagonal            sum -= Math.Abs(arr[i,i]);                 // Checking if the diagonal element is less            // than the sum of non-diagonal element            // then add their difference to the result            if (Math.Abs(arr[i,i]) < Math.Abs(sum))                result += Math.Abs(Math.Abs(arr[i,i]) - Math.Abs(sum));        }             return result;    }         // Driven code    static public void Main ()    {             int [,]arr = { { 3, -2, 1 },                        { 1, -3, 2 },                        { -1, 2, 4 } };             Console.WriteLine(findStepsForDDM(arr));    }} // This code is contributed by ajit.
<script>// Java script implementation of the approachlet N = 3 ;         // Function to return the minimum steps    // required to convert the given matrix    // to a Diagonally Dominant Matrix    function findStepsForDDM(arr)    {        let result = 0;             // For each row        for (let i = 0; i < N; i++)        {                 // To store the sum of the current row            let sum = 0;            for (let j = 0; j < N; j++)                sum += Math.abs(arr[i][j]);                 // Remove the element of the current row            // which lies on the main diagonal            sum -= Math.abs(arr[i][i]);                 // Checking if the diagonal element is less            // than the sum of non-diagonal element            // then add their difference to the result            if (Math.abs(arr[i][i]) < Math.abs(sum))                result += Math.abs(Math.abs(arr[i][i]) - Math.abs(sum));        }             return result;    }         // Driven code        let arr = [[ 3, -2, 1 ],                        [ 1, -3, 2 ],                        [ -1, 2, 4 ]];             document.write(findStepsForDDM(arr)); // This code is contributed by mohan pavan </script>
0
 
Time complexity: O(N2)
Auxiliary Space: O(1) 
mohit kumar 29
ankthon
jit_t
Akanksha_Rai
pulamolusaimohan
rohitkumarsinghcna
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to multiply two matrices
Multiplication of two Matrices in Single line using Numpy in Python
Min Cost Path | DP-6
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Sudoku | Backtracking-7
The Celebrity Problem
Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
Unique paths in a Grid with Obstacles
Mathematics | L U Decomposition of a System of Linear Equations
Program to find transpose of a matrix