Tutorialspoint.dev

Minimum flip required to make Binary Matrix symmetric

Given a Binary Matrix of size N X N, consisting of 1s and 0s. The task is to find the minimum flips required to make the matrix symmetric along main diagonal.

Examples :

Input : mat[][] = { { 0, 0, 1 },
                    { 1, 1, 1 },
                    { 1, 0, 0 } };
Output : 2
Value of mat[1][0] is not equal to mat[0][1].
Value of mat[2][1] is not equal to mat[1][2].
So, two flip are required.

Input : mat[][] = { { 1, 1, 1, 1, 0 },
                    { 0, 1, 0, 1, 1 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 1, 0, 0, 1 } };                  
Output : 3



Method 1 (Simple):
The idea is to find the transpose of the matrix and find minimum number of flip required to make transpose and original matrix equal. To find minimum flip, find the number of position where original matrix and transpose matrix are not same, say x. So, our answer will be x/2.

Below is the implementation of this approach:

C++

// CPP Program to find minimum flip required to make
// Binary Matrix symmetric along main diagonal
#include <bits/stdc++.h>
#define N 3
using namespace std;
  
// Return the minimum flip required to make
// Binary Matrix symmetric along main diagonal.
int minimumflip(int mat[][N], int n)
{
    int transpose[n][n];
  
    // finding the transpose of the matrix
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            transpose[i][j] = mat[j][i];
  
    // Finding the number of position where
    // element are not same.
    int flip = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (transpose[i][j] != mat[i][j])
                flip++;
  
    return flip / 2;
}
  
// Driver Program
int main()
{
    int n = 3;
    int mat[N][N] = {
        { 0, 0, 1 },
        { 1, 1, 1 },
        { 1, 0, 0 }
    };
    cout << minimumflip(mat, n) << endl;
    return 0;
}

Java

// Java Program to find minimum flip
// required to make Binary Matrix
// symmetric along main diagonal
import java.util.*;
  
class GFG {
      
    // Return the minimum flip required
    // to make Binary Matrix symmetric
    // along main diagonal.
    static int minimumflip(int mat[][], int n)
    {
        int transpose[][] = new int[n][n];
       
        // finding the transpose of the matrix
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                transpose[i][j] = mat[j][i];
       
        // Finding the number of position 
        // where element are not same.
        int flip = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (transpose[i][j] != mat[i][j])
                    flip++;
       
        return flip / 2;
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        int n = 3;
        int mat[][] = {{ 0, 0, 1 },
                       { 1, 1, 1 },
                       { 1, 0, 0 }};
          
        System.out.println(minimumflip(mat, n));
    }
}
      
// This code is contributed by Arnav Kr. Mandal.    

Python3

# Python3 code to find minimum flip
# required to make Binary Matrix 
# symmetric along main diagonal
N = 3
  
# Return the minimum flip required 
# to make Binary Matrix symmetric
# along main diagonal.
def minimumflip(mat, n):
      
    transpose =[[0] * n] * n
      
    # finding the transpose of the matrix
    for i in range(n):
        for j in range(n):
            transpose[i][j] = mat[j][i]
      
    # Finding the number of position 
    # where element are not same.
    flip = 0
    for i in range(n):
        for j in range(n):
            if transpose[i][j] != mat[i][j]:
                flip += 1
      
    return int(flip / 2)
      
# Driver Program
n = 3
mat =[[ 0, 0, 1],
      [ 1, 1, 1],
      [ 1, 0, 0]]
print( minimumflip(mat, n))
  
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# Program to find minimum flip
// required to make Binary Matrix
// symmetric along main diagonal
using System;
  
class GFG {
      
    // Return the minimum flip required
    // to make Binary Matrix symmetric
    // along main diagonal.
    static int minimumflip(int [,]mat, int n)
    {
        int [,]transpose = new int[n,n];
      
        // finding the transpose of the matrix
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                transpose[i,j] = mat[j,i];
      
        // Finding the number of position 
        // where element are not same.
        int flip = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (transpose[i,j] != mat[i,j])
                    flip++;
      
        return flip / 2;
    }
      
    /* Driver program to test above function */
    public static void Main() 
    {
        int n = 3;
        int [,]mat = {{ 0, 0, 1 },
                      { 1, 1, 1 },
                      { 1, 0, 0 }};
          
        Console.WriteLine(minimumflip(mat, n));
    }
}
      
// This code is contributed by vt_m. 

PHP

<?php
// PHP Program to find minimum
// flip required to make
$N = 3;
  
