Number of Relations that are both Irreflexive and Antisymmetric on a Set
Given a positive integer N, the task is to find the number of relations that are irreflexive antisymmetric 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.
A relation R on a set A is called Antisymmetric if and only if (a, b) € R and (b, a) € R, then a = b is called antisymmetric, i.e., the relation R = {(a, b)? R | a ? b} is anti-symmetric, since a ? b and b ? a implies a = b.
Examples:
Input: N = 2
Output: 3
Explanation:
Considering the set {a, b}, all possible relations that are both irreflexive and antisymmetric relations are:
- {}
- {{a, b}}
- {{b, a}}
Input: N = 5
Output: 59049
Approach: The given problem can be solved based on the following observations:
- A relation R on a set A is a subset of the Cartesian Product of a set, i.e., A * A with N2 elements.
- No (x, x) pair should be included in the subset to make sure the relation is irreflexive.
- For the remaining (N2 – N) pairs, divide them into (N2 – N)/2 groups where each group consists of a pair (x, y) and its symmetric pair (y, x).
- Now, there are three choices, either include one of the ordered pairs or neither of them from a group.
- Therefore, the total number of possible relations that are both irreflexive and antisymmetric is given by 3(N2 – N)/2.
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;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
int numberOfRelations( int N)
{
return power(3, (N * N - N) / 2);
}
int main()
{
int N = 2;
cout << numberOfRelations(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mod = 1000000007 ;
static int power( long x, int y)
{
int res = 1 ;
x = x % mod;
while (y > 0 )
{
if (y % 2 == 1 )
res = ( int )(res * x) % mod;
y = y >> 1 ;
x = (x * x) % mod;
}
return res;
}
static int numberOfRelations( int N)
{
return power( 3 , (N * N - N) / 2 );
}
public static void main(String[] args)
{
int N = 2 ;
System.out.print(numberOfRelations(N));
}
}
|
Python3
mod = 1000000007
def power(x, y):
res = 1
x = x % mod
while (y > 0 ):
if (y & 1 ):
res = (res * x) % mod
y = y >> 1
x = (x * x) % mod
return res
def numberOfRelations(N):
return power( 3 , (N * N - N) / / 2 )
if __name__ = = "__main__" :
N = 2
print (numberOfRelations(N))
|
C#
using System;
class GFG{
static int mod = 1000000007;
static int power( long x, int y)
{
int res = 1;
x = x % mod;
while (y > 0)
{
if (y % 2 == 1)
res = ( int )(res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static int numberOfRelations( int N)
{
return power(3, (N * N - N) / 2);
}
public static void Main(String[] args)
{
int N = 2;
Console.Write(numberOfRelations(N));
}
}
|
Javascript
<script>
let mod = 1000000007;
function power(x, y)
{
let res = 1;
x = x % mod;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function numberOfRelations(N)
{
return power(3, (N * N - N) / 2);
}
let N = 2;
document.write(numberOfRelations(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...