Number of Irreflexive Relations on a Set
Given a positive integer N, the task is to find the number of irreflexive relations that can be formed over the given set of elements. Since the count can be very large, print it to modulo 109 + 7.
A relation R on a set A is called reflexive if no (a, a) € R holds for every element a € A.
For Example: If set A = {a, b} then R = {(a, b), (b, a)} is irreflexive relation.
Examples:
Input: N = 2
Output: 4
Explanation:
Considering the set {1, 2}, the total possible irreflexive relations are:
- {}
- {(1, 2)}
- {(2, 1)}
- {(1, 2), (2, 1)}
Input: N = 5
Output: 1048576
Approach: Follow the steps below to solve the problem:
- A relation R on a set A is a subset of the cartesian product of a set, i.e. A * A with N2 elements.
- Irreflexive Relation: A relation R on a set A is called Irreflexive if and only if x
R x [(x, x) does not belong to R] for every element x in A.
- There are total N pairs of (x, x) are present in the Cartesian product which should not be included in an irreflexive relation. Therefore, for the remaining (N2 – N) elements, each element has two choices i.e., either to include or exclude it in the subset.
- Hence, the total number of possible irreflexive relations is given by 2(N2 – N).
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
const int mod = 1000000007;
int power( long long x, unsigned int y)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
int irreflexiveRelation( int N)
{
return power(2, N * N - N);
}
int main()
{
int N = 2;
cout << irreflexiveRelation(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int mod = 1000000007 ;
static int power( int x, int y)
{
int res = 1 ;
x = x % mod;
if (x == 0 )
return 0 ;
while (y > 0 )
{
if ((y & 1 ) != 0 )
res = (res * x) % mod;
y = y >> 1 ;
x = (x * x) % mod;
}
return res;
}
static int irreflexiveRelation( int N)
{
return power( 2 , N * N - N);
}
public static void main(String[] args)
{
int N = 2 ;
System.out.println(irreflexiveRelation(N));
}
}
|
Python3
mod = 1000000007
def power(x, y):
global mod
res = 1
x = x % mod
if (x = = 0 ):
return 0
while (y > 0 ):
if (y & 1 ):
res = (res * x) % mod
y = y >> 1
x = (x * x) % mod
return res
def irreflexiveRelation(N):
return power( 2 , N * N - N)
if __name__ = = '__main__' :
N = 2
print (irreflexiveRelation(N))
|
C#
using System;
public class GFG
{
static int mod = 1000000007;
static int power( int x, int y)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static int irreflexiveRelation( int N)
{
return power(2, N * N - N);
}
public static void Main(String[] args)
{
int N = 2;
Console.WriteLine(irreflexiveRelation(N));
}
}
|
Javascript
<script>
let mod = 1000000007;
function power(x, y)
{
let res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function irreflexiveRelation(N)
{
return power(2, N * N - N);
}
let N = 2;
document.write(irreflexiveRelation(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
30 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...