Number of Reflexive Relations on a Set
Given a number n, find out the number of Reflexive Relation on a set of first n natural numbers {1, 2, ..n}.
Examples :
Input: n = 2
Output: 4
The given set A = {1, 2}. The following are reflexive relations on A * A :
{{1, 1), (2, 2)}
{(1, 1), (2, 2), (1, 2)}
{(1, 1), (2, 2), (1, 2), (2, 1)}
{(1, 1), (2, 2), (2, 1)}
Input: n = 3
Output: 64
Explanation :
Reflexive Relation: A Relation R on A a set A is said to be Reflexive if xRx for every element of x ? A.
The number of reflexive relations on an n-element set is 2n(n-1)
How does this formula work?
A relation R is reflexive if the matrix diagonal elements are 1.
If we take a closer look the matrix, we can notice that the size of matrix is n2. The n diagonal entries are fixed. For remaining n2 – n entries, we have choice to either fill 0 or 1. So there are total 2n(n-1) ways of filling the matrix.
Below is the code implementation of the above approach:
CPP
#include <iostream>
using namespace std;
int countReflexive( int n)
{
return (1 << (n*n - n));
}
int main()
{
int n = 3;
cout << countReflexive(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int countReflexive( int n)
{
return ( 1 << (n*n - n));
}
public static void main (String[] args) {
int n = 3 ;
System.out.println(countReflexive(n));
}
}
|
Python3
def countReflexive(n):
return ( 1 << (n * n - n));
n = 3
ans = countReflexive(n);
print (ans)
|
C#
using System;
class GFG {
static int countReflexive( int n)
{
return (1 << (n*n - n));
}
public static void Main () {
int n = 3;
Console.WriteLine(countReflexive(n));
}
}
|
PHP
<?php
function countReflexive( $n )
{
return (1 << ( $n * $n - $n ));
}
$n = 3;
echo countReflexive( $n );
?>
|
Javascript
<script>
function countReflexive(n)
{
return (1 << (n*n - n));
}
let n = 3;
document.write(countReflexive(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...