// Return the minimum flip 
// required to make Binary 
// Matrix symmetric along 
// main diagonal.
function minimumflip($mat, $n)
{
    global $N;
    $transpose;
  
    // finding the transpose
    // of the matrix
    for ( $i = 0; $i < $n; $i++)
        for ($j = 0; $j < $n; $j++)
            $transpose[$i][$j] = $mat[$j][$i];
  
    // Finding the number of 
    // position where element
    // are not same.
    $flip = 0;
    for ( $i = 0; $i < $n; $i++)
        for ( $j = 0; $j < $n; $j++)
            if ($transpose[$i][$j] != $mat[$i][$j])
                $flip++;
  
    return $flip / 2;
}
  
// Driver Code
$n = 3;
$mat = array(array(0, 0, 1),
             array(1, 1, 1),
             array(1, 0, 0));
  
echo minimumflip($mat, $n)," ";
  
// This code is contributed by aj_36
?>


Output :

2

Method 2: (Efficient Approach)
The idea is to find minimum flip required to make upper triangle of matrix equals to lower triangle of the matrix. To do so, we run two nested loop, outer loop from i = 0 to n i.e for each row of the matrix and the inner loop from j = 0 to i, and check whether mat[i][j] is equal to mat[j][i]. Count of number of instance where theya re not equal will be the minimum flip required to make matrix symmetric along main diagonal.

Below is the implementation of this approach:

C++

// CPP Program to find minimum flip required to make
// Binary Matrix symmetric along main diagonal
#include <bits/stdc++.h>
#define N 3
using namespace std;
  
// Return the minimum flip required to make
// Binary Matrix symmetric along main diagonal.
int minimumflip(int mat[][N], int n)
{
    // Comparing elements across diagonal
    int flip = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < i; j++)
            if (mat[i][j] != mat[j][i])
                flip++;
    return flip;
}
  
// Driver Program
int main()
{
    int n = 3;
    int mat[N][N] = {
        { 0, 0, 1 },
        { 1, 1, 1 },
        { 1, 0, 0 }
    };
    cout << minimumflip(mat, n) << endl;
    return 0;
}

Java

// Java Program to find minimum flip
// required to make Binary Matrix
// symmetric along main diagonal
import java.util.*;
  
class GFG {
      
    // Return the minimum flip required 
    // to make Binary Matrix symmetric
    // along main diagonal.
    static int minimumflip(int mat[][], int n)
    {
        // Comparing elements across diagonal
        int flip = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < i; j++)
                if (mat[i][j] != mat[j][i])
                    flip++;
        return flip;
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        int n = 3;
        int mat[][] = {{ 0, 0, 1 },
                       { 1, 1, 1 },
                       { 1, 0, 0 }};
          
       System.out.println(minimumflip(mat, n));
    }
}
      
// This code is contributed by Arnav Kr. Mandal.    

Python3

# Python3 code to find minimum flip
# required to make Binary Matrix
# symmetric along main diagonal
N = 3
  
# Return the minimum flip required
# to make Binary Matrix symmetric 
# along main diagonal.
def minimumflip( mat , n ):
  
    # Comparing elements across diagonal
    flip = 0
    for i in range(n):
        for j in range(i):
            if mat[i][j] != mat[j][i] :
                flip += 1
      
    return flip
  
# Driver Program
n = 3
mat =[[ 0, 0, 1],
      [ 1, 1, 1],
      [ 1, 0, 0]]
print( minimumflip(mat, n))
  
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# Program to find minimum flip
// required to make Binary Matrix
// symmetric along main diagonal
using System;
  
class GFG {
      
    // Return the minimum flip required 
    // to make Binary Matrix symmetric
    // along main diagonal.
    static int minimumflip(int [,]mat, int n)
    {
          
        // Comparing elements across diagonal
        int flip = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < i; j++)
                if (mat[i,j] != mat[j,i])
                    flip++;
        return flip;
    }
      
    /* Driver program to test above function */
    public static void Main() 
    {
        int n = 3;
        int [,]mat = {{ 0, 0, 1 },
                      { 1, 1, 1 },
                      { 1, 0, 0 }};
          
    Console.WriteLine(minimumflip(mat, n));
    }
}
      
// This code is contributed by vt_m. 

PHP

<?php
// PHP Program to find minimum
// flip required to make Binary
// Matrix symmetric along main diagonal
$N = 3;
  
// Return the minimum flip 
// required to make Binary 
// Matrix symmetric along main diagonal.
  
function minimumflip($mat, $n)
{
    // Comparing elements
    // across diagonal
    $flip = 0;
    for ($i = 0; $i < $n; $i++)
        for ($j = 0; $j < $i; $j++)
            if ($mat[$i][$j] != $mat[$j][$i])
                $flip++;
    return $flip;
}
  
// Driver Code
$n = 3;
$mat = array(array(0, 0, 1),
             array(1, 1, 1),
             array(1, 0, 0));
echo minimumflip($mat, $n), " ";
      
// This code is contributed by ajit
?>


Output :

2


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter